blob: 07f77c7e6b22ab10ef24a5026cb6af6343cb0f5c [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;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400477 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400478 struct page **pages = NULL;
479 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400480 unsigned long total_compressed = 0;
481 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400482 int i;
483 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400484 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300485 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400486 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400487
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200488 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
489 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400490
Nikolay Borisov62b37622019-01-03 10:50:00 +0200491 actual_end = min_t(u64, i_size_read(inode), end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400492again:
493 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300494 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100495 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
496 nr_pages = min_t(unsigned long, nr_pages,
497 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400498
Chris Masonf03d9301f2009-02-04 09:31:06 -0500499 /*
500 * we don't want to send crud past the end of i_size through
501 * compression, that's just a waste of CPU time. So, if the
502 * end of the file is before the start of our current
503 * requested range of bytes, we bail out to the uncompressed
504 * cleanup code that can deal with all of this.
505 *
506 * It isn't really the fastest way to fix things, but this is a
507 * very uncommon corner.
508 */
509 if (actual_end <= start)
510 goto cleanup_and_bail_uncompressed;
511
Chris Masonc8b97812008-10-29 14:49:59 -0400512 total_compressed = actual_end - start;
513
Shilong Wang4bcbb332014-10-07 18:44:35 -0400514 /*
515 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400516 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400517 */
518 if (total_compressed <= blocksize &&
519 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
520 goto cleanup_and_bail_uncompressed;
521
David Sterba069eac72017-02-14 19:36:54 +0100522 total_compressed = min_t(unsigned long, total_compressed,
523 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400524 total_in = 0;
525 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400526
Chris Mason771ed682008-11-06 22:02:51 -0500527 /*
528 * we do compression for mount -o compress and when the
529 * inode has not been flagged as nocompress. This flag can
530 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400531 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300532 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400533 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100534 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800535 if (!pages) {
536 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100537 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800538 goto cont;
539 }
Chris Mason179e29e2007-11-01 11:28:41 -0400540
David Sterbaeec63c62017-07-17 19:41:31 +0200541 if (BTRFS_I(inode)->defrag_compress)
542 compress_type = BTRFS_I(inode)->defrag_compress;
543 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200544 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800545
Chris Mason4adaa612013-03-26 13:07:00 -0400546 /*
547 * we need to call clear_page_dirty_for_io on each
548 * page in the range. Otherwise applications with the file
549 * mmap'd can wander in and change the page contents while
550 * we are compressing them.
551 *
552 * If the compression fails for any reason, we set the pages
553 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300554 *
555 * Note that the remaining part is redirtied, the start pointer
556 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400557 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300558 if (!redirty) {
559 extent_range_clear_dirty_for_io(inode, start, end);
560 redirty = 1;
561 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200562
563 /* Compression level is applied here and only here */
564 ret = btrfs_compress_pages(
565 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800566 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100567 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100568 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800569 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100570 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400571
572 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100573 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100574 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400575 char *kaddr;
576
577 /* zero the tail end of the last page, we might be
578 * sending it down to disk
579 */
580 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800581 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400582 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300583 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800584 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400585 }
586 will_compress = 1;
587 }
588 }
Li Zefan560f7d72011-09-08 10:22:01 +0800589cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400590 if (start == 0) {
591 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300592 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400593 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500594 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400595 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200596 ret = cow_file_range_inline(inode, start, end, 0,
597 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400598 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500599 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200600 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000601 total_compressed,
602 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400603 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100604 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400605 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400606 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
607 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100608 unsigned long page_error_op;
609
Filipe Mananae6eb4312014-10-10 10:45:12 +0100610 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400611
Chris Mason771ed682008-11-06 22:02:51 -0500612 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100613 * inline extent creation worked or returned error,
614 * we don't need to create any more async work items.
615 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400616 *
617 * We use DO_ACCOUNTING here because we need the
618 * delalloc_release_metadata to be done _after_ we drop
619 * our outstanding extent for clearing delalloc for this
620 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500621 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300622 extent_clear_unlock_delalloc(inode, start, end, NULL,
623 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800624 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400625 PAGE_CLEAR_DIRTY |
626 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100627 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400628 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300629
630 for (i = 0; i < nr_pages; i++) {
631 WARN_ON(pages[i]->mapping);
632 put_page(pages[i]);
633 }
634 kfree(pages);
635
636 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400637 }
638 }
639
640 if (will_compress) {
641 /*
642 * we aren't doing an inline extent round the compressed size
643 * up to a block size boundary so the allocator does sane
644 * things
645 */
Qu Wenruofda28322013-02-26 08:10:22 +0000646 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400647
648 /*
649 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300650 * win, compare the page count read with the blocks on disk,
651 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400652 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300653 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300654 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300655 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700656
657 /*
658 * The async work queues will take care of doing actual
659 * allocation on disk for these compressed pages, and
660 * will submit them to the elevator.
661 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200662 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100663 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700664 compress_type);
665
Timofey Titovets11708622017-10-03 18:06:01 +0300666 if (start + total_in < end) {
667 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700668 pages = NULL;
669 cond_resched();
670 goto again;
671 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300672 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400673 }
674 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700675 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400676 /*
677 * the compression code ran but failed to make things smaller,
678 * free any pages it allocated and our page pointer array
679 */
David Sterba4d3a8002017-02-14 19:04:07 +0100680 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400681 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300682 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400683 }
684 kfree(pages);
685 pages = NULL;
686 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100687 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400688
689 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400690 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200691 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500692 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500693 }
Chris Masonc8b97812008-10-29 14:49:59 -0400694 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500695cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700696 /*
697 * No compression, but we still need to write the pages in the file
698 * we've been given so far. redirty the locked page if it corresponds
699 * to our extent and set things up for the async work queue to run
700 * cow_file_range to do the normal delalloc dance.
701 */
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200702 if (page_offset(async_chunk->locked_page) >= start &&
703 page_offset(async_chunk->locked_page) <= end)
704 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700705 /* unlocked later on in the async handlers */
706
707 if (redirty)
708 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200709 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700710 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300711 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500712
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300713 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500714}
Chris Mason771ed682008-11-06 22:02:51 -0500715
Filipe Manana40ae8372014-10-06 22:14:24 +0100716static void free_async_extent_pages(struct async_extent *async_extent)
717{
718 int i;
719
720 if (!async_extent->pages)
721 return;
722
723 for (i = 0; i < async_extent->nr_pages; i++) {
724 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300725 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100726 }
727 kfree(async_extent->pages);
728 async_extent->nr_pages = 0;
729 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500730}
731
732/*
733 * phase two of compressed writeback. This is the ordered portion
734 * of the code, which only gets called in the order the work was
735 * queued. We walk all the async extents created by compress_file_range
736 * and send them down to the disk.
737 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200738static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500739{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200740 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400741 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500742 struct async_extent *async_extent;
743 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500744 struct btrfs_key ins;
745 struct extent_map *em;
746 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200747 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500748 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500749
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500750again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200751 while (!list_empty(&async_chunk->extents)) {
752 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500753 struct async_extent, list);
754 list_del(&async_extent->list);
755
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500756retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200757 lock_extent(io_tree, async_extent->start,
758 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500759 /* did the compression code fall back to uncompressed IO? */
760 if (!async_extent->pages) {
761 int page_started = 0;
762 unsigned long nr_written = 0;
763
Chris Mason771ed682008-11-06 22:02:51 -0500764 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200765 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500766 async_extent->start,
767 async_extent->start +
768 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300769 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500770
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100771 /* JDM XXX */
772
Chris Mason771ed682008-11-06 22:02:51 -0500773 /*
774 * if page_started, cow_file_range inserted an
775 * inline extent and took care of all the unlocking
776 * and IO for us. Otherwise, we need to submit
777 * all those pages down to the drive.
778 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500779 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200780 extent_write_locked_range(inode,
781 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500782 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500783 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500784 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500785 else if (ret)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200786 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500787 kfree(async_extent);
788 cond_resched();
789 continue;
790 }
791
Wang Xiaoguang18513092016-07-25 15:51:40 +0800792 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500793 async_extent->compressed_size,
794 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800795 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500796 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100797 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400799 if (ret == -ENOSPC) {
800 unlock_extent(io_tree, async_extent->start,
801 async_extent->start +
802 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800803
804 /*
805 * we need to redirty the pages if we decide to
806 * fallback to uncompressed IO, otherwise we
807 * will not submit these pages down to lower
808 * layers.
809 */
810 extent_range_redirty_for_io(inode,
811 async_extent->start,
812 async_extent->start +
813 async_extent->ram_size - 1);
814
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100815 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400816 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500817 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500818 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000819 /*
820 * here we're doing allocation and writeback of the
821 * compressed pages
822 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800823 em = create_io_em(inode, async_extent->start,
824 async_extent->ram_size, /* len */
825 async_extent->start, /* orig_start */
826 ins.objectid, /* block_start */
827 ins.offset, /* block_len */
828 ins.offset, /* orig_block_len */
829 async_extent->ram_size, /* ram_bytes */
830 async_extent->compress_type,
831 BTRFS_ORDERED_COMPRESSED);
832 if (IS_ERR(em))
833 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500834 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800835 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500836
Li Zefan261507a02010-12-17 14:21:50 +0800837 ret = btrfs_add_ordered_extent_compress(inode,
838 async_extent->start,
839 ins.objectid,
840 async_extent->ram_size,
841 ins.offset,
842 BTRFS_ORDERED_COMPRESSED,
843 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100844 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200845 btrfs_drop_extent_cache(BTRFS_I(inode),
846 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100847 async_extent->start +
848 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500849 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100850 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400851 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500852
Chris Mason771ed682008-11-06 22:02:51 -0500853 /*
854 * clear dirty, set writeback and unlock the pages.
855 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400856 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400857 async_extent->start +
858 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400859 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
860 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400861 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200862 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500863 async_extent->start,
864 async_extent->ram_size,
865 ins.objectid,
866 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600867 async_extent->nr_pages,
Nikolay Borisovb5326272019-03-12 17:20:25 +0200868 async_chunk->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100869 struct page *p = async_extent->pages[0];
870 const u64 start = async_extent->start;
871 const u64 end = start + async_extent->ram_size - 1;
872
873 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200874 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200875
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100876 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300877 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800878 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100879 PAGE_END_WRITEBACK |
880 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100881 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100882 }
Chris Mason771ed682008-11-06 22:02:51 -0500883 alloc_hint = ins.objectid + ins.offset;
884 kfree(async_extent);
885 cond_resched();
886 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100887 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500888out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400889 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400890 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100891out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400892 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500893 async_extent->start +
894 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400895 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100896 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400897 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
898 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100899 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
900 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100901 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100902 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500903 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500904}
905
Josef Bacik4b46fce2010-05-23 11:00:55 -0400906static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
907 u64 num_bytes)
908{
909 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
910 struct extent_map *em;
911 u64 alloc_hint = 0;
912
913 read_lock(&em_tree->lock);
914 em = search_extent_mapping(em_tree, start, num_bytes);
915 if (em) {
916 /*
917 * if block start isn't an actual block number then find the
918 * first block in this inode and use that as a hint. If that
919 * block is also bogus then just don't worry about it.
920 */
921 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
922 free_extent_map(em);
923 em = search_extent_mapping(em_tree, 0, 0);
924 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
925 alloc_hint = em->block_start;
926 if (em)
927 free_extent_map(em);
928 } else {
929 alloc_hint = em->block_start;
930 free_extent_map(em);
931 }
932 }
933 read_unlock(&em_tree->lock);
934
935 return alloc_hint;
936}
937
Chris Mason771ed682008-11-06 22:02:51 -0500938/*
939 * when extent_io.c finds a delayed allocation range in the file,
940 * the call backs end up in this code. The basic idea is to
941 * allocate extents on disk for the range, and create ordered data structs
942 * in ram to track those extents.
943 *
944 * locked_page is the page that writepage had locked already. We use
945 * it to make sure we don't do extra locks or unlocks.
946 *
947 * *page_started is set to one if we unlock locked_page and do everything
948 * required to start IO on it. It may be clean and already done with
949 * IO when we return.
950 */
Josef Bacik00361582013-08-14 14:02:47 -0400951static noinline int cow_file_range(struct inode *inode,
952 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300953 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300954 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500955{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400956 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400957 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500958 u64 alloc_hint = 0;
959 u64 num_bytes;
960 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000961 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400962 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500963 struct btrfs_key ins;
964 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000965 unsigned clear_bits;
966 unsigned long page_ops;
967 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500968 int ret = 0;
969
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200970 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400971 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500972 ret = -EINVAL;
973 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400974 }
Chris Mason771ed682008-11-06 22:02:51 -0500975
Qu Wenruofda28322013-02-26 08:10:22 +0000976 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500977 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800978 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500979
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200980 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400981
Chris Mason771ed682008-11-06 22:02:51 -0500982 if (start == 0) {
983 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200984 ret = cow_file_range_inline(inode, start, end, 0,
985 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500986 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400987 /*
988 * We use DO_ACCOUNTING here because we need the
989 * delalloc_release_metadata to be run _after_ we drop
990 * our outstanding extent for clearing delalloc for this
991 * range.
992 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300993 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400994 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400995 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
996 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400997 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
998 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500999 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001000 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001001 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001002 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001003 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001005 }
1006 }
Chris Masonc8b97812008-10-29 14:49:59 -04001007
Josef Bacik4b46fce2010-05-23 11:00:55 -04001008 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001009 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1010 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001011
Anand Jain3752d222018-02-15 12:29:38 +08001012 while (num_bytes > 0) {
1013 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001014 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001015 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001016 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001017 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001018 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001019 cur_alloc_size = ins.offset;
1020 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001021
Chris Mason771ed682008-11-06 22:02:51 -05001022 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001023 em = create_io_em(inode, start, ins.offset, /* len */
1024 start, /* orig_start */
1025 ins.objectid, /* block_start */
1026 ins.offset, /* block_len */
1027 ins.offset, /* orig_block_len */
1028 ram_size, /* ram_bytes */
1029 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001030 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001031 if (IS_ERR(em)) {
1032 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001033 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001034 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001035 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001036
Chris Masone6dcd2d2008-07-17 12:53:50 -04001037 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001038 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001039 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001040 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001041
Yan Zheng17d217f2008-12-12 10:03:38 -05001042 if (root->root_key.objectid ==
1043 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1044 ret = btrfs_reloc_clone_csums(inode, start,
1045 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001046 /*
1047 * Only drop cache here, and process as normal.
1048 *
1049 * We must not allow extent_clear_unlock_delalloc()
1050 * at out_unlock label to free meta of this ordered
1051 * extent, as its meta should be freed by
1052 * btrfs_finish_ordered_io().
1053 *
1054 * So we must continue until @start is increased to
1055 * skip current ordered extent.
1056 */
Josef Bacik00361582013-08-14 14:02:47 -04001057 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001058 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1059 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001060 }
1061
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001062 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001063
Chris Masonc8b97812008-10-29 14:49:59 -04001064 /* we're not doing compressed IO, don't unlock the first
1065 * page (which the caller expects to stay locked), don't
1066 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001067 *
1068 * Do set the Private2 bit so we know this page was properly
1069 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001070 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001071 page_ops = unlock ? PAGE_UNLOCK : 0;
1072 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001073
Josef Bacikc2790a22013-07-29 11:20:47 -04001074 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001075 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001076 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001077 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001078 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001079 if (num_bytes < cur_alloc_size)
1080 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001081 else
Anand Jain3752d222018-02-15 12:29:38 +08001082 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001083 alloc_hint = ins.objectid + ins.offset;
1084 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001085 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001086
1087 /*
1088 * btrfs_reloc_clone_csums() error, since start is increased
1089 * extent_clear_unlock_delalloc() at out_unlock label won't
1090 * free metadata of current ordered extent, we're OK to exit.
1091 */
1092 if (ret)
1093 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001094 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001095out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001096 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001097
Filipe Mananad9f85962014-08-25 10:43:00 +01001098out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001099 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001100out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001101 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001102 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001103out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001104 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1105 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001106 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1107 PAGE_END_WRITEBACK;
1108 /*
1109 * If we reserved an extent for our delalloc range (or a subrange) and
1110 * failed to create the respective ordered extent, then it means that
1111 * when we reserved the extent we decremented the extent's size from
1112 * the data space_info's bytes_may_use counter and incremented the
1113 * space_info's bytes_reserved counter by the same amount. We must make
1114 * sure extent_clear_unlock_delalloc() does not try to decrement again
1115 * the data space_info's bytes_may_use counter, therefore we do not pass
1116 * it the flag EXTENT_CLEAR_DATA_RESV.
1117 */
1118 if (extent_reserved) {
1119 extent_clear_unlock_delalloc(inode, start,
1120 start + cur_alloc_size,
Filipe Mananaa315e682017-03-06 23:04:20 +00001121 locked_page,
1122 clear_bits,
1123 page_ops);
1124 start += cur_alloc_size;
1125 if (start >= end)
1126 goto out;
1127 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001128 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001129 clear_bits | EXTENT_CLEAR_DATA_RESV,
1130 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001131 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001132}
Chris Masonc8b97812008-10-29 14:49:59 -04001133
Chris Mason771ed682008-11-06 22:02:51 -05001134/*
1135 * work queue call back to started compression on a file and pages
1136 */
1137static noinline void async_cow_start(struct btrfs_work *work)
1138{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001139 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001140 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001141
Nikolay Borisovb5326272019-03-12 17:20:25 +02001142 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001143
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001144 compressed_extents = compress_file_range(async_chunk);
1145 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001146 btrfs_add_delayed_iput(async_chunk->inode);
1147 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001148 }
Chris Mason771ed682008-11-06 22:02:51 -05001149}
1150
1151/*
1152 * work queue call back to submit previously compressed pages
1153 */
1154static noinline void async_cow_submit(struct btrfs_work *work)
1155{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001156 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1157 work);
1158 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001159 unsigned long nr_pages;
1160
Nikolay Borisovb5326272019-03-12 17:20:25 +02001161 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001162 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001163
David Sterba093258e2018-02-26 16:15:17 +01001164 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001165 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001166 5 * SZ_1M)
1167 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001168
Nikolay Borisov4546d172019-01-03 10:50:03 +02001169 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001170 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001171 * in which case we don't have anything to submit, yet we need to
1172 * always adjust ->async_delalloc_pages as its paired with the init
1173 * happening in cow_file_range_async
1174 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001175 if (async_chunk->inode)
1176 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001177}
Chris Masonc8b97812008-10-29 14:49:59 -04001178
Chris Mason771ed682008-11-06 22:02:51 -05001179static noinline void async_cow_free(struct btrfs_work *work)
1180{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001181 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001182
Nikolay Borisovb5326272019-03-12 17:20:25 +02001183 async_chunk = container_of(work, struct async_chunk, work);
1184 if (async_chunk->inode)
1185 btrfs_add_delayed_iput(async_chunk->inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001186 /*
1187 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001188 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001189 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001190 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001191 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001192}
1193
1194static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1195 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001196 unsigned long *nr_written,
1197 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001198{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001200 struct async_cow *ctx;
1201 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001202 unsigned long nr_pages;
1203 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001204 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1205 int i;
1206 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001207 unsigned nofs_flag;
Chris Mason771ed682008-11-06 22:02:51 -05001208
Nikolay Borisov69684c52019-03-12 17:20:28 +02001209 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001210
1211 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1212 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1213 num_chunks = 1;
1214 should_compress = false;
1215 } else {
1216 should_compress = true;
1217 }
1218
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001219 nofs_flag = memalloc_nofs_save();
1220 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1221 memalloc_nofs_restore(nofs_flag);
1222
Nikolay Borisov97db1202019-03-12 17:20:24 +02001223 if (!ctx) {
1224 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1225 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1226 EXTENT_DO_ACCOUNTING;
1227 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1228 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1229 PAGE_SET_ERROR;
1230
Nikolay Borisov74e91942019-07-17 16:18:16 +03001231 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001232 clear_bits, page_ops);
1233 return -ENOMEM;
1234 }
1235
1236 async_chunk = ctx->chunks;
1237 atomic_set(&ctx->num_chunks, num_chunks);
1238
1239 for (i = 0; i < num_chunks; i++) {
1240 if (should_compress)
1241 cur_end = min(end, start + SZ_512K - 1);
1242 else
1243 cur_end = end;
1244
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001245 /*
1246 * igrab is called higher up in the call chain, take only the
1247 * lightweight reference for the callback lifetime
1248 */
1249 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001250 async_chunk[i].pending = &ctx->num_chunks;
1251 async_chunk[i].inode = inode;
1252 async_chunk[i].start = start;
1253 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001254 async_chunk[i].locked_page = locked_page;
1255 async_chunk[i].write_flags = write_flags;
1256 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001257
Nikolay Borisov97db1202019-03-12 17:20:24 +02001258 btrfs_init_work(&async_chunk[i].work,
Liu Bo9e0af232014-08-15 23:36:53 +08001259 btrfs_delalloc_helper,
1260 async_cow_start, async_cow_submit,
1261 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001262
Nikolay Borisov97db1202019-03-12 17:20:24 +02001263 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001264 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001265
Nikolay Borisov97db1202019-03-12 17:20:24 +02001266 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001267
Chris Mason771ed682008-11-06 22:02:51 -05001268 *nr_written += nr_pages;
1269 start = cur_end + 1;
1270 }
1271 *page_started = 1;
1272 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001273}
1274
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001275static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001276 u64 bytenr, u64 num_bytes)
1277{
1278 int ret;
1279 struct btrfs_ordered_sum *sums;
1280 LIST_HEAD(list);
1281
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001282 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001283 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001284 if (ret == 0 && list_empty(&list))
1285 return 0;
1286
1287 while (!list_empty(&list)) {
1288 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1289 list_del(&sums->list);
1290 kfree(sums);
1291 }
Liu Bo58113752018-01-31 17:09:13 -07001292 if (ret < 0)
1293 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001294 return 1;
1295}
1296
Chris Masond352ac62008-09-29 15:18:18 -04001297/*
1298 * when nowcow writeback call back. This checks for snapshots or COW copies
1299 * of the extents that exist in the file, and COWs the file as required.
1300 *
1301 * If no cow copies or snapshots exist, we write directly to the existing
1302 * blocks on disk
1303 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001304static noinline int run_delalloc_nocow(struct inode *inode,
1305 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001306 const u64 start, const u64 end,
1307 int *page_started, int force,
1308 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001309{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001310 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001311 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001312 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001313 u64 cow_start = (u64)-1;
1314 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001315 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001316 bool check_prev = true;
1317 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001318 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001319 bool nocow = false;
1320 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001321
1322 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001323 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001324 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001325 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001326 EXTENT_DO_ACCOUNTING |
1327 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001328 PAGE_CLEAR_DIRTY |
1329 PAGE_SET_WRITEBACK |
1330 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001331 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001332 }
Li Zefan82d59022011-04-20 10:33:24 +08001333
Yan Zheng80ff3852008-10-30 14:20:02 -04001334 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001335 struct btrfs_key found_key;
1336 struct btrfs_file_extent_item *fi;
1337 struct extent_buffer *leaf;
1338 u64 extent_end;
1339 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001340 u64 num_bytes = 0;
1341 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001342 u64 ram_bytes;
1343 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001344
1345 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001346
Liu Boe4c3b2d2017-01-30 12:25:28 -08001347 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001349 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001350 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001351
1352 /*
1353 * If there is no extent for our range when doing the initial
1354 * search, then go back to the previous slot as it will be the
1355 * one containing the search offset
1356 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1358 leaf = path->nodes[0];
1359 btrfs_item_key_to_cpu(leaf, &found_key,
1360 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001361 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 found_key.type == BTRFS_EXTENT_DATA_KEY)
1363 path->slots[0]--;
1364 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001365 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001366next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001367 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 leaf = path->nodes[0];
1369 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1370 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001371 if (ret < 0) {
1372 if (cow_start != (u64)-1)
1373 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001374 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001375 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001376 if (ret > 0)
1377 break;
1378 leaf = path->nodes[0];
1379 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001380
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001382
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001383 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001384 if (found_key.objectid > ino)
1385 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001386 /*
1387 * Keep searching until we find an EXTENT_ITEM or there are no
1388 * more extents for this inode
1389 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001390 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1391 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1392 path->slots[0]++;
1393 goto next_slot;
1394 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001395
1396 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001397 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001398 found_key.offset > end)
1399 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001400
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001401 /*
1402 * If the found extent starts after requested offset, then
1403 * adjust extent_end to be right before this extent begins
1404 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001405 if (found_key.offset > cur_offset) {
1406 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001407 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 goto out_check;
1409 }
Chris Masonc31f8832008-01-08 15:46:31 -05001410
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001411 /*
1412 * Found extent which begins before our range and potentially
1413 * intersect it
1414 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001415 fi = btrfs_item_ptr(leaf, path->slots[0],
1416 struct btrfs_file_extent_item);
1417 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001418
Josef Bacikcc95bef2013-04-04 14:31:27 -04001419 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001420 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1421 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001422 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001423 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001424 extent_end = found_key.offset +
1425 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001426 disk_num_bytes =
1427 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001428 /*
1429 * If extent we got ends before our range starts, skip
1430 * to next extent
1431 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001432 if (extent_end <= start) {
1433 path->slots[0]++;
1434 goto next_slot;
1435 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001436 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001437 if (disk_bytenr == 0)
1438 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001439 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001440 if (btrfs_file_extent_compression(leaf, fi) ||
1441 btrfs_file_extent_encryption(leaf, fi) ||
1442 btrfs_file_extent_other_encoding(leaf, fi))
1443 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001444 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001445 * If extent is created before the last volume's snapshot
1446 * this implies the extent is shared, hence we can't do
1447 * nocow. This is the same check as in
1448 * btrfs_cross_ref_exist but without calling
1449 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001450 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001451 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001452 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001453 btrfs_root_last_snapshot(&root->root_item))
1454 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001455 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1456 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001457 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001458 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001459 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001460 ret = btrfs_cross_ref_exist(root, ino,
1461 found_key.offset -
1462 extent_offset, disk_bytenr);
1463 if (ret) {
1464 /*
1465 * ret could be -EIO if the above fails to read
1466 * metadata.
1467 */
1468 if (ret < 0) {
1469 if (cow_start != (u64)-1)
1470 cur_offset = cow_start;
1471 goto error;
1472 }
1473
Nikolay Borisov3e024842019-08-21 10:42:03 +03001474 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001475 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001476 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001477 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001478 disk_bytenr += cur_offset - found_key.offset;
1479 num_bytes = min(end + 1, extent_end) - cur_offset;
1480 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001481 * If there are pending snapshots for this root, we
1482 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001483 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001484 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001485 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001486 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001487 * force cow if csum exists in the range.
1488 * this ensure that csum for a given extent are
1489 * either valid or do not exist.
1490 */
Liu Bo58113752018-01-31 17:09:13 -07001491 ret = csum_exist_in_range(fs_info, disk_bytenr,
1492 num_bytes);
1493 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001494 /*
1495 * ret could be -EIO if the above fails to read
1496 * metadata.
1497 */
1498 if (ret < 0) {
1499 if (cow_start != (u64)-1)
1500 cur_offset = cow_start;
1501 goto error;
1502 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001503 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001504 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001505 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001506 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001507 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001508 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001509 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001510 extent_end = found_key.offset + ram_bytes;
1511 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001512 /* Skip extents outside of our requested range */
1513 if (extent_end <= start) {
1514 path->slots[0]++;
1515 goto next_slot;
1516 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001517 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001518 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001519 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001520 }
1521out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001522 /*
1523 * If nocow is false then record the beginning of the range
1524 * that needs to be COWed
1525 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001526 if (!nocow) {
1527 if (cow_start == (u64)-1)
1528 cow_start = cur_offset;
1529 cur_offset = extent_end;
1530 if (cur_offset > end)
1531 break;
1532 path->slots[0]++;
1533 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001534 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001535
David Sterbab3b4aa72011-04-21 01:20:15 +02001536 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001537
1538 /*
1539 * COW range from cow_start to found_key.offset - 1. As the key
1540 * will contain the beginning of the first extent that can be
1541 * NOCOW, following one which needs to be COW'ed
1542 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001543 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001544 ret = cow_file_range(inode, locked_page,
1545 cow_start, found_key.offset - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001546 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001547 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001548 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001549 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001550 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001551 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001552 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001553 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001554 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001555
Yan Zhengd899e052008-10-30 14:25:28 -04001556 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001557 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001558 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001559
1560 em = create_io_em(inode, cur_offset, num_bytes,
1561 orig_start,
1562 disk_bytenr, /* block_start */
1563 num_bytes, /* block_len */
1564 disk_num_bytes, /* orig_block_len */
1565 ram_bytes, BTRFS_COMPRESS_NONE,
1566 BTRFS_ORDERED_PREALLOC);
1567 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001568 if (nocow)
1569 btrfs_dec_nocow_writers(fs_info,
1570 disk_bytenr);
1571 ret = PTR_ERR(em);
1572 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001573 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001574 free_extent_map(em);
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001575 ret = btrfs_add_ordered_extent(inode, cur_offset,
1576 disk_bytenr, num_bytes,
1577 num_bytes,
1578 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001579 if (ret) {
1580 btrfs_drop_extent_cache(BTRFS_I(inode),
1581 cur_offset,
1582 cur_offset + num_bytes - 1,
1583 0);
1584 goto error;
1585 }
Yan Zhengd899e052008-10-30 14:25:28 -04001586 } else {
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001587 ret = btrfs_add_ordered_extent(inode, cur_offset,
1588 disk_bytenr, num_bytes,
1589 num_bytes,
1590 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001591 if (ret)
1592 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001593 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001594
Filipe Mananaf78c4362016-05-09 13:15:41 +01001595 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001596 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001597 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001598
Yan, Zhengefa56462010-05-16 10:49:59 -04001599 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001600 BTRFS_DATA_RELOC_TREE_OBJECTID)
1601 /*
1602 * Error handled later, as we must prevent
1603 * extent_clear_unlock_delalloc() in error handler
1604 * from freeing metadata of created ordered extent.
1605 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001606 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1607 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001608
Josef Bacikc2790a22013-07-29 11:20:47 -04001609 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001610 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001611 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001612 EXTENT_DELALLOC |
1613 EXTENT_CLEAR_DATA_RESV,
1614 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1615
Yan Zheng80ff3852008-10-30 14:20:02 -04001616 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001617
1618 /*
1619 * btrfs_reloc_clone_csums() error, now we're OK to call error
1620 * handler, as metadata for created ordered extent will only
1621 * be freed by btrfs_finish_ordered_io().
1622 */
1623 if (ret)
1624 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001625 if (cur_offset > end)
1626 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001627 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001628 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001629
Robbie Ko506481b2018-10-30 18:04:04 +08001630 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001631 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001632
Yan Zheng80ff3852008-10-30 14:20:02 -04001633 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001634 cur_offset = end;
Nikolay Borisov74e91942019-07-17 16:18:16 +03001635 ret = cow_file_range(inode, locked_page, cow_start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001636 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001637 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001638 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001639 }
1640
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001641error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001642 if (nocow)
1643 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1644
Josef Bacik17ca04a2012-05-31 15:58:55 -04001645 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001646 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001647 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001648 EXTENT_DELALLOC | EXTENT_DEFRAG |
1649 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1650 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001651 PAGE_SET_WRITEBACK |
1652 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001653 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001654 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001655}
1656
Wang Shilong47059d92014-07-03 18:22:07 +08001657static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1658{
1659
1660 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1661 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1662 return 0;
1663
1664 /*
1665 * @defrag_bytes is a hint value, no spinlock held here,
1666 * if is not zero, it means the file is defragging.
1667 * Force cow if given extent needs to be defragged.
1668 */
1669 if (BTRFS_I(inode)->defrag_bytes &&
1670 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1671 EXTENT_DEFRAG, 0, NULL))
1672 return 1;
1673
1674 return 0;
1675}
1676
Chris Masond352ac62008-09-29 15:18:18 -04001677/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001678 * Function to process delayed allocation (create CoW) for ranges which are
1679 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001680 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001681int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001682 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1683 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001684{
Chris Masonbe20aa92007-12-17 20:14:01 -05001685 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001686 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001687 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001688
Wang Shilong47059d92014-07-03 18:22:07 +08001689 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001690 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001691 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001692 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001693 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001694 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001695 } else if (!inode_can_compress(inode) ||
1696 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001697 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001698 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001699 } else {
1700 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1701 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001702 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001703 page_started, nr_written,
1704 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001705 }
Qu Wenruo524272602017-03-08 10:25:52 +08001706 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001707 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1708 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001709 return ret;
1710}
1711
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001712void btrfs_split_delalloc_extent(struct inode *inode,
1713 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001714{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001715 u64 size;
1716
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001717 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001718 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001719 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001720
Josef Bacikdcab6a32015-02-11 15:08:59 -05001721 size = orig->end - orig->start + 1;
1722 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001723 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001724 u64 new_size;
1725
1726 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001727 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001728 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001729 */
1730 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001731 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001732 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001733 num_extents += count_max_extents(new_size);
1734 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001735 return;
1736 }
1737
Josef Bacik9e0baf62011-07-15 15:16:44 +00001738 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001739 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001740 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001741}
1742
1743/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001744 * Handle merged delayed allocation extents so we can keep track of new extents
1745 * that are just merged onto old extents, such as when we are doing sequential
1746 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001747 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001748void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1749 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001750{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001751 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001752 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001753
Josef Bacik9ed74f22009-09-11 16:12:44 -04001754 /* not delalloc, ignore it */
1755 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001756 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001757
Josef Bacik8461a3d2015-03-13 15:12:08 -04001758 if (new->start > other->start)
1759 new_size = new->end - other->start + 1;
1760 else
1761 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001762
1763 /* we're not bigger than the max, unreserve the space and go */
1764 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1765 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001766 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001767 spin_unlock(&BTRFS_I(inode)->lock);
1768 return;
1769 }
1770
1771 /*
Josef Bacikba117212015-03-13 15:01:24 -04001772 * We have to add up either side to figure out how many extents were
1773 * accounted for before we merged into one big extent. If the number of
1774 * extents we accounted for is <= the amount we need for the new range
1775 * then we can return, otherwise drop. Think of it like this
1776 *
1777 * [ 4k][MAX_SIZE]
1778 *
1779 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1780 * need 2 outstanding extents, on one side we have 1 and the other side
1781 * we have 1 so they are == and we can return. But in this case
1782 *
1783 * [MAX_SIZE+4k][MAX_SIZE+4k]
1784 *
1785 * Each range on their own accounts for 2 extents, but merged together
1786 * they are only 3 extents worth of accounting, so we need to drop in
1787 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001788 */
Josef Bacikba117212015-03-13 15:01:24 -04001789 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001790 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001791 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001792 num_extents += count_max_extents(old_size);
1793 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001794 return;
1795
Josef Bacik9e0baf62011-07-15 15:16:44 +00001796 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001797 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001798 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001799}
1800
Miao Xieeb73c1b2013-05-15 07:48:22 +00001801static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1802 struct inode *inode)
1803{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001804 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1805
Miao Xieeb73c1b2013-05-15 07:48:22 +00001806 spin_lock(&root->delalloc_lock);
1807 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1808 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1809 &root->delalloc_inodes);
1810 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1811 &BTRFS_I(inode)->runtime_flags);
1812 root->nr_delalloc_inodes++;
1813 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001814 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001815 BUG_ON(!list_empty(&root->delalloc_root));
1816 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001817 &fs_info->delalloc_roots);
1818 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001819 }
1820 }
1821 spin_unlock(&root->delalloc_lock);
1822}
1823
Nikolay Borisov2b877332018-04-27 12:21:51 +03001824
1825void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1826 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001827{
David Sterba3ffbd682018-06-29 10:56:42 +02001828 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001829
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001830 if (!list_empty(&inode->delalloc_inodes)) {
1831 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001832 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001833 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001834 root->nr_delalloc_inodes--;
1835 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001836 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001837 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001838 BUG_ON(list_empty(&root->delalloc_root));
1839 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001840 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001841 }
1842 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001843}
1844
1845static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1846 struct btrfs_inode *inode)
1847{
1848 spin_lock(&root->delalloc_lock);
1849 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001850 spin_unlock(&root->delalloc_lock);
1851}
1852
Chris Masond352ac62008-09-29 15:18:18 -04001853/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001854 * Properly track delayed allocation bytes in the inode and to maintain the
1855 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001856 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001857void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1858 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001859{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001860 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1861
Wang Shilong47059d92014-07-03 18:22:07 +08001862 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1863 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001864 /*
1865 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001866 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001867 * bit, which is only set or cleared with irqs on
1868 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001869 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001870 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001871 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001872 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001873 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001874
Josef Bacik8b62f872017-10-19 14:15:55 -04001875 spin_lock(&BTRFS_I(inode)->lock);
1876 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1877 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001878
Josef Bacik6a3891c2015-03-16 17:38:52 -04001879 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001880 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001881 return;
1882
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001883 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1884 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001885 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001886 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001887 if (*bits & EXTENT_DEFRAG)
1888 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001889 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001890 &BTRFS_I(inode)->runtime_flags))
1891 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001892 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001893 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001894
1895 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1896 (*bits & EXTENT_DELALLOC_NEW)) {
1897 spin_lock(&BTRFS_I(inode)->lock);
1898 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1899 state->start;
1900 spin_unlock(&BTRFS_I(inode)->lock);
1901 }
Chris Mason291d6732008-01-29 15:55:23 -05001902}
1903
Chris Masond352ac62008-09-29 15:18:18 -04001904/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001905 * Once a range is no longer delalloc this function ensures that proper
1906 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001907 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001908void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1909 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001910{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001911 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1912 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001913 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001914 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001915
Filipe Manana4a4b9642017-07-27 19:52:55 +01001916 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1917 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001918 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001919 spin_unlock(&inode->lock);
1920 }
Wang Shilong47059d92014-07-03 18:22:07 +08001921
Chris Mason75eff682008-12-15 15:54:40 -05001922 /*
1923 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001924 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001925 * bit, which is only set or cleared with irqs on
1926 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001927 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001928 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001929 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001930
Josef Bacik8b62f872017-10-19 14:15:55 -04001931 spin_lock(&inode->lock);
1932 btrfs_mod_outstanding_extents(inode, -num_extents);
1933 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001934
Josef Bacikb6d08f02013-09-27 14:57:43 -04001935 /*
1936 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001937 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001938 * error.
1939 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001940 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001941 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001942 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001943
Josef Bacik6a3891c2015-03-16 17:38:52 -04001944 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001945 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001946 return;
1947
Filipe Mananaa315e682017-03-06 23:04:20 +00001948 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1949 do_list && !(state->state & EXTENT_NORESERVE) &&
1950 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001951 btrfs_free_reserved_data_space_noquota(
1952 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001953 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001954
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001955 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1956 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001957 spin_lock(&inode->lock);
1958 inode->delalloc_bytes -= len;
1959 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001960 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001961 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001962 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001963 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001964 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001965
1966 if ((state->state & EXTENT_DELALLOC_NEW) &&
1967 (*bits & EXTENT_DELALLOC_NEW)) {
1968 spin_lock(&inode->lock);
1969 ASSERT(inode->new_delalloc_bytes >= len);
1970 inode->new_delalloc_bytes -= len;
1971 spin_unlock(&inode->lock);
1972 }
Chris Mason291d6732008-01-29 15:55:23 -05001973}
1974
Chris Masond352ac62008-09-29 15:18:18 -04001975/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001976 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
1977 * in a chunk's stripe. This function ensures that bios do not span a
1978 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07001979 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001980 * @page - The page we are about to add to the bio
1981 * @size - size we want to add to the bio
1982 * @bio - bio we want to ensure is smaller than a stripe
1983 * @bio_flags - flags of the bio
1984 *
1985 * return 1 if page cannot be added to the bio
1986 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07001987 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001988 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001989int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
1990 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001991{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001992 struct inode *inode = page->mapping->host;
1993 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001994 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001995 u64 length = 0;
1996 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001997 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03001998 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04001999
Chris Mason771ed682008-11-06 22:02:51 -05002000 if (bio_flags & EXTENT_BIO_COMPRESSED)
2001 return 0;
2002
Kent Overstreet4f024f32013-10-11 15:44:27 -07002003 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002004 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002005 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2006 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002007 if (ret < 0)
2008 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002009
2010 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002011 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002012 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002013}
2014
Chris Masond352ac62008-09-29 15:18:18 -04002015/*
2016 * in order to insert checksums into the metadata in large chunks,
2017 * we wait until bio submission time. All the pages in the bio are
2018 * checksummed and sums are attached onto the ordered extent record.
2019 *
2020 * At IO completion time the cums attached on the ordered extent record
2021 * are inserted into the btree
2022 */
David Sterbad0ee3932018-03-08 14:35:48 +01002023static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002024 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002025{
Josef Bacikc6100a42017-05-05 11:57:13 -04002026 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002027 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002028
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002029 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002030 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002031 return 0;
2032}
Chris Masone0156402008-04-16 11:15:20 -04002033
Chris Mason4a69a412008-11-06 22:03:00 -05002034/*
Chris Masoncad321a2008-12-17 14:51:42 -05002035 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002036 * on write, or reading the csums from the tree before a read.
2037 *
2038 * Rules about async/sync submit,
2039 * a) read: sync submit
2040 *
2041 * b) write without checksum: sync submit
2042 *
2043 * c) write with checksum:
2044 * c-1) if bio is issued by fsync: sync submit
2045 * (sync_writers != 0)
2046 *
2047 * c-2) if root is reloc root: sync submit
2048 * (only in case of buffered IO)
2049 *
2050 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002051 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002052static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002053 int mirror_num,
2054 unsigned long bio_flags)
2055
Chris Mason44b8bd72008-04-16 11:14:51 -04002056{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002057 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002058 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302059 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002060 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002061 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002062 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002063
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002064 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002065
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002066 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302067 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002068
Mike Christie37226b22016-06-05 14:31:52 -05002069 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002070 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002071 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002072 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002073
Chris Masond20f7042008-12-08 16:58:54 -05002074 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002075 ret = btrfs_submit_compressed_read(inode, bio,
2076 mirror_num,
2077 bio_flags);
2078 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002079 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002080 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002081 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002082 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002083 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002084 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002085 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002086 /* csum items have already been cloned */
2087 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2088 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002089 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002090 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002091 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002092 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002093 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002094 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002095 if (ret)
2096 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002097 }
2098
Chris Mason0b86a832008-03-24 15:01:56 -04002099mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002100 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002101
2102out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002103 if (ret) {
2104 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002105 bio_endio(bio);
2106 }
Stefan Behrens61891922012-11-05 18:51:52 +01002107 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002108}
Chris Mason6885f302008-02-20 16:11:05 -05002109
Chris Masond352ac62008-09-29 15:18:18 -04002110/*
2111 * given a list of ordered sums record them in the inode. This happens
2112 * at IO completion time based on sums calculated at bio submission time.
2113 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002114static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002115 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002116{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002117 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002118 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002119
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002120 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002121 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002122 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002123 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002124 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002125 if (ret)
2126 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002127 }
2128 return 0;
2129}
2130
Josef Bacik2ac55d42010-02-03 19:33:23 +00002131int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002132 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002133 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002134{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002135 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002136 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002137 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002138}
2139
Chris Masond352ac62008-09-29 15:18:18 -04002140/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002141struct btrfs_writepage_fixup {
2142 struct page *page;
2143 struct btrfs_work work;
2144};
2145
Christoph Hellwigb2950862008-12-02 09:54:17 -05002146static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002147{
2148 struct btrfs_writepage_fixup *fixup;
2149 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002150 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002151 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002152 struct page *page;
2153 struct inode *inode;
2154 u64 page_start;
2155 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002156 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002157
2158 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2159 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002160again:
Chris Mason247e7432008-07-17 12:53:51 -04002161 lock_page(page);
2162 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2163 ClearPageChecked(page);
2164 goto out_page;
2165 }
2166
2167 inode = page->mapping->host;
2168 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002169 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002170
David Sterbaff13db42015-12-03 14:30:40 +01002171 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002172 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002173
2174 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002175 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002176 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002177
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002178 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002179 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002180 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002181 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002182 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002183 unlock_page(page);
2184 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002185 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002186 goto again;
2187 }
Chris Mason247e7432008-07-17 12:53:51 -04002188
Qu Wenruo364ecf32017-02-27 15:10:38 +08002189 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002190 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002191 if (ret) {
2192 mapping_set_error(page->mapping, ret);
2193 end_extent_writepage(page, ret, page_start, page_end);
2194 ClearPageChecked(page);
2195 goto out;
2196 }
2197
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002198 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002199 &cached_state);
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002200 if (ret) {
2201 mapping_set_error(page->mapping, ret);
2202 end_extent_writepage(page, ret, page_start, page_end);
2203 ClearPageChecked(page);
2204 goto out;
2205 }
2206
Chris Mason247e7432008-07-17 12:53:51 -04002207 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002208 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002209 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002210out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002211 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002212 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002213out_page:
2214 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002215 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002216 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002217 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002218}
2219
2220/*
2221 * There are a few paths in the higher layers of the kernel that directly
2222 * set the page dirty bit without asking the filesystem if it is a
2223 * good idea. This causes problems because we want to make sure COW
2224 * properly happens and the data=ordered rules are followed.
2225 *
Chris Masonc8b97812008-10-29 14:49:59 -04002226 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002227 * hasn't been properly setup for IO. We kick off an async process
2228 * to fix it up. The async helper will wait for ordered extents, set
2229 * the delalloc bit and make it safe to write the page.
2230 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002231int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002232{
2233 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002234 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002235 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002236
Chris Mason8b62b722009-09-02 16:53:46 -04002237 /* this page is properly in the ordered list */
2238 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002239 return 0;
2240
2241 if (PageChecked(page))
2242 return -EAGAIN;
2243
2244 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2245 if (!fixup)
2246 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002247
Chris Mason247e7432008-07-17 12:53:51 -04002248 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002249 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002250 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2251 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002252 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002253 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002254 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002255}
2256
Yan Zhengd899e052008-10-30 14:25:28 -04002257static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2258 struct inode *inode, u64 file_pos,
2259 u64 disk_bytenr, u64 disk_num_bytes,
2260 u64 num_bytes, u64 ram_bytes,
2261 u8 compression, u8 encryption,
2262 u16 other_encoding, int extent_type)
2263{
2264 struct btrfs_root *root = BTRFS_I(inode)->root;
2265 struct btrfs_file_extent_item *fi;
2266 struct btrfs_path *path;
2267 struct extent_buffer *leaf;
2268 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002269 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002270 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002271 int ret;
2272
2273 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002274 if (!path)
2275 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002276
Chris Masona1ed8352009-09-11 12:27:37 -04002277 /*
2278 * we may be replacing one extent in the tree with another.
2279 * The new extent is pinned in the extent map, and we don't want
2280 * to drop it from the cache until it is completely in the btree.
2281 *
2282 * So, tell btrfs_drop_extents to leave this extent in the cache.
2283 * the caller is expected to unpin it and allow it to be merged
2284 * with the others.
2285 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002286 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2287 file_pos + num_bytes, NULL, 0,
2288 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002289 if (ret)
2290 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002291
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002292 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002293 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002294 ins.offset = file_pos;
2295 ins.type = BTRFS_EXTENT_DATA_KEY;
2296
2297 path->leave_spinning = 1;
2298 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2299 sizeof(*fi));
2300 if (ret)
2301 goto out;
2302 }
Yan Zhengd899e052008-10-30 14:25:28 -04002303 leaf = path->nodes[0];
2304 fi = btrfs_item_ptr(leaf, path->slots[0],
2305 struct btrfs_file_extent_item);
2306 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2307 btrfs_set_file_extent_type(leaf, fi, extent_type);
2308 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2309 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2310 btrfs_set_file_extent_offset(leaf, fi, 0);
2311 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2312 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2313 btrfs_set_file_extent_compression(leaf, fi, compression);
2314 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2315 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002316
Yan Zhengd899e052008-10-30 14:25:28 -04002317 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002318 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002319
2320 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002321
2322 ins.objectid = disk_bytenr;
2323 ins.offset = disk_num_bytes;
2324 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002325
Qu Wenruo297d7502015-09-08 17:08:37 +08002326 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002327 * Release the reserved range from inode dirty range map, as it is
2328 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002329 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002330 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2331 if (ret < 0)
2332 goto out;
2333 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002334 ret = btrfs_alloc_reserved_file_extent(trans, root,
2335 btrfs_ino(BTRFS_I(inode)),
2336 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002337out:
Yan Zhengd899e052008-10-30 14:25:28 -04002338 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002339
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002340 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002341}
2342
Liu Bo38c227d2013-01-29 03:18:40 +00002343/* snapshot-aware defrag */
2344struct sa_defrag_extent_backref {
2345 struct rb_node node;
2346 struct old_sa_defrag_extent *old;
2347 u64 root_id;
2348 u64 inum;
2349 u64 file_pos;
2350 u64 extent_offset;
2351 u64 num_bytes;
2352 u64 generation;
2353};
2354
2355struct old_sa_defrag_extent {
2356 struct list_head list;
2357 struct new_sa_defrag_extent *new;
2358
2359 u64 extent_offset;
2360 u64 bytenr;
2361 u64 offset;
2362 u64 len;
2363 int count;
2364};
2365
2366struct new_sa_defrag_extent {
2367 struct rb_root root;
2368 struct list_head head;
2369 struct btrfs_path *path;
2370 struct inode *inode;
2371 u64 file_pos;
2372 u64 len;
2373 u64 bytenr;
2374 u64 disk_len;
2375 u8 compress_type;
2376};
2377
2378static int backref_comp(struct sa_defrag_extent_backref *b1,
2379 struct sa_defrag_extent_backref *b2)
2380{
2381 if (b1->root_id < b2->root_id)
2382 return -1;
2383 else if (b1->root_id > b2->root_id)
2384 return 1;
2385
2386 if (b1->inum < b2->inum)
2387 return -1;
2388 else if (b1->inum > b2->inum)
2389 return 1;
2390
2391 if (b1->file_pos < b2->file_pos)
2392 return -1;
2393 else if (b1->file_pos > b2->file_pos)
2394 return 1;
2395
2396 /*
2397 * [------------------------------] ===> (a range of space)
2398 * |<--->| |<---->| =============> (fs/file tree A)
2399 * |<---------------------------->| ===> (fs/file tree B)
2400 *
2401 * A range of space can refer to two file extents in one tree while
2402 * refer to only one file extent in another tree.
2403 *
2404 * So we may process a disk offset more than one time(two extents in A)
2405 * and locate at the same extent(one extent in B), then insert two same
2406 * backrefs(both refer to the extent in B).
2407 */
2408 return 0;
2409}
2410
2411static void backref_insert(struct rb_root *root,
2412 struct sa_defrag_extent_backref *backref)
2413{
2414 struct rb_node **p = &root->rb_node;
2415 struct rb_node *parent = NULL;
2416 struct sa_defrag_extent_backref *entry;
2417 int ret;
2418
2419 while (*p) {
2420 parent = *p;
2421 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2422
2423 ret = backref_comp(backref, entry);
2424 if (ret < 0)
2425 p = &(*p)->rb_left;
2426 else
2427 p = &(*p)->rb_right;
2428 }
2429
2430 rb_link_node(&backref->node, parent, p);
2431 rb_insert_color(&backref->node, root);
2432}
2433
2434/*
2435 * Note the backref might has changed, and in this case we just return 0.
2436 */
2437static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2438 void *ctx)
2439{
2440 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002441 struct old_sa_defrag_extent *old = ctx;
2442 struct new_sa_defrag_extent *new = old->new;
2443 struct btrfs_path *path = new->path;
2444 struct btrfs_key key;
2445 struct btrfs_root *root;
2446 struct sa_defrag_extent_backref *backref;
2447 struct extent_buffer *leaf;
2448 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002449 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002450 int slot;
2451 int ret;
2452 u64 extent_offset;
2453 u64 num_bytes;
2454
2455 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002456 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002457 return 0;
2458
2459 key.objectid = root_id;
2460 key.type = BTRFS_ROOT_ITEM_KEY;
2461 key.offset = (u64)-1;
2462
Liu Bo38c227d2013-01-29 03:18:40 +00002463 root = btrfs_read_fs_root_no_name(fs_info, &key);
2464 if (IS_ERR(root)) {
2465 if (PTR_ERR(root) == -ENOENT)
2466 return 0;
2467 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002468 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002469 inum, offset, root_id);
2470 return PTR_ERR(root);
2471 }
2472
2473 key.objectid = inum;
2474 key.type = BTRFS_EXTENT_DATA_KEY;
2475 if (offset > (u64)-1 << 32)
2476 key.offset = 0;
2477 else
2478 key.offset = offset;
2479
2480 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302481 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002482 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002483 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002484
2485 while (1) {
2486 cond_resched();
2487
2488 leaf = path->nodes[0];
2489 slot = path->slots[0];
2490
2491 if (slot >= btrfs_header_nritems(leaf)) {
2492 ret = btrfs_next_leaf(root, path);
2493 if (ret < 0) {
2494 goto out;
2495 } else if (ret > 0) {
2496 ret = 0;
2497 goto out;
2498 }
2499 continue;
2500 }
2501
2502 path->slots[0]++;
2503
2504 btrfs_item_key_to_cpu(leaf, &key, slot);
2505
2506 if (key.objectid > inum)
2507 goto out;
2508
2509 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2510 continue;
2511
2512 extent = btrfs_item_ptr(leaf, slot,
2513 struct btrfs_file_extent_item);
2514
2515 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2516 continue;
2517
Liu Boe68afa42013-07-01 22:13:26 +08002518 /*
2519 * 'offset' refers to the exact key.offset,
2520 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2521 * (key.offset - extent_offset).
2522 */
2523 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002524 continue;
2525
Liu Boe68afa42013-07-01 22:13:26 +08002526 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002527 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002528
Liu Bo38c227d2013-01-29 03:18:40 +00002529 if (extent_offset >= old->extent_offset + old->offset +
2530 old->len || extent_offset + num_bytes <=
2531 old->extent_offset + old->offset)
2532 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002533 break;
2534 }
2535
2536 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2537 if (!backref) {
2538 ret = -ENOENT;
2539 goto out;
2540 }
2541
2542 backref->root_id = root_id;
2543 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002544 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002545 backref->num_bytes = num_bytes;
2546 backref->extent_offset = extent_offset;
2547 backref->generation = btrfs_file_extent_generation(leaf, extent);
2548 backref->old = old;
2549 backref_insert(&new->root, backref);
2550 old->count++;
2551out:
2552 btrfs_release_path(path);
2553 WARN_ON(ret);
2554 return ret;
2555}
2556
2557static noinline bool record_extent_backrefs(struct btrfs_path *path,
2558 struct new_sa_defrag_extent *new)
2559{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002560 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002561 struct old_sa_defrag_extent *old, *tmp;
2562 int ret;
2563
2564 new->path = path;
2565
2566 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002567 ret = iterate_inodes_from_logical(old->bytenr +
2568 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002569 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002570 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002571 if (ret < 0 && ret != -ENOENT)
2572 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002573
2574 /* no backref to be processed for this extent */
2575 if (!old->count) {
2576 list_del(&old->list);
2577 kfree(old);
2578 }
2579 }
2580
2581 if (list_empty(&new->head))
2582 return false;
2583
2584 return true;
2585}
2586
2587static int relink_is_mergable(struct extent_buffer *leaf,
2588 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002589 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002590{
Liu Bo116e0022013-08-02 16:30:40 +08002591 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002592 return 0;
2593
2594 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2595 return 0;
2596
Liu Bo116e0022013-08-02 16:30:40 +08002597 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2598 return 0;
2599
2600 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002601 btrfs_file_extent_other_encoding(leaf, fi))
2602 return 0;
2603
2604 return 1;
2605}
2606
2607/*
2608 * Note the backref might has changed, and in this case we just return 0.
2609 */
2610static noinline int relink_extent_backref(struct btrfs_path *path,
2611 struct sa_defrag_extent_backref *prev,
2612 struct sa_defrag_extent_backref *backref)
2613{
2614 struct btrfs_file_extent_item *extent;
2615 struct btrfs_file_extent_item *item;
2616 struct btrfs_ordered_extent *ordered;
2617 struct btrfs_trans_handle *trans;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002618 struct btrfs_ref ref = { 0 };
Liu Bo38c227d2013-01-29 03:18:40 +00002619 struct btrfs_root *root;
2620 struct btrfs_key key;
2621 struct extent_buffer *leaf;
2622 struct old_sa_defrag_extent *old = backref->old;
2623 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002624 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002625 struct inode *inode;
2626 struct extent_state *cached = NULL;
2627 int ret = 0;
2628 u64 start;
2629 u64 len;
2630 u64 lock_start;
2631 u64 lock_end;
2632 bool merge = false;
2633 int index;
2634
2635 if (prev && prev->root_id == backref->root_id &&
2636 prev->inum == backref->inum &&
2637 prev->file_pos + prev->num_bytes == backref->file_pos)
2638 merge = true;
2639
2640 /* step 1: get root */
2641 key.objectid = backref->root_id;
2642 key.type = BTRFS_ROOT_ITEM_KEY;
2643 key.offset = (u64)-1;
2644
Liu Bo38c227d2013-01-29 03:18:40 +00002645 index = srcu_read_lock(&fs_info->subvol_srcu);
2646
2647 root = btrfs_read_fs_root_no_name(fs_info, &key);
2648 if (IS_ERR(root)) {
2649 srcu_read_unlock(&fs_info->subvol_srcu, index);
2650 if (PTR_ERR(root) == -ENOENT)
2651 return 0;
2652 return PTR_ERR(root);
2653 }
Liu Bo38c227d2013-01-29 03:18:40 +00002654
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002655 if (btrfs_root_readonly(root)) {
2656 srcu_read_unlock(&fs_info->subvol_srcu, index);
2657 return 0;
2658 }
2659
Liu Bo38c227d2013-01-29 03:18:40 +00002660 /* step 2: get inode */
2661 key.objectid = backref->inum;
2662 key.type = BTRFS_INODE_ITEM_KEY;
2663 key.offset = 0;
2664
2665 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2666 if (IS_ERR(inode)) {
2667 srcu_read_unlock(&fs_info->subvol_srcu, index);
2668 return 0;
2669 }
2670
2671 srcu_read_unlock(&fs_info->subvol_srcu, index);
2672
2673 /* step 3: relink backref */
2674 lock_start = backref->file_pos;
2675 lock_end = backref->file_pos + backref->num_bytes - 1;
2676 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002677 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002678
2679 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2680 if (ordered) {
2681 btrfs_put_ordered_extent(ordered);
2682 goto out_unlock;
2683 }
2684
2685 trans = btrfs_join_transaction(root);
2686 if (IS_ERR(trans)) {
2687 ret = PTR_ERR(trans);
2688 goto out_unlock;
2689 }
2690
2691 key.objectid = backref->inum;
2692 key.type = BTRFS_EXTENT_DATA_KEY;
2693 key.offset = backref->file_pos;
2694
2695 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2696 if (ret < 0) {
2697 goto out_free_path;
2698 } else if (ret > 0) {
2699 ret = 0;
2700 goto out_free_path;
2701 }
2702
2703 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2704 struct btrfs_file_extent_item);
2705
2706 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2707 backref->generation)
2708 goto out_free_path;
2709
2710 btrfs_release_path(path);
2711
2712 start = backref->file_pos;
2713 if (backref->extent_offset < old->extent_offset + old->offset)
2714 start += old->extent_offset + old->offset -
2715 backref->extent_offset;
2716
2717 len = min(backref->extent_offset + backref->num_bytes,
2718 old->extent_offset + old->offset + old->len);
2719 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2720
2721 ret = btrfs_drop_extents(trans, root, inode, start,
2722 start + len, 1);
2723 if (ret)
2724 goto out_free_path;
2725again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002726 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002727 key.type = BTRFS_EXTENT_DATA_KEY;
2728 key.offset = start;
2729
Liu Boa09a0a72013-03-11 09:20:58 +00002730 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002731 if (merge) {
2732 struct btrfs_file_extent_item *fi;
2733 u64 extent_len;
2734 struct btrfs_key found_key;
2735
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002736 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002737 if (ret < 0)
2738 goto out_free_path;
2739
2740 path->slots[0]--;
2741 leaf = path->nodes[0];
2742 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2743
2744 fi = btrfs_item_ptr(leaf, path->slots[0],
2745 struct btrfs_file_extent_item);
2746 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2747
Liu Bo116e0022013-08-02 16:30:40 +08002748 if (extent_len + found_key.offset == start &&
2749 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002750 btrfs_set_file_extent_num_bytes(leaf, fi,
2751 extent_len + len);
2752 btrfs_mark_buffer_dirty(leaf);
2753 inode_add_bytes(inode, len);
2754
2755 ret = 1;
2756 goto out_free_path;
2757 } else {
2758 merge = false;
2759 btrfs_release_path(path);
2760 goto again;
2761 }
2762 }
2763
2764 ret = btrfs_insert_empty_item(trans, root, path, &key,
2765 sizeof(*extent));
2766 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002767 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002768 goto out_free_path;
2769 }
2770
2771 leaf = path->nodes[0];
2772 item = btrfs_item_ptr(leaf, path->slots[0],
2773 struct btrfs_file_extent_item);
2774 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2775 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2776 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2777 btrfs_set_file_extent_num_bytes(leaf, item, len);
2778 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2779 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2780 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2781 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2782 btrfs_set_file_extent_encryption(leaf, item, 0);
2783 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2784
2785 btrfs_mark_buffer_dirty(leaf);
2786 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002787 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002788
Qu Wenruo82fa1132019-04-04 14:45:35 +08002789 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr,
2790 new->disk_len, 0);
2791 btrfs_init_data_ref(&ref, backref->root_id, backref->inum,
2792 new->file_pos); /* start - extent_offset */
2793 ret = btrfs_inc_extent_ref(trans, &ref);
Liu Bo38c227d2013-01-29 03:18:40 +00002794 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002795 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002796 goto out_free_path;
2797 }
2798
2799 ret = 1;
2800out_free_path:
2801 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002802 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002803 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002804out_unlock:
2805 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002806 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002807 iput(inode);
2808 return ret;
2809}
2810
Liu Bo6f519562013-10-29 10:45:05 +08002811static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2812{
2813 struct old_sa_defrag_extent *old, *tmp;
2814
2815 if (!new)
2816 return;
2817
2818 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002819 kfree(old);
2820 }
2821 kfree(new);
2822}
2823
Liu Bo38c227d2013-01-29 03:18:40 +00002824static void relink_file_extents(struct new_sa_defrag_extent *new)
2825{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002826 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002827 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002828 struct sa_defrag_extent_backref *backref;
2829 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002830 struct rb_node *node;
2831 int ret;
2832
Liu Bo38c227d2013-01-29 03:18:40 +00002833 path = btrfs_alloc_path();
2834 if (!path)
2835 return;
2836
2837 if (!record_extent_backrefs(path, new)) {
2838 btrfs_free_path(path);
2839 goto out;
2840 }
2841 btrfs_release_path(path);
2842
2843 while (1) {
2844 node = rb_first(&new->root);
2845 if (!node)
2846 break;
2847 rb_erase(node, &new->root);
2848
2849 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2850
2851 ret = relink_extent_backref(path, prev, backref);
2852 WARN_ON(ret < 0);
2853
2854 kfree(prev);
2855
2856 if (ret == 1)
2857 prev = backref;
2858 else
2859 prev = NULL;
2860 cond_resched();
2861 }
2862 kfree(prev);
2863
2864 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002865out:
Liu Bo6f519562013-10-29 10:45:05 +08002866 free_sa_defrag_extent(new);
2867
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002868 atomic_dec(&fs_info->defrag_running);
2869 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002870}
2871
2872static struct new_sa_defrag_extent *
2873record_old_file_extents(struct inode *inode,
2874 struct btrfs_ordered_extent *ordered)
2875{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002876 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002877 struct btrfs_root *root = BTRFS_I(inode)->root;
2878 struct btrfs_path *path;
2879 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002880 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002881 struct new_sa_defrag_extent *new;
2882 int ret;
2883
2884 new = kmalloc(sizeof(*new), GFP_NOFS);
2885 if (!new)
2886 return NULL;
2887
2888 new->inode = inode;
2889 new->file_pos = ordered->file_offset;
2890 new->len = ordered->len;
2891 new->bytenr = ordered->start;
2892 new->disk_len = ordered->disk_len;
2893 new->compress_type = ordered->compress_type;
2894 new->root = RB_ROOT;
2895 INIT_LIST_HEAD(&new->head);
2896
2897 path = btrfs_alloc_path();
2898 if (!path)
2899 goto out_kfree;
2900
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002901 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002902 key.type = BTRFS_EXTENT_DATA_KEY;
2903 key.offset = new->file_pos;
2904
2905 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2906 if (ret < 0)
2907 goto out_free_path;
2908 if (ret > 0 && path->slots[0] > 0)
2909 path->slots[0]--;
2910
2911 /* find out all the old extents for the file range */
2912 while (1) {
2913 struct btrfs_file_extent_item *extent;
2914 struct extent_buffer *l;
2915 int slot;
2916 u64 num_bytes;
2917 u64 offset;
2918 u64 end;
2919 u64 disk_bytenr;
2920 u64 extent_offset;
2921
2922 l = path->nodes[0];
2923 slot = path->slots[0];
2924
2925 if (slot >= btrfs_header_nritems(l)) {
2926 ret = btrfs_next_leaf(root, path);
2927 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002928 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002929 else if (ret > 0)
2930 break;
2931 continue;
2932 }
2933
2934 btrfs_item_key_to_cpu(l, &key, slot);
2935
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002936 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002937 break;
2938 if (key.type != BTRFS_EXTENT_DATA_KEY)
2939 break;
2940 if (key.offset >= new->file_pos + new->len)
2941 break;
2942
2943 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2944
2945 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2946 if (key.offset + num_bytes < new->file_pos)
2947 goto next;
2948
2949 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2950 if (!disk_bytenr)
2951 goto next;
2952
2953 extent_offset = btrfs_file_extent_offset(l, extent);
2954
2955 old = kmalloc(sizeof(*old), GFP_NOFS);
2956 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002957 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002958
2959 offset = max(new->file_pos, key.offset);
2960 end = min(new->file_pos + new->len, key.offset + num_bytes);
2961
2962 old->bytenr = disk_bytenr;
2963 old->extent_offset = extent_offset;
2964 old->offset = offset - key.offset;
2965 old->len = end - offset;
2966 old->new = new;
2967 old->count = 0;
2968 list_add_tail(&old->list, &new->head);
2969next:
2970 path->slots[0]++;
2971 cond_resched();
2972 }
2973
2974 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002975 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002976
2977 return new;
2978
Liu Bo38c227d2013-01-29 03:18:40 +00002979out_free_path:
2980 btrfs_free_path(path);
2981out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002982 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002983 return NULL;
2984}
2985
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002986static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002987 u64 start, u64 len)
2988{
2989 struct btrfs_block_group_cache *cache;
2990
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002991 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002992 ASSERT(cache);
2993
2994 spin_lock(&cache->lock);
2995 cache->delalloc_bytes -= len;
2996 spin_unlock(&cache->lock);
2997
2998 btrfs_put_block_group(cache);
2999}
3000
Chris Masond352ac62008-09-29 15:18:18 -04003001/* as ordered data IO finishes, this gets called so we can finish
3002 * an ordered extent if the range of bytes in the file it covers are
3003 * fully written.
3004 */
Josef Bacik5fd02042012-05-02 14:00:54 -04003005static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003006{
Josef Bacik5fd02042012-05-02 14:00:54 -04003007 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003008 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003009 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003010 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003011 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003012 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00003013 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08003014 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003015 int ret = 0;
3016 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08003017 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04003018 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003019 bool range_locked = false;
3020 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04003021 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003022
3023 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3024 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3025 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
3026 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003027
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003028 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04003029
Josef Bacik5fd02042012-05-02 14:00:54 -04003030 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3031 ret = -EIO;
3032 goto out;
3033 }
3034
Nikolay Borisov7ab79562017-02-20 13:50:57 +02003035 btrfs_free_io_failure_record(BTRFS_I(inode),
3036 ordered_extent->file_offset,
3037 ordered_extent->file_offset +
3038 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08003039
Josef Bacik77cef2e2013-08-29 13:57:21 -04003040 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3041 truncated = true;
3042 logical_len = ordered_extent->truncated_len;
3043 /* Truncated the entire extent, don't bother adding */
3044 if (!logical_len)
3045 goto out;
3046 }
3047
Yan, Zhengc2167752009-11-12 09:34:21 +00003048 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08003050
3051 /*
3052 * For mwrite(mmap + memset to write) case, we still reserve
3053 * space for NOCOW range.
3054 * As NOCOW won't cause a new delayed ref, just free the space
3055 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003056 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003057 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003058 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3059 if (nolock)
3060 trans = btrfs_join_transaction_nolock(root);
3061 else
3062 trans = btrfs_join_transaction(root);
3063 if (IS_ERR(trans)) {
3064 ret = PTR_ERR(trans);
3065 trans = NULL;
3066 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003067 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003068 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003069 ret = btrfs_update_inode_fallback(trans, root, inode);
3070 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003071 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003072 goto out;
3073 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003074
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003075 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003076 lock_extent_bits(io_tree, ordered_extent->file_offset,
3077 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003078 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003079
Liu Bo38c227d2013-01-29 03:18:40 +00003080 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3081 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003082 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003083 if (ret) {
3084 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003085 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003086 /* the inode is shared */
3087 new = record_old_file_extents(inode, ordered_extent);
3088
3089 clear_extent_bit(io_tree, ordered_extent->file_offset,
3090 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003091 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003092 }
3093
Josef Bacik0cb59c92010-07-02 12:14:14 -04003094 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003095 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003096 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003097 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003098 if (IS_ERR(trans)) {
3099 ret = PTR_ERR(trans);
3100 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003101 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003102 }
Chris Masona79b7d42014-05-22 16:18:52 -07003103
Josef Bacik69fe2d72017-10-19 14:15:57 -04003104 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003105
Chris Masonc8b97812008-10-29 14:49:59 -04003106 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003107 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003108 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003109 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003110 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3111 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003112 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003113 ordered_extent->file_offset,
3114 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003115 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003116 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003117 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003118 ret = insert_reserved_file_extent(trans, inode,
3119 ordered_extent->file_offset,
3120 ordered_extent->start,
3121 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003122 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003123 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003124 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003125 if (!ret) {
3126 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003127 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003128 ordered_extent->start,
3129 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003130 }
Yan Zhengd899e052008-10-30 14:25:28 -04003131 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003132 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3133 ordered_extent->file_offset, ordered_extent->len,
3134 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003135 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003136 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003137 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003138 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003139
Nikolay Borisovac01f262018-01-08 10:59:43 +02003140 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3141 if (ret) {
3142 btrfs_abort_transaction(trans, ret);
3143 goto out;
3144 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003145
Josef Bacik6c760c02012-11-09 10:53:21 -05003146 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3147 ret = btrfs_update_inode_fallback(trans, root, inode);
3148 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003149 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003150 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003151 }
3152 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003153out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003154 if (range_locked || clear_new_delalloc_bytes) {
3155 unsigned int clear_bits = 0;
3156
3157 if (range_locked)
3158 clear_bits |= EXTENT_LOCKED;
3159 if (clear_new_delalloc_bytes)
3160 clear_bits |= EXTENT_DELALLOC_NEW;
3161 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3162 ordered_extent->file_offset,
3163 ordered_extent->file_offset +
3164 ordered_extent->len - 1,
3165 clear_bits,
3166 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003167 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003168 }
3169
Miao Xiea698d0752012-09-20 01:51:59 -06003170 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003171 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003172
Josef Bacik77cef2e2013-08-29 13:57:21 -04003173 if (ret || truncated) {
3174 u64 start, end;
3175
3176 if (truncated)
3177 start = ordered_extent->file_offset + logical_len;
3178 else
3179 start = ordered_extent->file_offset;
3180 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003181 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003182
3183 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003184 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003185
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003186 /*
3187 * If the ordered extent had an IOERR or something else went
3188 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003189 * back to the allocator. We only free the extent in the
3190 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003191 *
3192 * If we made it past insert_reserved_file_extent before we
3193 * errored out then we don't need to do this as the accounting
3194 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003195 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003196 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003197 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003198 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003199 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003200 btrfs_free_reserved_extent(fs_info,
3201 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003202 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003203 }
3204
3205
Josef Bacik5fd02042012-05-02 14:00:54 -04003206 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003207 * This needs to be done to make sure anybody waiting knows we are done
3208 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003209 */
3210 btrfs_remove_ordered_extent(inode, ordered_extent);
3211
Liu Bo38c227d2013-01-29 03:18:40 +00003212 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003213 if (new) {
3214 if (ret) {
3215 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003216 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003217 } else {
3218 relink_file_extents(new);
3219 }
3220 }
Liu Bo38c227d2013-01-29 03:18:40 +00003221
Chris Masone6dcd2d2008-07-17 12:53:50 -04003222 /* once for us */
3223 btrfs_put_ordered_extent(ordered_extent);
3224 /* once for the tree */
3225 btrfs_put_ordered_extent(ordered_extent);
3226
Josef Bacik5fd02042012-05-02 14:00:54 -04003227 return ret;
3228}
3229
3230static void finish_ordered_fn(struct btrfs_work *work)
3231{
3232 struct btrfs_ordered_extent *ordered_extent;
3233 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3234 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003235}
3236
Nikolay Borisovc6297322018-11-08 10:18:08 +02003237void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3238 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003239{
Josef Bacik5fd02042012-05-02 14:00:54 -04003240 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003241 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003242 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003243 struct btrfs_workqueue *wq;
3244 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003245
liubo1abe9b82011-03-24 11:18:59 +00003246 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3247
Chris Mason8b62b722009-09-02 16:53:46 -04003248 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003249 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3250 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003251 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003252
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003253 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003254 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003255 func = btrfs_freespace_write_helper;
3256 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003257 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003258 func = btrfs_endio_write_helper;
3259 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003260
Liu Bo9e0af232014-08-15 23:36:53 +08003261 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3262 NULL);
3263 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003264}
3265
Miao Xiedc380ae2014-09-12 18:43:55 +08003266static int __readpage_endio_check(struct inode *inode,
3267 struct btrfs_io_bio *io_bio,
3268 int icsum, struct page *page,
3269 int pgoff, u64 start, size_t len)
3270{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003271 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3272 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003273 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003274 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
3275 u8 *csum_expected;
3276 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003277
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003278 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003279
3280 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003281 shash->tfm = fs_info->csum_shash;
3282
3283 crypto_shash_init(shash);
3284 crypto_shash_update(shash, kaddr + pgoff, len);
3285 crypto_shash_final(shash, csum);
3286
3287 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003288 goto zeroit;
3289
3290 kunmap_atomic(kaddr);
3291 return 0;
3292zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02003293 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3294 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003295 memset(kaddr + pgoff, 1, len);
3296 flush_dcache_page(page);
3297 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003298 return -EIO;
3299}
3300
Chris Masond352ac62008-09-29 15:18:18 -04003301/*
Chris Masond352ac62008-09-29 15:18:18 -04003302 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003303 * if there's a match, we allow the bio to finish. If not, the code in
3304 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003305 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003306static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3307 u64 phy_offset, struct page *page,
3308 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003309{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003310 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003311 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003312 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003313 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003314
Chris Masond20f7042008-12-08 16:58:54 -05003315 if (PageChecked(page)) {
3316 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003317 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003318 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003319
3320 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003321 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003322
Yan Zheng17d217f2008-12-12 10:03:38 -05003323 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003324 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003325 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003326 return 0;
3327 }
3328
Miao Xiefacc8a222013-07-25 19:22:34 +08003329 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003330 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3331 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003332}
Chris Masonb888db22007-08-27 16:49:44 -04003333
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003334/*
3335 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3336 *
3337 * @inode: The inode we want to perform iput on
3338 *
3339 * This function uses the generic vfs_inode::i_count to track whether we should
3340 * just decrement it (in case it's > 1) or if this is the last iput then link
3341 * the inode to the delayed iput machinery. Delayed iputs are processed at
3342 * transaction commit time/superblock commit/cleaner kthread.
3343 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003344void btrfs_add_delayed_iput(struct inode *inode)
3345{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003346 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003347 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003348
3349 if (atomic_add_unless(&inode->i_count, -1, 1))
3350 return;
3351
Josef Bacik034f7842018-12-03 11:06:52 -05003352 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003353 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003354 ASSERT(list_empty(&binode->delayed_iput));
3355 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003356 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003357 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3358 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003359}
3360
Josef Bacik63611e72019-06-18 10:59:18 -04003361static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3362 struct btrfs_inode *inode)
3363{
3364 list_del_init(&inode->delayed_iput);
3365 spin_unlock(&fs_info->delayed_iput_lock);
3366 iput(&inode->vfs_inode);
3367 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3368 wake_up(&fs_info->delayed_iputs_wait);
3369 spin_lock(&fs_info->delayed_iput_lock);
3370}
3371
3372static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3373 struct btrfs_inode *inode)
3374{
3375 if (!list_empty(&inode->delayed_iput)) {
3376 spin_lock(&fs_info->delayed_iput_lock);
3377 if (!list_empty(&inode->delayed_iput))
3378 run_delayed_iput_locked(fs_info, inode);
3379 spin_unlock(&fs_info->delayed_iput_lock);
3380 }
3381}
3382
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003383void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003384{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003385
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003386 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003387 while (!list_empty(&fs_info->delayed_iputs)) {
3388 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003389
David Sterba8089fe62015-11-19 14:15:51 +01003390 inode = list_first_entry(&fs_info->delayed_iputs,
3391 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003392 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003393 }
David Sterba8089fe62015-11-19 14:15:51 +01003394 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003395}
3396
Josef Bacik034f7842018-12-03 11:06:52 -05003397/**
3398 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3399 * @fs_info - the fs_info for this fs
3400 * @return - EINTR if we were killed, 0 if nothing's pending
3401 *
3402 * This will wait on any delayed iputs that are currently running with KILLABLE
3403 * set. Once they are all done running we will return, unless we are killed in
3404 * which case we return EINTR. This helps in user operations like fallocate etc
3405 * that might get blocked on the iputs.
3406 */
3407int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3408{
3409 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3410 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3411 if (ret)
3412 return -EINTR;
3413 return 0;
3414}
3415
Yan, Zhengd68fc572010-05-16 10:49:58 -04003416/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003417 * This creates an orphan entry for the given inode in case something goes wrong
3418 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003419 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003420int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003421 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003422{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003423 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003424
Omar Sandoval27919062018-05-11 13:13:37 -07003425 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3426 if (ret && ret != -EEXIST) {
3427 btrfs_abort_transaction(trans, ret);
3428 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003429 }
3430
Yan, Zhengd68fc572010-05-16 10:49:58 -04003431 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003432}
3433
3434/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003435 * We have done the delete so we can go ahead and remove the orphan item for
3436 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003437 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003438static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003439 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003440{
Omar Sandoval27919062018-05-11 13:13:37 -07003441 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003442}
3443
3444/*
3445 * this cleans up any orphans that may be left on the list from the last use
3446 * of this root.
3447 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003448int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003449{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003450 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003451 struct btrfs_path *path;
3452 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003453 struct btrfs_key key, found_key;
3454 struct btrfs_trans_handle *trans;
3455 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003456 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003457 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003458
Yan, Zhengd68fc572010-05-16 10:49:58 -04003459 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003460 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003461
3462 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003463 if (!path) {
3464 ret = -ENOMEM;
3465 goto out;
3466 }
David Sterbae4058b52015-11-27 16:31:35 +01003467 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003468
3469 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003470 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003471 key.offset = (u64)-1;
3472
Josef Bacik7b128762008-07-24 12:17:14 -04003473 while (1) {
3474 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003475 if (ret < 0)
3476 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003477
3478 /*
3479 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003480 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003481 * find the key and see if we have stuff that matches
3482 */
3483 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003484 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003485 if (path->slots[0] == 0)
3486 break;
3487 path->slots[0]--;
3488 }
3489
3490 /* pull out the item */
3491 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003492 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3493
3494 /* make sure the item matches what we want */
3495 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3496 break;
David Sterba962a2982014-06-04 18:41:45 +02003497 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003498 break;
3499
3500 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003501 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003502
3503 /*
3504 * this is where we are basically btrfs_lookup, without the
3505 * crossing root thing. we store the inode number in the
3506 * offset of the orphan item.
3507 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003508
3509 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003510 btrfs_err(fs_info,
3511 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003512 ret = -EINVAL;
3513 goto out;
3514 }
3515
3516 last_objectid = found_key.offset;
3517
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003518 found_key.objectid = found_key.offset;
3519 found_key.type = BTRFS_INODE_ITEM_KEY;
3520 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003521 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303522 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003523 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003524 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003525
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003526 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003527 struct btrfs_root *dead_root;
3528 struct btrfs_fs_info *fs_info = root->fs_info;
3529 int is_dead_root = 0;
3530
3531 /*
3532 * this is an orphan in the tree root. Currently these
3533 * could come from 2 sources:
3534 * a) a snapshot deletion in progress
3535 * b) a free space cache inode
3536 * We need to distinguish those two, as the snapshot
3537 * orphan must not get deleted.
3538 * find_dead_roots already ran before us, so if this
3539 * is a snapshot deletion, we should find the root
3540 * in the dead_roots list
3541 */
3542 spin_lock(&fs_info->trans_lock);
3543 list_for_each_entry(dead_root, &fs_info->dead_roots,
3544 root_list) {
3545 if (dead_root->root_key.objectid ==
3546 found_key.objectid) {
3547 is_dead_root = 1;
3548 break;
3549 }
3550 }
3551 spin_unlock(&fs_info->trans_lock);
3552 if (is_dead_root) {
3553 /* prevent this orphan from being found again */
3554 key.offset = found_key.objectid - 1;
3555 continue;
3556 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003557
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003558 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003559
Josef Bacika8c9e572011-09-21 16:55:59 -04003560 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003561 * If we have an inode with links, there are a couple of
3562 * possibilities. Old kernels (before v3.12) used to create an
3563 * orphan item for truncate indicating that there were possibly
3564 * extent items past i_size that needed to be deleted. In v3.12,
3565 * truncate was changed to update i_size in sync with the extent
3566 * items, but the (useless) orphan item was still created. Since
3567 * v4.18, we don't create the orphan item for truncate at all.
3568 *
3569 * So, this item could mean that we need to do a truncate, but
3570 * only if this filesystem was last used on a pre-v3.12 kernel
3571 * and was not cleanly unmounted. The odds of that are quite
3572 * slim, and it's a pain to do the truncate now, so just delete
3573 * the orphan item.
3574 *
3575 * It's also possible that this orphan item was supposed to be
3576 * deleted but wasn't. The inode number may have been reused,
3577 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003578 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003579 if (ret == -ENOENT || inode->i_nlink) {
3580 if (!ret)
3581 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003582 trans = btrfs_start_transaction(root, 1);
3583 if (IS_ERR(trans)) {
3584 ret = PTR_ERR(trans);
3585 goto out;
3586 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003587 btrfs_debug(fs_info, "auto deleting %Lu",
3588 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003589 ret = btrfs_del_orphan_item(trans, root,
3590 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003591 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003592 if (ret)
3593 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003594 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003595 }
Josef Bacik7b128762008-07-24 12:17:14 -04003596
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003597 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003598
3599 /* this will do delete_inode and everything for us */
3600 iput(inode);
3601 }
Miao Xie3254c872011-11-10 20:45:05 -05003602 /* release the path since we're done with it */
3603 btrfs_release_path(path);
3604
Yan, Zhengd68fc572010-05-16 10:49:58 -04003605 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3606
Omar Sandovala575cee2018-05-11 13:13:38 -07003607 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003608 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003609 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003610 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003611 }
Josef Bacik7b128762008-07-24 12:17:14 -04003612
3613 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003614 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003615
3616out:
3617 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003618 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003619 btrfs_free_path(path);
3620 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003621}
3622
Chris Masond352ac62008-09-29 15:18:18 -04003623/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003624 * very simple check to peek ahead in the leaf looking for xattrs. If we
3625 * don't find any xattrs, we know there can't be any acls.
3626 *
3627 * slot is the slot the inode is in, objectid is the objectid of the inode
3628 */
3629static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003630 int slot, u64 objectid,
3631 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003632{
3633 u32 nritems = btrfs_header_nritems(leaf);
3634 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003635 static u64 xattr_access = 0;
3636 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003637 int scanned = 0;
3638
Josef Bacikf23b5a52013-06-19 10:16:26 -04003639 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003640 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3641 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3642 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3643 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003644 }
3645
Chris Mason46a53cc2009-04-27 11:47:50 -04003646 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003647 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003648 while (slot < nritems) {
3649 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3650
3651 /* we found a different objectid, there must not be acls */
3652 if (found_key.objectid != objectid)
3653 return 0;
3654
3655 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003656 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003657 if (*first_xattr_slot == -1)
3658 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003659 if (found_key.offset == xattr_access ||
3660 found_key.offset == xattr_default)
3661 return 1;
3662 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003663
3664 /*
3665 * we found a key greater than an xattr key, there can't
3666 * be any acls later on
3667 */
3668 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3669 return 0;
3670
3671 slot++;
3672 scanned++;
3673
3674 /*
3675 * it goes inode, inode backrefs, xattrs, extents,
3676 * so if there are a ton of hard links to an inode there can
3677 * be a lot of backrefs. Don't waste time searching too hard,
3678 * this is just an optimization
3679 */
3680 if (scanned >= 8)
3681 break;
3682 }
3683 /* we hit the end of the leaf before we found an xattr or
3684 * something larger than an xattr. We have to assume the inode
3685 * has acls
3686 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003687 if (*first_xattr_slot == -1)
3688 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003689 return 1;
3690}
3691
3692/*
Chris Masond352ac62008-09-29 15:18:18 -04003693 * read an inode from the btree into the in-memory inode
3694 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003695static int btrfs_read_locked_inode(struct inode *inode,
3696 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003697{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003698 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003699 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003700 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003701 struct btrfs_inode_item *inode_item;
3702 struct btrfs_root *root = BTRFS_I(inode)->root;
3703 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003704 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003705 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003706 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003707 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003708 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003709 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003710
3711 ret = btrfs_fill_inode(inode, &rdev);
3712 if (!ret)
3713 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003714
Filipe Manana4222ea72018-10-24 10:13:03 +01003715 if (!path) {
3716 path = btrfs_alloc_path();
3717 if (!path)
3718 return -ENOMEM;
3719 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003720
Chris Mason39279cc2007-06-12 06:35:45 -04003721 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003722
Chris Mason39279cc2007-06-12 06:35:45 -04003723 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003724 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003725 if (path != in_path)
3726 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003727 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003728 }
Chris Mason39279cc2007-06-12 06:35:45 -04003729
Chris Mason5f39d392007-10-15 16:14:19 -04003730 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003731
3732 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003733 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003734
Chris Mason5f39d392007-10-15 16:14:19 -04003735 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3736 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003737 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003738 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003739 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3740 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003741 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003742
David Sterbaa937b972014-12-12 17:39:12 +01003743 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3744 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003745
David Sterbaa937b972014-12-12 17:39:12 +01003746 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3747 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003748
David Sterbaa937b972014-12-12 17:39:12 +01003749 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3750 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003751
chandan r9cc97d62012-07-04 12:48:07 +05303752 BTRFS_I(inode)->i_otime.tv_sec =
3753 btrfs_timespec_sec(leaf, &inode_item->otime);
3754 BTRFS_I(inode)->i_otime.tv_nsec =
3755 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003756
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003757 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003758 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003759 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3760
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003761 inode_set_iversion_queried(inode,
3762 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003763 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003764 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003765 rdev = btrfs_inode_rdev(leaf, inode_item);
3766
Josef Bacikaec74772008-07-24 12:12:38 -04003767 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003768 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003769
3770cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003771 /*
3772 * If we were modified in the current generation and evicted from memory
3773 * and then re-read we need to do a full sync since we don't have any
3774 * idea about which extents were modified before we were evicted from
3775 * cache.
3776 *
3777 * This is required for both inode re-read from disk and delayed inode
3778 * in delayed_nodes_tree.
3779 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003780 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003781 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3782 &BTRFS_I(inode)->runtime_flags);
3783
Filipe Mananabde6c242015-07-24 00:00:19 +01003784 /*
3785 * We don't persist the id of the transaction where an unlink operation
3786 * against the inode was last made. So here we assume the inode might
3787 * have been evicted, and therefore the exact value of last_unlink_trans
3788 * lost, and set it to last_trans to avoid metadata inconsistencies
3789 * between the inode and its parent if the inode is fsync'ed and the log
3790 * replayed. For example, in the scenario:
3791 *
3792 * touch mydir/foo
3793 * ln mydir/foo mydir/bar
3794 * sync
3795 * unlink mydir/bar
3796 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3797 * xfs_io -c fsync mydir/foo
3798 * <power failure>
3799 * mount fs, triggers fsync log replay
3800 *
3801 * We must make sure that when we fsync our inode foo we also log its
3802 * parent inode, otherwise after log replay the parent still has the
3803 * dentry with the "bar" name but our inode foo has a link count of 1
3804 * and doesn't have an inode ref with the name "bar" anymore.
3805 *
3806 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003807 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003808 * transaction commits on fsync if our inode is a directory, or if our
3809 * inode is not a directory, logging its parent unnecessarily.
3810 */
3811 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3812
Miao Xie67de1172013-12-26 13:07:06 +08003813 path->slots[0]++;
3814 if (inode->i_nlink != 1 ||
3815 path->slots[0] >= btrfs_header_nritems(leaf))
3816 goto cache_acl;
3817
3818 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003819 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003820 goto cache_acl;
3821
3822 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3823 if (location.type == BTRFS_INODE_REF_KEY) {
3824 struct btrfs_inode_ref *ref;
3825
3826 ref = (struct btrfs_inode_ref *)ptr;
3827 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3828 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3829 struct btrfs_inode_extref *extref;
3830
3831 extref = (struct btrfs_inode_extref *)ptr;
3832 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3833 extref);
3834 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003835cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003836 /*
3837 * try to precache a NULL acl entry for files that don't have
3838 * any xattrs or acls
3839 */
Li Zefan33345d012011-04-20 10:31:50 +08003840 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003841 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003842 if (first_xattr_slot != -1) {
3843 path->slots[0] = first_xattr_slot;
3844 ret = btrfs_load_inode_props(inode, path);
3845 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003846 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003847 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003848 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003849 root->root_key.objectid, ret);
3850 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003851 if (path != in_path)
3852 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003853
Al Viro72c04902009-06-24 16:58:48 -04003854 if (!maybe_acls)
3855 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003856
Chris Mason39279cc2007-06-12 06:35:45 -04003857 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003858 case S_IFREG:
3859 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003860 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003861 inode->i_fop = &btrfs_file_operations;
3862 inode->i_op = &btrfs_file_inode_operations;
3863 break;
3864 case S_IFDIR:
3865 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003866 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003867 break;
3868 case S_IFLNK:
3869 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003870 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003871 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003872 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003873 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003874 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003875 init_special_inode(inode, inode->i_mode, rdev);
3876 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003877 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003878
David Sterba7b6a2212018-03-26 18:40:21 +02003879 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003880 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003881}
3882
Chris Masond352ac62008-09-29 15:18:18 -04003883/*
3884 * given a leaf and an inode, copy the inode fields into the leaf
3885 */
Chris Masone02119d2008-09-05 16:13:11 -04003886static void fill_inode_item(struct btrfs_trans_handle *trans,
3887 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003888 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003889 struct inode *inode)
3890{
Liu Bo51fab692012-12-27 09:01:21 +00003891 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003892
Liu Bo51fab692012-12-27 09:01:21 +00003893 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003894
Liu Bo51fab692012-12-27 09:01:21 +00003895 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3896 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3897 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3898 &token);
3899 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3900 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003901
David Sterbaa937b972014-12-12 17:39:12 +01003902 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003903 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003904 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003905 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003906
David Sterbaa937b972014-12-12 17:39:12 +01003907 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003908 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003909 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003910 inode->i_mtime.tv_nsec, &token);
3911
David Sterbaa937b972014-12-12 17:39:12 +01003912 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003913 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003914 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003915 inode->i_ctime.tv_nsec, &token);
3916
chandan r9cc97d62012-07-04 12:48:07 +05303917 btrfs_set_token_timespec_sec(leaf, &item->otime,
3918 BTRFS_I(inode)->i_otime.tv_sec, &token);
3919 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3920 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3921
Liu Bo51fab692012-12-27 09:01:21 +00003922 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3923 &token);
3924 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3925 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003926 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3927 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003928 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3929 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3930 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3931 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003932}
3933
Chris Masond352ac62008-09-29 15:18:18 -04003934/*
3935 * copy everything in the in-memory inode into the btree.
3936 */
Chris Mason21151332011-11-10 20:39:08 -05003937static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003938 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003939{
3940 struct btrfs_inode_item *inode_item;
3941 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003942 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003943 int ret;
3944
3945 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003946 if (!path)
3947 return -ENOMEM;
3948
Chris Masonb9473432009-03-13 11:00:37 -04003949 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003950 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3951 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003952 if (ret) {
3953 if (ret > 0)
3954 ret = -ENOENT;
3955 goto failed;
3956 }
3957
Chris Mason5f39d392007-10-15 16:14:19 -04003958 leaf = path->nodes[0];
3959 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003960 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003961
Chris Masone02119d2008-09-05 16:13:11 -04003962 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003963 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003964 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003965 ret = 0;
3966failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003967 btrfs_free_path(path);
3968 return ret;
3969}
3970
Chris Masond352ac62008-09-29 15:18:18 -04003971/*
Chris Mason21151332011-11-10 20:39:08 -05003972 * copy everything in the in-memory inode into the btree.
3973 */
3974noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3975 struct btrfs_root *root, struct inode *inode)
3976{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003977 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003978 int ret;
3979
3980 /*
3981 * If the inode is a free space inode, we can deadlock during commit
3982 * if we put it into the delayed code.
3983 *
3984 * The data relocation inode should also be directly updated
3985 * without delay
3986 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003987 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003988 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003989 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003990 btrfs_update_root_times(trans, root);
3991
Chris Mason21151332011-11-10 20:39:08 -05003992 ret = btrfs_delayed_update_inode(trans, root, inode);
3993 if (!ret)
3994 btrfs_set_inode_last_trans(trans, inode);
3995 return ret;
3996 }
3997
3998 return btrfs_update_inode_item(trans, root, inode);
3999}
4000
Josef Bacikbe6aef62012-10-22 15:43:12 -04004001noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4002 struct btrfs_root *root,
4003 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004004{
4005 int ret;
4006
4007 ret = btrfs_update_inode(trans, root, inode);
4008 if (ret == -ENOSPC)
4009 return btrfs_update_inode_item(trans, root, inode);
4010 return ret;
4011}
4012
4013/*
Chris Masond352ac62008-09-29 15:18:18 -04004014 * unlink helper that gets used here in inode.c and in the tree logging
4015 * recovery code. It remove a link in a directory with a given name, and
4016 * also drops the back refs in the inode to the directory
4017 */
Al Viro92986792011-03-04 17:14:37 +00004018static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4019 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004020 struct btrfs_inode *dir,
4021 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004022 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004023{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004024 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004025 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004026 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004027 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04004028 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004029 u64 ino = btrfs_ino(inode);
4030 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004031
4032 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004033 if (!path) {
4034 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004035 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004036 }
4037
Chris Masonb9473432009-03-13 11:00:37 -04004038 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004039 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004040 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08004041 if (IS_ERR_OR_NULL(di)) {
4042 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04004043 goto err;
4044 }
Chris Mason39279cc2007-06-12 06:35:45 -04004045 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004046 if (ret)
4047 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004048 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004049
Miao Xie67de1172013-12-26 13:07:06 +08004050 /*
4051 * If we don't have dir index, we have to get it by looking up
4052 * the inode ref, since we get the inode ref, remove it directly,
4053 * it is unnecessary to do delayed deletion.
4054 *
4055 * But if we have dir index, needn't search inode ref to get it.
4056 * Since the inode ref is close to the inode item, it is better
4057 * that we delay to delete it, and just do this deletion when
4058 * we update the inode item.
4059 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004060 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004061 ret = btrfs_delayed_delete_inode_ref(inode);
4062 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004063 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004064 goto skip_backref;
4065 }
4066 }
4067
Li Zefan33345d012011-04-20 10:31:50 +08004068 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4069 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004070 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004071 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004072 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004073 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004074 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004075 goto err;
4076 }
Miao Xie67de1172013-12-26 13:07:06 +08004077skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004078 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004079 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004080 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004081 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004082 }
Chris Mason39279cc2007-06-12 06:35:45 -04004083
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004084 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4085 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004086 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004087 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004088 goto err;
4089 }
Chris Masone02119d2008-09-05 16:13:11 -04004090
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004091 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4092 index);
Chris Mason6418c962010-10-30 07:34:24 -04004093 if (ret == -ENOENT)
4094 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004095 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004096 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04004097
4098 /*
4099 * If we have a pending delayed iput we could end up with the final iput
4100 * being run in btrfs-cleaner context. If we have enough of these built
4101 * up we can end up burning a lot of time in btrfs-cleaner without any
4102 * way to throttle the unlinks. Since we're currently holding a ref on
4103 * the inode we can run the delayed iput here without any issues as the
4104 * final iput won't be done until after we drop the ref we're currently
4105 * holding.
4106 */
4107 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004108err:
4109 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004110 if (ret)
4111 goto out;
4112
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004113 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004114 inode_inc_iversion(&inode->vfs_inode);
4115 inode_inc_iversion(&dir->vfs_inode);
4116 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4117 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4118 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004119out:
Chris Mason39279cc2007-06-12 06:35:45 -04004120 return ret;
4121}
4122
Al Viro92986792011-03-04 17:14:37 +00004123int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4124 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004125 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004126 const char *name, int name_len)
4127{
4128 int ret;
4129 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4130 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004131 drop_nlink(&inode->vfs_inode);
4132 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004133 }
4134 return ret;
4135}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004136
4137/*
4138 * helper to start transaction for unlink and rmdir.
4139 *
Josef Bacikd52be812013-05-29 14:54:47 -04004140 * unlink and rmdir are special in btrfs, they do not always free space, so
4141 * if we cannot make our reservations the normal way try and see if there is
4142 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4143 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004144 */
Josef Bacikd52be812013-05-29 14:54:47 -04004145static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004146{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004147 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004148
Josef Bacike70bea52011-10-11 14:18:24 -04004149 /*
4150 * 1 for the possible orphan item
4151 * 1 for the dir item
4152 * 1 for the dir index
4153 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004154 * 1 for the inode
4155 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004156 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004157}
4158
Chris Mason39279cc2007-06-12 06:35:45 -04004159static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4160{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004161 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004162 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004163 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004164 int ret;
4165
Josef Bacikd52be812013-05-29 14:54:47 -04004166 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004167 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004168 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004169
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004170 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4171 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004172
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004173 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4174 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4175 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004176 if (ret)
4177 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004178
Yan, Zhenga22285a2010-05-16 10:48:46 -04004179 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004180 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004181 if (ret)
4182 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004183 }
Josef Bacik7b128762008-07-24 12:17:14 -04004184
Tsutomu Itohb5324022011-07-19 07:27:20 +00004185out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004186 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004187 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004188 return ret;
4189}
4190
Misono Tomohirof60a2362018-04-18 11:34:52 +09004191static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004192 struct inode *dir, u64 objectid,
4193 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004195 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004196 struct btrfs_path *path;
4197 struct extent_buffer *leaf;
4198 struct btrfs_dir_item *di;
4199 struct btrfs_key key;
4200 u64 index;
4201 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004202 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004203
4204 path = btrfs_alloc_path();
4205 if (!path)
4206 return -ENOMEM;
4207
Li Zefan33345d012011-04-20 10:31:50 +08004208 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004209 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004210 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004211 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004212 goto out;
4213 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004214
4215 leaf = path->nodes[0];
4216 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4217 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4218 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004219 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004220 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004221 goto out;
4222 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004223 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004224
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004225 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4226 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004227 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004228 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004229 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004230 goto out;
4231 }
Li Zefan33345d012011-04-20 10:31:50 +08004232 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004233 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004234 if (IS_ERR_OR_NULL(di)) {
4235 if (!di)
4236 ret = -ENOENT;
4237 else
4238 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004239 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004240 goto out;
4241 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004242
4243 leaf = path->nodes[0];
4244 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004245 index = key.offset;
4246 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004247 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004248
Lu Fengqi9add2942018-08-01 11:32:26 +08004249 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004250 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004251 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004252 goto out;
4253 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004254
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004255 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004256 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004257 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004258 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004259 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004260 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004261out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004262 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004263 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004264}
4265
Misono Tomohiroec42f162018-04-18 11:34:13 +09004266/*
4267 * Helper to check if the subvolume references other subvolumes or if it's
4268 * default.
4269 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004270static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004271{
4272 struct btrfs_fs_info *fs_info = root->fs_info;
4273 struct btrfs_path *path;
4274 struct btrfs_dir_item *di;
4275 struct btrfs_key key;
4276 u64 dir_id;
4277 int ret;
4278
4279 path = btrfs_alloc_path();
4280 if (!path)
4281 return -ENOMEM;
4282
4283 /* Make sure this root isn't set as the default subvol */
4284 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4285 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4286 dir_id, "default", 7, 0);
4287 if (di && !IS_ERR(di)) {
4288 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4289 if (key.objectid == root->root_key.objectid) {
4290 ret = -EPERM;
4291 btrfs_err(fs_info,
4292 "deleting default subvolume %llu is not allowed",
4293 key.objectid);
4294 goto out;
4295 }
4296 btrfs_release_path(path);
4297 }
4298
4299 key.objectid = root->root_key.objectid;
4300 key.type = BTRFS_ROOT_REF_KEY;
4301 key.offset = (u64)-1;
4302
4303 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4304 if (ret < 0)
4305 goto out;
4306 BUG_ON(ret == 0);
4307
4308 ret = 0;
4309 if (path->slots[0] > 0) {
4310 path->slots[0]--;
4311 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4312 if (key.objectid == root->root_key.objectid &&
4313 key.type == BTRFS_ROOT_REF_KEY)
4314 ret = -ENOTEMPTY;
4315 }
4316out:
4317 btrfs_free_path(path);
4318 return ret;
4319}
4320
Nikolay Borisov20a68002018-04-27 14:36:24 +03004321/* Delete all dentries for inodes belonging to the root */
4322static void btrfs_prune_dentries(struct btrfs_root *root)
4323{
4324 struct btrfs_fs_info *fs_info = root->fs_info;
4325 struct rb_node *node;
4326 struct rb_node *prev;
4327 struct btrfs_inode *entry;
4328 struct inode *inode;
4329 u64 objectid = 0;
4330
4331 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4332 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4333
4334 spin_lock(&root->inode_lock);
4335again:
4336 node = root->inode_tree.rb_node;
4337 prev = NULL;
4338 while (node) {
4339 prev = node;
4340 entry = rb_entry(node, struct btrfs_inode, rb_node);
4341
David Sterba37508512018-06-29 10:56:40 +02004342 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004343 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004344 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004345 node = node->rb_right;
4346 else
4347 break;
4348 }
4349 if (!node) {
4350 while (prev) {
4351 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004352 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004353 node = prev;
4354 break;
4355 }
4356 prev = rb_next(prev);
4357 }
4358 }
4359 while (node) {
4360 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004361 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004362 inode = igrab(&entry->vfs_inode);
4363 if (inode) {
4364 spin_unlock(&root->inode_lock);
4365 if (atomic_read(&inode->i_count) > 1)
4366 d_prune_aliases(inode);
4367 /*
4368 * btrfs_drop_inode will have it removed from the inode
4369 * cache when its usage count hits zero.
4370 */
4371 iput(inode);
4372 cond_resched();
4373 spin_lock(&root->inode_lock);
4374 goto again;
4375 }
4376
4377 if (cond_resched_lock(&root->inode_lock))
4378 goto again;
4379
4380 node = rb_next(node);
4381 }
4382 spin_unlock(&root->inode_lock);
4383}
4384
Misono Tomohirof60a2362018-04-18 11:34:52 +09004385int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4386{
4387 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4388 struct btrfs_root *root = BTRFS_I(dir)->root;
4389 struct inode *inode = d_inode(dentry);
4390 struct btrfs_root *dest = BTRFS_I(inode)->root;
4391 struct btrfs_trans_handle *trans;
4392 struct btrfs_block_rsv block_rsv;
4393 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004394 int ret;
4395 int err;
4396
4397 /*
4398 * Don't allow to delete a subvolume with send in progress. This is
4399 * inside the inode lock so the error handling that has to drop the bit
4400 * again is not run concurrently.
4401 */
4402 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004403 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004404 spin_unlock(&dest->root_item_lock);
4405 btrfs_warn(fs_info,
4406 "attempt to delete subvolume %llu during send",
4407 dest->root_key.objectid);
4408 return -EPERM;
4409 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004410 root_flags = btrfs_root_flags(&dest->root_item);
4411 btrfs_set_root_flags(&dest->root_item,
4412 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4413 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004414
4415 down_write(&fs_info->subvol_sem);
4416
4417 err = may_destroy_subvol(dest);
4418 if (err)
4419 goto out_up_write;
4420
4421 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4422 /*
4423 * One for dir inode,
4424 * two for dir entries,
4425 * two for root ref/backref.
4426 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004427 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004428 if (err)
4429 goto out_up_write;
4430
4431 trans = btrfs_start_transaction(root, 0);
4432 if (IS_ERR(trans)) {
4433 err = PTR_ERR(trans);
4434 goto out_release;
4435 }
4436 trans->block_rsv = &block_rsv;
4437 trans->bytes_reserved = block_rsv.size;
4438
4439 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4440
Lu Fengqi401b3b12018-08-01 11:32:30 +08004441 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4442 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004443 if (ret) {
4444 err = ret;
4445 btrfs_abort_transaction(trans, ret);
4446 goto out_end_trans;
4447 }
4448
4449 btrfs_record_root_in_trans(trans, dest);
4450
4451 memset(&dest->root_item.drop_progress, 0,
4452 sizeof(dest->root_item.drop_progress));
4453 dest->root_item.drop_level = 0;
4454 btrfs_set_root_refs(&dest->root_item, 0);
4455
4456 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4457 ret = btrfs_insert_orphan_item(trans,
4458 fs_info->tree_root,
4459 dest->root_key.objectid);
4460 if (ret) {
4461 btrfs_abort_transaction(trans, ret);
4462 err = ret;
4463 goto out_end_trans;
4464 }
4465 }
4466
Lu Fengqid1957792018-05-29 15:01:54 +08004467 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004468 BTRFS_UUID_KEY_SUBVOL,
4469 dest->root_key.objectid);
4470 if (ret && ret != -ENOENT) {
4471 btrfs_abort_transaction(trans, ret);
4472 err = ret;
4473 goto out_end_trans;
4474 }
4475 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004476 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004477 dest->root_item.received_uuid,
4478 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4479 dest->root_key.objectid);
4480 if (ret && ret != -ENOENT) {
4481 btrfs_abort_transaction(trans, ret);
4482 err = ret;
4483 goto out_end_trans;
4484 }
4485 }
4486
4487out_end_trans:
4488 trans->block_rsv = NULL;
4489 trans->bytes_reserved = 0;
4490 ret = btrfs_end_transaction(trans);
4491 if (ret && !err)
4492 err = ret;
4493 inode->i_flags |= S_DEAD;
4494out_release:
4495 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4496out_up_write:
4497 up_write(&fs_info->subvol_sem);
4498 if (err) {
4499 spin_lock(&dest->root_item_lock);
4500 root_flags = btrfs_root_flags(&dest->root_item);
4501 btrfs_set_root_flags(&dest->root_item,
4502 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4503 spin_unlock(&dest->root_item_lock);
4504 } else {
4505 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004506 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004507 ASSERT(dest->send_in_progress == 0);
4508
4509 /* the last ref */
4510 if (dest->ino_cache_inode) {
4511 iput(dest->ino_cache_inode);
4512 dest->ino_cache_inode = NULL;
4513 }
4514 }
4515
4516 return err;
4517}
4518
Chris Mason39279cc2007-06-12 06:35:45 -04004519static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4520{
David Howells2b0143b2015-03-17 22:25:59 +00004521 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004522 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004523 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004524 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004525 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004526
David Sterbab3ae2442012-09-13 16:04:34 -06004527 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004528 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004529 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004530 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004531
Josef Bacikd52be812013-05-29 14:54:47 -04004532 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004533 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004534 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004535
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004536 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004537 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004538 BTRFS_I(inode)->location.objectid,
4539 dentry->d_name.name,
4540 dentry->d_name.len);
4541 goto out;
4542 }
4543
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004544 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004545 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004546 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004547
Filipe Manana44f714d2016-06-06 16:11:13 +01004548 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4549
Chris Mason39279cc2007-06-12 06:35:45 -04004550 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004551 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4552 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4553 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004554 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004555 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004556 /*
4557 * Propagate the last_unlink_trans value of the deleted dir to
4558 * its parent directory. This is to prevent an unrecoverable
4559 * log tree in the case we do something like this:
4560 * 1) create dir foo
4561 * 2) create snapshot under dir foo
4562 * 3) delete the snapshot
4563 * 4) rmdir foo
4564 * 5) mkdir foo
4565 * 6) fsync foo or some file inside foo
4566 */
4567 if (last_unlink_trans >= trans->transid)
4568 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4569 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004570out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004571 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004572 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004573
Chris Mason39279cc2007-06-12 06:35:45 -04004574 return err;
4575}
4576
Josef Bacikddfae632017-10-19 14:16:02 -04004577/*
4578 * Return this if we need to call truncate_block for the last bit of the
4579 * truncate.
4580 */
4581#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004582
Chris Mason323ac952008-10-01 19:05:46 -04004583/*
Chris Mason39279cc2007-06-12 06:35:45 -04004584 * this can truncate away extent items, csum items and directory items.
4585 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004586 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004587 *
4588 * csum items that cross the new i_size are truncated to the new size
4589 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004590 *
4591 * min_type is the minimum key type to truncate down to. If set to 0, this
4592 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004593 */
Yan, Zheng80825102009-11-12 09:35:36 +00004594int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4595 struct btrfs_root *root,
4596 struct inode *inode,
4597 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004598{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004599 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004600 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004601 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004602 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004603 struct btrfs_key key;
4604 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004605 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004606 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004607 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004608 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004609 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004610 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004611 int found_extent;
4612 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004613 int pending_del_nr = 0;
4614 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004615 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004616 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004617 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004618 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004619 bool be_nice = false;
4620 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004621
4622 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004623
Chris Mason28ed1342014-12-17 09:41:04 -08004624 /*
4625 * for non-free space inodes and ref cows, we want to back off from
4626 * time to time
4627 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004628 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004629 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004630 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004631
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004632 path = btrfs_alloc_path();
4633 if (!path)
4634 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004635 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004636
Josef Bacik5dc562c2012-08-17 13:14:17 -04004637 /*
4638 * We want to drop from the next block forward in case this new size is
4639 * not block aligned since we will be keeping the last block of the
4640 * extent just the way it is.
4641 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004642 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004643 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004644 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004645 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004646 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004647
Miao Xie16cdcec2011-04-22 18:12:22 +08004648 /*
4649 * This function is also used to drop the items in the log tree before
4650 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004651 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004652 * items.
4653 */
4654 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004655 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004656
Li Zefan33345d012011-04-20 10:31:50 +08004657 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004658 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004659 key.type = (u8)-1;
4660
Chris Mason85e21ba2008-01-29 15:11:36 -05004661search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004662 /*
4663 * with a 16K leaf size and 128MB extents, you can actually queue
4664 * up a huge file in a single leaf. Most of the time that
4665 * bytes_deleted is > 0, it will be huge by the time we get here
4666 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004667 if (be_nice && bytes_deleted > SZ_32M &&
4668 btrfs_should_end_transaction(trans)) {
4669 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004670 goto out;
4671 }
Chris Masond3977122009-01-05 21:25:51 -05004672
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004673 path->leave_spinning = 1;
4674 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4675 if (ret < 0)
4676 goto out;
4677
Chris Mason85e21ba2008-01-29 15:11:36 -05004678 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004679 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004680 /* there are no items in the tree for us to truncate, we're
4681 * done
4682 */
Yan, Zheng80825102009-11-12 09:35:36 +00004683 if (path->slots[0] == 0)
4684 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004685 path->slots[0]--;
4686 }
4687
Chris Masond3977122009-01-05 21:25:51 -05004688 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004689 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004690 leaf = path->nodes[0];
4691 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004692 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004693
Li Zefan33345d012011-04-20 10:31:50 +08004694 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004695 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004696
Chris Mason85e21ba2008-01-29 15:11:36 -05004697 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004698 break;
4699
Chris Mason5f39d392007-10-15 16:14:19 -04004700 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004701 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004702 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004703 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004704 extent_type = btrfs_file_extent_type(leaf, fi);
4705 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004706 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004707 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004708
4709 trace_btrfs_truncate_show_fi_regular(
4710 BTRFS_I(inode), leaf, fi,
4711 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004712 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004713 item_end += btrfs_file_extent_ram_bytes(leaf,
4714 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004715
4716 trace_btrfs_truncate_show_fi_inline(
4717 BTRFS_I(inode), leaf, fi, path->slots[0],
4718 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004719 }
Yan008630c2007-11-07 13:31:09 -05004720 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004721 }
Yan, Zheng80825102009-11-12 09:35:36 +00004722 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004723 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004724 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004725 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004726 break;
4727 if (found_key.offset >= new_size)
4728 del_item = 1;
4729 else
4730 del_item = 0;
4731 }
Chris Mason39279cc2007-06-12 06:35:45 -04004732 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004733 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004734 if (found_type != BTRFS_EXTENT_DATA_KEY)
4735 goto delete;
4736
4737 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004738 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004739 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004740 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004741 u64 orig_num_bytes =
4742 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004743 extent_num_bytes = ALIGN(new_size -
4744 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004745 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004746 btrfs_set_file_extent_num_bytes(leaf, fi,
4747 extent_num_bytes);
4748 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004749 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004750 if (test_bit(BTRFS_ROOT_REF_COWS,
4751 &root->state) &&
4752 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004753 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004754 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004755 } else {
Chris Masondb945352007-10-15 16:15:53 -04004756 extent_num_bytes =
4757 btrfs_file_extent_disk_num_bytes(leaf,
4758 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004759 extent_offset = found_key.offset -
4760 btrfs_file_extent_offset(leaf, fi);
4761
Chris Mason39279cc2007-06-12 06:35:45 -04004762 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004763 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004764 if (extent_start != 0) {
4765 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004766 if (test_bit(BTRFS_ROOT_REF_COWS,
4767 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004768 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004769 }
4770 }
Chris Mason90692182008-02-08 13:49:28 -05004771 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004772 /*
4773 * we can't truncate inline items that have had
4774 * special encodings
4775 */
4776 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004777 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004778 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4779 btrfs_file_extent_compression(leaf, fi) == 0) {
4780 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004781
Josef Bacikddfae632017-10-19 14:16:02 -04004782 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4783 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004784 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004785 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004786 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004787 * We have to bail so the last_size is set to
4788 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004789 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004790 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004791 break;
Chris Mason90692182008-02-08 13:49:28 -05004792 }
Josef Bacikddfae632017-10-19 14:16:02 -04004793
4794 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4795 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004796 }
Chris Mason179e29e2007-11-01 11:28:41 -04004797delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004798 if (del_item)
4799 last_size = found_key.offset;
4800 else
4801 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004802 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004803 if (!pending_del_nr) {
4804 /* no pending yet, add ourselves */
4805 pending_del_slot = path->slots[0];
4806 pending_del_nr = 1;
4807 } else if (pending_del_nr &&
4808 path->slots[0] + 1 == pending_del_slot) {
4809 /* hop on the pending chunk */
4810 pending_del_nr++;
4811 pending_del_slot = path->slots[0];
4812 } else {
Chris Masond3977122009-01-05 21:25:51 -05004813 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004814 }
Chris Mason39279cc2007-06-12 06:35:45 -04004815 } else {
4816 break;
4817 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004818 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004819
Miao Xie27cdeb72014-04-02 19:51:05 +08004820 if (found_extent &&
4821 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004822 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004823 struct btrfs_ref ref = { 0 };
4824
Chris Masonb9473432009-03-13 11:00:37 -04004825 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004826 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004827
4828 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4829 extent_start, extent_num_bytes, 0);
4830 ref.real_root = root->root_key.objectid;
4831 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4832 ino, extent_offset);
4833 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004834 if (ret) {
4835 btrfs_abort_transaction(trans, ret);
4836 break;
4837 }
Chris Mason28f75a02015-02-04 06:59:29 -08004838 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004839 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004840 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004841 }
Chris Mason39279cc2007-06-12 06:35:45 -04004842 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004843
Yan, Zheng80825102009-11-12 09:35:36 +00004844 if (found_type == BTRFS_INODE_ITEM_KEY)
4845 break;
4846
4847 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004848 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004849 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004850 if (pending_del_nr) {
4851 ret = btrfs_del_items(trans, root, path,
4852 pending_del_slot,
4853 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004854 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004855 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004856 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004857 }
Yan, Zheng80825102009-11-12 09:35:36 +00004858 pending_del_nr = 0;
4859 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004860 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004861
Chris Mason28f75a02015-02-04 06:59:29 -08004862 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004863 * We can generate a lot of delayed refs, so we need to
4864 * throttle every once and a while and make sure we're
4865 * adding enough space to keep up with the work we are
4866 * generating. Since we hold a transaction here we
4867 * can't flush, and we don't want to FLUSH_LIMIT because
4868 * we could have generated too many delayed refs to
4869 * actually allocate, so just bail if we're short and
4870 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004871 */
Josef Bacik28bad212018-12-03 10:20:38 -05004872 if (should_throttle) {
4873 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4874 BTRFS_RESERVE_NO_FLUSH);
4875 if (ret) {
4876 ret = -EAGAIN;
4877 break;
4878 }
Chris Mason28f75a02015-02-04 06:59:29 -08004879 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004880 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004881 } else {
4882 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004883 }
Chris Mason39279cc2007-06-12 06:35:45 -04004884 }
Yan, Zheng80825102009-11-12 09:35:36 +00004885out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004886 if (ret >= 0 && pending_del_nr) {
4887 int err;
4888
4889 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004890 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004891 if (err) {
4892 btrfs_abort_transaction(trans, err);
4893 ret = err;
4894 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004895 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004896 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4897 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004898 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004899 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004900 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004901 }
Chris Mason28ed1342014-12-17 09:41:04 -08004902
Chris Mason39279cc2007-06-12 06:35:45 -04004903 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004904 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004905}
4906
Chris Masona52d9a82007-08-27 16:49:44 -04004907/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304908 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004909 * @inode - inode that we're zeroing
4910 * @from - the offset to start zeroing
4911 * @len - the length to zero, 0 to zero the entire range respective to the
4912 * offset
4913 * @front - zero up to the offset instead of from the offset on
4914 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304915 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004916 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004917 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304918int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004919 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004920{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004922 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004923 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4924 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004925 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004926 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004927 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004928 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004929 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304930 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004931 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004932 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004933 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304934 u64 block_start;
4935 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004936
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004937 if (IS_ALIGNED(offset, blocksize) &&
4938 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004939 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304940
Josef Bacik8b62f872017-10-19 14:15:55 -04004941 block_start = round_down(from, blocksize);
4942 block_end = block_start + blocksize - 1;
4943
Qu Wenruo364ecf32017-02-27 15:10:38 +08004944 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004945 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004946 if (ret)
4947 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004948
Chris Mason211c17f2008-05-15 09:13:45 -04004949again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004950 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004951 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004952 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004953 block_start, blocksize, true);
4954 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004955 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004956 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004957 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004958
Chris Masona52d9a82007-08-27 16:49:44 -04004959 if (!PageUptodate(page)) {
4960 ret = btrfs_readpage(NULL, page);
4961 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004962 if (page->mapping != mapping) {
4963 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004964 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004965 goto again;
4966 }
Chris Masona52d9a82007-08-27 16:49:44 -04004967 if (!PageUptodate(page)) {
4968 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004969 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004970 }
4971 }
Chris Mason211c17f2008-05-15 09:13:45 -04004972 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004973
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304974 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004975 set_page_extent_mapped(page);
4976
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304977 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004978 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304979 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004980 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004981 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004982 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004983 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004984 btrfs_put_ordered_extent(ordered);
4985 goto again;
4986 }
4987
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304988 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004989 EXTENT_DIRTY | EXTENT_DELALLOC |
4990 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004991 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004992
Filipe Mananae3b8a482017-11-04 00:16:59 +00004993 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004994 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004995 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304996 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004997 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004998 goto out_unlock;
4999 }
5000
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305001 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04005002 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305003 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005004 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005005 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305006 memset(kaddr + (block_start - page_offset(page)),
5007 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04005008 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305009 memset(kaddr + (block_start - page_offset(page)) + offset,
5010 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005011 flush_dcache_page(page);
5012 kunmap(page);
5013 }
Chris Mason247e7432008-07-17 12:53:51 -04005014 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005015 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01005016 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04005017
Chris Mason89642222008-07-24 09:41:53 -04005018out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04005019 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005020 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08005021 blocksize, true);
5022 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04005023 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005024 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04005025out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08005026 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04005027 return ret;
5028}
5029
Josef Bacik16e75492013-10-22 12:18:51 -04005030static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
5031 u64 offset, u64 len)
5032{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005033 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04005034 struct btrfs_trans_handle *trans;
5035 int ret;
5036
5037 /*
5038 * Still need to make sure the inode looks like it's been updated so
5039 * that any holes get logged if we fsync.
5040 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005041 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
5042 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04005043 BTRFS_I(inode)->last_sub_trans = root->log_transid;
5044 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
5045 return 0;
5046 }
5047
5048 /*
5049 * 1 - for the one we're dropping
5050 * 1 - for the one we're adding
5051 * 1 - for updating the inode.
5052 */
5053 trans = btrfs_start_transaction(root, 3);
5054 if (IS_ERR(trans))
5055 return PTR_ERR(trans);
5056
5057 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5058 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005059 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005060 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005061 return ret;
5062 }
5063
David Sterbaf85b7372017-01-20 14:54:07 +01005064 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5065 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04005066 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04005067 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04005068 else
5069 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005070 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005071 return ret;
5072}
5073
Josef Bacik695a0d02011-03-04 15:46:53 -05005074/*
5075 * This function puts in dummy file extents for the area we're creating a hole
5076 * for. So if we are truncating this file to a larger size we need to insert
5077 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5078 * the range between oldsize and size
5079 */
Josef Bacika41ad392011-01-31 15:30:16 -05005080int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005082 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005083 struct btrfs_root *root = BTRFS_I(inode)->root;
5084 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005085 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005086 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005087 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005088 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5089 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005090 u64 last_byte;
5091 u64 cur_offset;
5092 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005093 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005094
Josef Bacika71754f2013-06-17 17:14:39 -04005095 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305096 * If our size started in the middle of a block we need to zero out the
5097 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005098 * expose stale data.
5099 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305100 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005101 if (err)
5102 return err;
5103
Yan Zheng9036c102008-10-30 14:19:41 -04005104 if (size <= hole_start)
5105 return 0;
5106
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03005107 btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start,
5108 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005109 cur_offset = hole_start;
5110 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005111 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005112 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005113 if (IS_ERR(em)) {
5114 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005115 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005116 break;
5117 }
Yan Zheng9036c102008-10-30 14:19:41 -04005118 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005119 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005120 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005121 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005122 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005123
Josef Bacik16e75492013-10-22 12:18:51 -04005124 err = maybe_insert_hole(root, inode, cur_offset,
5125 hole_size);
5126 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005127 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005128 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005129 cur_offset + hole_size - 1, 0);
5130 hole_em = alloc_extent_map();
5131 if (!hole_em) {
5132 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5133 &BTRFS_I(inode)->runtime_flags);
5134 goto next;
5135 }
5136 hole_em->start = cur_offset;
5137 hole_em->len = hole_size;
5138 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005139
Josef Bacik5dc562c2012-08-17 13:14:17 -04005140 hole_em->block_start = EXTENT_MAP_HOLE;
5141 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005142 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005143 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005144 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005145 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005146 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005147
5148 while (1) {
5149 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005150 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005151 write_unlock(&em_tree->lock);
5152 if (err != -EEXIST)
5153 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005154 btrfs_drop_extent_cache(BTRFS_I(inode),
5155 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005156 cur_offset +
5157 hole_size - 1, 0);
5158 }
5159 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005160 }
Josef Bacik16e75492013-10-22 12:18:51 -04005161next:
Yan Zheng9036c102008-10-30 14:19:41 -04005162 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005163 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005164 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005165 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005166 break;
5167 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005168 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005169 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005170 return err;
5171}
5172
Eric Sandeen3972f262013-01-12 02:57:22 +00005173static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005174{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005175 struct btrfs_root *root = BTRFS_I(inode)->root;
5176 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005177 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005178 loff_t newsize = attr->ia_size;
5179 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005180 int ret;
5181
Eric Sandeen3972f262013-01-12 02:57:22 +00005182 /*
5183 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5184 * special case where we need to update the times despite not having
5185 * these flags set. For all other operations the VFS set these flags
5186 * explicitly if it wants a timestamp update.
5187 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005188 if (newsize != oldsize) {
5189 inode_inc_iversion(inode);
5190 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5191 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005192 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005193 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005194
Josef Bacika41ad392011-01-31 15:30:16 -05005195 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005196 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005197 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005198 * This is to ensure the snapshot captures a fully consistent
5199 * state of this file - if the snapshot captures this expanding
5200 * truncation, it must capture all writes that happened before
5201 * this truncation.
5202 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005203 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005204 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005205 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005206 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005207 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005208 }
Yan, Zheng80825102009-11-12 09:35:36 +00005209
Miao Xief4a2f4c2011-12-14 20:12:01 -05005210 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005211 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005212 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005213 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005214 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005215
5216 i_size_write(inode, newsize);
5217 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305218 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005219 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005220 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005221 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005222 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005223
Josef Bacika41ad392011-01-31 15:30:16 -05005224 /*
5225 * We're truncating a file that used to have good data down to
5226 * zero. Make sure it gets into the ordered flush list so that
5227 * any new writes get down to disk quickly.
5228 */
5229 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005230 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5231 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005232
Josef Bacika41ad392011-01-31 15:30:16 -05005233 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005234
Andrea Gelmini52042d82018-11-28 12:05:13 +01005235 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005236 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005237 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005238 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005239
Filipe Manana213e8c52018-02-06 20:40:31 +00005240 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005241 if (ret && inode->i_nlink) {
5242 int err;
5243
5244 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005245 * Truncate failed, so fix up the in-memory size. We
5246 * adjusted disk_i_size down as we removed extents, so
5247 * wait for disk_i_size to be stable and then update the
5248 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005249 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005250 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005251 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005252 return err;
5253 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005254 }
Yan, Zheng80825102009-11-12 09:35:36 +00005255 }
5256
Josef Bacika41ad392011-01-31 15:30:16 -05005257 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005258}
5259
Chris Mason39279cc2007-06-12 06:35:45 -04005260static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5261{
David Howells2b0143b2015-03-17 22:25:59 +00005262 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005263 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005264 int err;
5265
Li Zefanb83cc962010-12-20 16:04:08 +08005266 if (btrfs_root_readonly(root))
5267 return -EROFS;
5268
Jan Kara31051c82016-05-26 16:55:18 +02005269 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005270 if (err)
5271 return err;
5272
Chris Mason5a3f23d2009-03-31 13:27:11 -04005273 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005274 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005275 if (err)
5276 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005277 }
Yan Zheng9036c102008-10-30 14:19:41 -04005278
Christoph Hellwig10257742010-06-04 11:30:02 +02005279 if (attr->ia_valid) {
5280 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005281 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005282 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005283
Josef Bacik22c44fe2011-11-30 10:45:38 -05005284 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005285 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005286 }
5287
Chris Mason39279cc2007-06-12 06:35:45 -04005288 return err;
5289}
Chris Mason61295eb2008-01-14 16:24:38 -05005290
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005291/*
5292 * While truncating the inode pages during eviction, we get the VFS calling
5293 * btrfs_invalidatepage() against each page of the inode. This is slow because
5294 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5295 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5296 * extent_state structures over and over, wasting lots of time.
5297 *
5298 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5299 * those expensive operations on a per page basis and do only the ordered io
5300 * finishing, while we release here the extent_map and extent_state structures,
5301 * without the excessive merging and splitting.
5302 */
5303static void evict_inode_truncate_pages(struct inode *inode)
5304{
5305 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5306 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5307 struct rb_node *node;
5308
5309 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005310 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005311
5312 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005313 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005314 struct extent_map *em;
5315
Liu Bo07e1ce02018-08-23 03:51:52 +08005316 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005317 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005318 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5319 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005320 remove_extent_mapping(map_tree, em);
5321 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005322 if (need_resched()) {
5323 write_unlock(&map_tree->lock);
5324 cond_resched();
5325 write_lock(&map_tree->lock);
5326 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005327 }
5328 write_unlock(&map_tree->lock);
5329
Filipe Manana6ca07092015-05-26 00:55:42 +01005330 /*
5331 * Keep looping until we have no more ranges in the io tree.
5332 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005333 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5334 * still in progress (unlocked the pages in the bio but did not yet
5335 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005336 * ranges can still be locked and eviction started because before
5337 * submitting those bios, which are executed by a separate task (work
5338 * queue kthread), inode references (inode->i_count) were not taken
5339 * (which would be dropped in the end io callback of each bio).
5340 * Therefore here we effectively end up waiting for those bios and
5341 * anyone else holding locked ranges without having bumped the inode's
5342 * reference count - if we don't do it, when they access the inode's
5343 * io_tree to unlock a range it may be too late, leading to an
5344 * use-after-free issue.
5345 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005346 spin_lock(&io_tree->lock);
5347 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5348 struct extent_state *state;
5349 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005350 u64 start;
5351 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005352 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005353
5354 node = rb_first(&io_tree->state);
5355 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005356 start = state->start;
5357 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005358 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005359 spin_unlock(&io_tree->lock);
5360
David Sterbaff13db42015-12-03 14:30:40 +01005361 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005362
5363 /*
5364 * If still has DELALLOC flag, the extent didn't reach disk,
5365 * and its reserved space won't be freed by delayed_ref.
5366 * So we need to free its reserved space here.
5367 * (Refer to comment in btrfs_invalidatepage, case 2)
5368 *
5369 * Note, end is the bytenr of last byte, so we need + 1 here.
5370 */
Filipe Manana421f0922018-10-12 13:02:48 +01005371 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005372 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005373
Filipe Manana6ca07092015-05-26 00:55:42 +01005374 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005375 EXTENT_LOCKED | EXTENT_DIRTY |
5376 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005377 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005378
Filipe Manana7064dd52014-08-08 02:47:05 +01005379 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005380 spin_lock(&io_tree->lock);
5381 }
5382 spin_unlock(&io_tree->lock);
5383}
5384
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005385static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005386 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005387{
5388 struct btrfs_fs_info *fs_info = root->fs_info;
5389 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005390 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005391 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005392 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005393
Josef Bacikd3984c92019-08-01 18:19:37 -04005394 /*
5395 * Eviction should be taking place at some place safe because of our
5396 * delayed iputs. However the normal flushing code will run delayed
5397 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5398 *
5399 * We reserve the delayed_refs_extra here again because we can't use
5400 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5401 * above. We reserve our extra bit here because we generate a ton of
5402 * delayed refs activity by truncating.
5403 *
5404 * If we cannot make our reservation we'll attempt to steal from the
5405 * global reserve, because we really want to be able to free up space.
5406 */
5407 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5408 BTRFS_RESERVE_FLUSH_EVICT);
5409 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005410 /*
5411 * Try to steal from the global reserve if there is space for
5412 * it.
5413 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005414 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5415 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5416 btrfs_warn(fs_info,
5417 "could not allocate space for delete; will truncate on mount");
5418 return ERR_PTR(-ENOSPC);
5419 }
5420 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005421 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005422
5423 trans = btrfs_join_transaction(root);
5424 if (IS_ERR(trans))
5425 return trans;
5426
5427 if (delayed_refs_extra) {
5428 trans->block_rsv = &fs_info->trans_block_rsv;
5429 trans->bytes_reserved = delayed_refs_extra;
5430 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5431 delayed_refs_extra, 1);
5432 }
5433 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005434}
5435
Al Virobd555972010-06-07 11:35:40 -04005436void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005437{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005438 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005439 struct btrfs_trans_handle *trans;
5440 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005441 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005442 int ret;
5443
liubo1abe9b82011-03-24 11:18:59 +00005444 trace_btrfs_inode_evict(inode);
5445
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005446 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005447 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005448 return;
5449 }
5450
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005451 evict_inode_truncate_pages(inode);
5452
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005453 if (inode->i_nlink &&
5454 ((btrfs_root_refs(&root->root_item) != 0 &&
5455 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005456 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005457 goto no_delete;
5458
Omar Sandoval27919062018-05-11 13:13:37 -07005459 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005460 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005461
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005462 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005463
Omar Sandoval7b40b692018-05-11 13:13:33 -07005464 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005465 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005466
Yan, Zheng76dda932009-09-21 16:00:26 -04005467 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005468 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5469 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005470 goto no_delete;
5471 }
5472
Nikolay Borisovaa790212017-01-10 20:35:40 +02005473 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005474 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005475 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005476
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005477 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005478 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005479 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005480 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005481 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005482
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005483 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005484
Yan, Zheng80825102009-11-12 09:35:36 +00005485 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005486 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005487 if (IS_ERR(trans))
5488 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005489
5490 trans->block_rsv = rsv;
5491
Yan, Zhengd68fc572010-05-16 10:49:58 -04005492 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005493 trans->block_rsv = &fs_info->trans_block_rsv;
5494 btrfs_end_transaction(trans);
5495 btrfs_btree_balance_dirty(fs_info);
5496 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5497 goto free_rsv;
5498 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005499 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005500 }
5501
Josef Bacik4ef31a42013-08-13 14:10:08 -04005502 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005503 * Errors here aren't a big deal, it just means we leave orphan items in
5504 * the tree. They will be cleaned up on the next mount. If the inode
5505 * number gets reused, cleanup deletes the orphan item without doing
5506 * anything, and unlink reuses the existing orphan item.
5507 *
5508 * If it turns out that we are dropping too many of these, we might want
5509 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005510 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005511 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005512 if (!IS_ERR(trans)) {
5513 trans->block_rsv = rsv;
5514 btrfs_orphan_del(trans, BTRFS_I(inode));
5515 trans->block_rsv = &fs_info->trans_block_rsv;
5516 btrfs_end_transaction(trans);
5517 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005518
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005519 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005520 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005521 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005522
Omar Sandoval27919062018-05-11 13:13:37 -07005523free_rsv:
5524 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005525no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005526 /*
5527 * If we didn't successfully delete, the orphan item will still be in
5528 * the tree and we'll retry on the next mount. Again, we might also want
5529 * to retry these periodically in the future.
5530 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005531 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005532 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005533}
5534
5535/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005536 * Return the key found in the dir entry in the location pointer, fill @type
5537 * with BTRFS_FT_*, and return 0.
5538 *
Su Yue005d6712018-03-05 17:13:37 +08005539 * If no dir entries were found, returns -ENOENT.
5540 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005541 */
5542static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005543 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005544{
5545 const char *name = dentry->d_name.name;
5546 int namelen = dentry->d_name.len;
5547 struct btrfs_dir_item *di;
5548 struct btrfs_path *path;
5549 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005550 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005551
5552 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005553 if (!path)
5554 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005555
David Sterbaf85b7372017-01-20 14:54:07 +01005556 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5557 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005558 if (IS_ERR_OR_NULL(di)) {
5559 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005560 goto out;
5561 }
Chris Masond3977122009-01-05 21:25:51 -05005562
Chris Mason5f39d392007-10-15 16:14:19 -04005563 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005564 if (location->type != BTRFS_INODE_ITEM_KEY &&
5565 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005566 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005567 btrfs_warn(root->fs_info,
5568"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5569 __func__, name, btrfs_ino(BTRFS_I(dir)),
5570 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005571 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005572 if (!ret)
5573 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005574out:
Chris Mason39279cc2007-06-12 06:35:45 -04005575 btrfs_free_path(path);
5576 return ret;
5577}
5578
5579/*
5580 * when we hit a tree root in a directory, the btrfs part of the inode
5581 * needs to be changed to reflect the root directory of the tree root. This
5582 * is kind of like crossing a mount point.
5583 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005584static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005585 struct inode *dir,
5586 struct dentry *dentry,
5587 struct btrfs_key *location,
5588 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005589{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005590 struct btrfs_path *path;
5591 struct btrfs_root *new_root;
5592 struct btrfs_root_ref *ref;
5593 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005594 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005595 int ret;
5596 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005597
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005598 path = btrfs_alloc_path();
5599 if (!path) {
5600 err = -ENOMEM;
5601 goto out;
5602 }
Chris Mason39279cc2007-06-12 06:35:45 -04005603
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005604 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005605 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5606 key.type = BTRFS_ROOT_REF_KEY;
5607 key.offset = location->objectid;
5608
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005609 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005610 if (ret) {
5611 if (ret < 0)
5612 err = ret;
5613 goto out;
5614 }
Chris Mason39279cc2007-06-12 06:35:45 -04005615
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005616 leaf = path->nodes[0];
5617 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005618 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005619 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5620 goto out;
5621
5622 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5623 (unsigned long)(ref + 1),
5624 dentry->d_name.len);
5625 if (ret)
5626 goto out;
5627
David Sterbab3b4aa72011-04-21 01:20:15 +02005628 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005629
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005630 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005631 if (IS_ERR(new_root)) {
5632 err = PTR_ERR(new_root);
5633 goto out;
5634 }
5635
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005636 *sub_root = new_root;
5637 location->objectid = btrfs_root_dirid(&new_root->root_item);
5638 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005639 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005640 err = 0;
5641out:
5642 btrfs_free_path(path);
5643 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005644}
5645
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005646static void inode_tree_add(struct inode *inode)
5647{
5648 struct btrfs_root *root = BTRFS_I(inode)->root;
5649 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005650 struct rb_node **p;
5651 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005652 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005653 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005654
Al Viro1d3382cb2010-10-23 15:19:20 -04005655 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005656 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005657 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005658 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005659 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005660 while (*p) {
5661 parent = *p;
5662 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5663
David Sterba37508512018-06-29 10:56:40 +02005664 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005665 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005666 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005667 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005668 else {
5669 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005670 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005671 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005672 RB_CLEAR_NODE(parent);
5673 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005674 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005675 }
5676 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005677 rb_link_node(new, parent, p);
5678 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005679 spin_unlock(&root->inode_lock);
5680}
5681
5682static void inode_tree_del(struct inode *inode)
5683{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005684 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005685 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005686 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005687
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005688 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005689 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005690 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005691 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005692 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005693 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005694 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005695
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005696 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005697 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005698 spin_lock(&root->inode_lock);
5699 empty = RB_EMPTY_ROOT(&root->inode_tree);
5700 spin_unlock(&root->inode_lock);
5701 if (empty)
5702 btrfs_add_dead_root(root);
5703 }
5704}
5705
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005706
Chris Masone02119d2008-09-05 16:13:11 -04005707static int btrfs_init_locked_inode(struct inode *inode, void *p)
5708{
5709 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005710 inode->i_ino = args->location->objectid;
5711 memcpy(&BTRFS_I(inode)->location, args->location,
5712 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005713 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005714 return 0;
5715}
5716
5717static int btrfs_find_actor(struct inode *inode, void *opaque)
5718{
5719 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005720 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005721 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005722}
5723
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005724static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005725 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005726 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005727{
5728 struct inode *inode;
5729 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005730 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005731
Chris Mason90d3e592014-01-09 17:28:00 -08005732 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005733 args.root = root;
5734
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005735 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005736 btrfs_init_locked_inode,
5737 (void *)&args);
5738 return inode;
5739}
5740
Balaji Rao1a54ef82008-07-21 02:01:04 +05305741/* Get an inode object given its location and corresponding root.
5742 * Returns in *is_new if the inode was read from disk
5743 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005744struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5745 struct btrfs_root *root, int *new,
5746 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305747{
5748 struct inode *inode;
5749
Chris Mason90d3e592014-01-09 17:28:00 -08005750 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305751 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005752 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305753
5754 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005755 int ret;
5756
Filipe Manana4222ea72018-10-24 10:13:03 +01005757 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005758 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005759 inode_tree_add(inode);
5760 unlock_new_inode(inode);
5761 if (new)
5762 *new = 1;
5763 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005764 iget_failed(inode);
5765 /*
5766 * ret > 0 can come from btrfs_search_slot called by
5767 * btrfs_read_locked_inode, this means the inode item
5768 * was not found.
5769 */
5770 if (ret > 0)
5771 ret = -ENOENT;
5772 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005773 }
5774 }
5775
Balaji Rao1a54ef82008-07-21 02:01:04 +05305776 return inode;
5777}
5778
Filipe Manana4222ea72018-10-24 10:13:03 +01005779struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5780 struct btrfs_root *root, int *new)
5781{
5782 return btrfs_iget_path(s, location, root, new, NULL);
5783}
5784
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005785static struct inode *new_simple_dir(struct super_block *s,
5786 struct btrfs_key *key,
5787 struct btrfs_root *root)
5788{
5789 struct inode *inode = new_inode(s);
5790
5791 if (!inode)
5792 return ERR_PTR(-ENOMEM);
5793
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005794 BTRFS_I(inode)->root = root;
5795 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005796 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005797
5798 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005799 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005800 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005801 inode->i_fop = &simple_dir_operations;
5802 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005803 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305804 inode->i_atime = inode->i_mtime;
5805 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005806 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005807
5808 return inode;
5809}
5810
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005811static inline u8 btrfs_inode_type(struct inode *inode)
5812{
5813 /*
5814 * Compile-time asserts that generic FT_* types still match
5815 * BTRFS_FT_* types
5816 */
5817 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5818 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5819 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5820 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5821 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5822 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5823 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5824 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5825
5826 return fs_umode_to_ftype(inode->i_mode);
5827}
5828
Chris Mason3de45862008-11-17 21:02:50 -05005829struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005830{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005831 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005832 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005833 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005834 struct btrfs_root *sub_root = root;
5835 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005836 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005837 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005838 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005839
5840 if (dentry->d_name.len > BTRFS_NAME_LEN)
5841 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005842
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005843 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005844 if (ret < 0)
5845 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005846
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005847 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005848 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005849 if (IS_ERR(inode))
5850 return inode;
5851
5852 /* Do extra check against inode mode with di_type */
5853 if (btrfs_inode_type(inode) != di_type) {
5854 btrfs_crit(fs_info,
5855"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5856 inode->i_mode, btrfs_inode_type(inode),
5857 di_type);
5858 iput(inode);
5859 return ERR_PTR(-EUCLEAN);
5860 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005861 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005862 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005863
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005864 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005865 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005866 &location, &sub_root);
5867 if (ret < 0) {
5868 if (ret != -ENOENT)
5869 inode = ERR_PTR(ret);
5870 else
5871 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5872 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005873 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005874 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005875 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005876
Julia Lawall34d19ba2011-01-24 19:55:19 +00005877 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005878 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005879 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005880 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005881 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005882 if (ret) {
5883 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005884 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005885 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005886 }
5887
Chris Mason3de45862008-11-17 21:02:50 -05005888 return inode;
5889}
5890
Nick Pigginfe15ce42011-01-07 17:49:23 +11005891static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005892{
5893 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005894 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005895
Li Zefan848cce02012-02-21 17:04:28 +08005896 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005897 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005898
Li Zefan848cce02012-02-21 17:04:28 +08005899 if (inode) {
5900 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005901 if (btrfs_root_refs(&root->root_item) == 0)
5902 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005903
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005904 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005905 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005906 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005907 return 0;
5908}
5909
Chris Mason3de45862008-11-17 21:02:50 -05005910static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005911 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005912{
Al Viro3837d202018-10-10 16:38:27 -04005913 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005914
Al Viro3837d202018-10-10 16:38:27 -04005915 if (inode == ERR_PTR(-ENOENT))
5916 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005917 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005918}
5919
Josef Bacik23b5ec72017-07-24 15:14:25 -04005920/*
5921 * All this infrastructure exists because dir_emit can fault, and we are holding
5922 * the tree lock when doing readdir. For now just allocate a buffer and copy
5923 * our information into that, and then dir_emit from the buffer. This is
5924 * similar to what NFS does, only we don't keep the buffer around in pagecache
5925 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5926 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5927 * tree lock.
5928 */
5929static int btrfs_opendir(struct inode *inode, struct file *file)
5930{
5931 struct btrfs_file_private *private;
5932
5933 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5934 if (!private)
5935 return -ENOMEM;
5936 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5937 if (!private->filldir_buf) {
5938 kfree(private);
5939 return -ENOMEM;
5940 }
5941 file->private_data = private;
5942 return 0;
5943}
5944
5945struct dir_entry {
5946 u64 ino;
5947 u64 offset;
5948 unsigned type;
5949 int name_len;
5950};
5951
5952static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5953{
5954 while (entries--) {
5955 struct dir_entry *entry = addr;
5956 char *name = (char *)(entry + 1);
5957
David Sterba92d32172018-04-16 21:10:14 +02005958 ctx->pos = get_unaligned(&entry->offset);
5959 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5960 get_unaligned(&entry->ino),
5961 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005962 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005963 addr += sizeof(struct dir_entry) +
5964 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005965 ctx->pos++;
5966 }
5967 return 0;
5968}
5969
Al Viro9cdda8d2013-05-22 16:48:09 -04005970static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005971{
Al Viro9cdda8d2013-05-22 16:48:09 -04005972 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005973 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005974 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005975 struct btrfs_dir_item *di;
5976 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005977 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005978 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005979 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005980 struct list_head ins_list;
5981 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005982 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005983 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005984 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005985 char *name_ptr;
5986 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005987 int entries = 0;
5988 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005989 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005990 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005991
Al Viro9cdda8d2013-05-22 16:48:09 -04005992 if (!dir_emit_dots(file, ctx))
5993 return 0;
5994
David Woodhouse49593bf2008-08-17 17:08:36 +01005995 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005996 if (!path)
5997 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005998
Josef Bacik23b5ec72017-07-24 15:14:25 -04005999 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01006000 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01006001
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006002 INIT_LIST_HEAD(&ins_list);
6003 INIT_LIST_HEAD(&del_list);
6004 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08006005
Josef Bacik23b5ec72017-07-24 15:14:25 -04006006again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006007 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04006008 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006009 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006010
Chris Mason39279cc2007-06-12 06:35:45 -04006011 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6012 if (ret < 0)
6013 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01006014
6015 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04006016 struct dir_entry *entry;
6017
Chris Mason5f39d392007-10-15 16:14:19 -04006018 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04006019 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08006020 if (slot >= btrfs_header_nritems(leaf)) {
6021 ret = btrfs_next_leaf(root, path);
6022 if (ret < 0)
6023 goto err;
6024 else if (ret > 0)
6025 break;
6026 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04006027 }
Chris Mason3de45862008-11-17 21:02:50 -05006028
Chris Mason5f39d392007-10-15 16:14:19 -04006029 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6030
6031 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04006032 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006033 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04006034 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04006035 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006036 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006037 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006038 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006039 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006040 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006041 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6042 PAGE_SIZE) {
6043 btrfs_release_path(path);
6044 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6045 if (ret)
6046 goto nopos;
6047 addr = private->filldir_buf;
6048 entries = 0;
6049 total_len = 0;
6050 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006051 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006052
6053 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006054 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006055 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006056 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6057 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006058 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006059 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006060 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006061 put_unaligned(location.objectid, &entry->ino);
6062 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006063 entries++;
6064 addr += sizeof(struct dir_entry) + name_len;
6065 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006066next:
6067 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006068 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006069 btrfs_release_path(path);
6070
6071 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6072 if (ret)
6073 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006074
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006075 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006076 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006077 goto nopos;
6078
Zach Browndb62efb2013-07-11 16:19:42 -07006079 /*
6080 * Stop new entries from being returned after we return the last
6081 * entry.
6082 *
6083 * New directory entries are assigned a strictly increasing
6084 * offset. This means that new entries created during readdir
6085 * are *guaranteed* to be seen in the future by that readdir.
6086 * This has broken buggy programs which operate on names as
6087 * they're returned by readdir. Until we re-use freed offsets
6088 * we have this hack to stop new entries from being returned
6089 * under the assumption that they'll never reach this huge
6090 * offset.
6091 *
6092 * This is being careful not to overflow 32bit loff_t unless the
6093 * last entry requires it because doing so has broken 32bit apps
6094 * in the past.
6095 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006096 if (ctx->pos >= INT_MAX)
6097 ctx->pos = LLONG_MAX;
6098 else
6099 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006100nopos:
6101 ret = 0;
6102err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006103 if (put)
6104 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006105 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006106 return ret;
6107}
6108
Chris Mason39279cc2007-06-12 06:35:45 -04006109/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006110 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006111 * inode changes. But, it is most likely to find the inode in cache.
6112 * FIXME, needs more benchmarking...there are no reasons other than performance
6113 * to keep or drop this code.
6114 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006115static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006116{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006117 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006118 struct btrfs_root *root = BTRFS_I(inode)->root;
6119 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006120 int ret;
6121
Josef Bacik72ac3c02012-05-23 14:13:11 -04006122 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006123 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006124
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006125 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006126 if (IS_ERR(trans))
6127 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006128
6129 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006130 if (ret && ret == -ENOSPC) {
6131 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006132 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006133 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006134 if (IS_ERR(trans))
6135 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006136
Chris Mason94b60442010-05-26 11:02:00 -04006137 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006138 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006139 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006140 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006141 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006142
6143 return ret;
6144}
6145
6146/*
6147 * This is a copy of file_update_time. We need this so we can return error on
6148 * ENOSPC for updating the inode in the case of file write and mmap writes.
6149 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006150static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006151 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006152{
Alexander Block2bc5565282012-06-15 09:49:33 +02006153 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006154 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006155
6156 if (btrfs_root_readonly(root))
6157 return -EROFS;
6158
Josef Bacike41f9412012-03-26 09:46:47 -04006159 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006160 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006161 if (flags & S_CTIME)
6162 inode->i_ctime = *now;
6163 if (flags & S_MTIME)
6164 inode->i_mtime = *now;
6165 if (flags & S_ATIME)
6166 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006167 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006168}
6169
Chris Masond352ac62008-09-29 15:18:18 -04006170/*
6171 * find the highest existing sequence number in a directory
6172 * and then set the in-memory index_cnt variable to reflect
6173 * free sequence numbers
6174 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006175static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006176{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006177 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006178 struct btrfs_key key, found_key;
6179 struct btrfs_path *path;
6180 struct extent_buffer *leaf;
6181 int ret;
6182
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006183 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006184 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006185 key.offset = (u64)-1;
6186
6187 path = btrfs_alloc_path();
6188 if (!path)
6189 return -ENOMEM;
6190
6191 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6192 if (ret < 0)
6193 goto out;
6194 /* FIXME: we should be able to handle this */
6195 if (ret == 0)
6196 goto out;
6197 ret = 0;
6198
6199 /*
6200 * MAGIC NUMBER EXPLANATION:
6201 * since we search a directory based on f_pos we have to start at 2
6202 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6203 * else has to start at 2
6204 */
6205 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006206 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006207 goto out;
6208 }
6209
6210 path->slots[0]--;
6211
6212 leaf = path->nodes[0];
6213 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6214
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006215 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006216 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006217 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006218 goto out;
6219 }
6220
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006221 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006222out:
6223 btrfs_free_path(path);
6224 return ret;
6225}
6226
Chris Masond352ac62008-09-29 15:18:18 -04006227/*
6228 * helper to find a free sequence number in a given directory. This current
6229 * code is very simple, later versions will do smarter things in the btree
6230 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006231int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006232{
6233 int ret = 0;
6234
Nikolay Borisov877574e2017-02-20 13:50:33 +02006235 if (dir->index_cnt == (u64)-1) {
6236 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006237 if (ret) {
6238 ret = btrfs_set_inode_index_count(dir);
6239 if (ret)
6240 return ret;
6241 }
Josef Bacikaec74772008-07-24 12:12:38 -04006242 }
6243
Nikolay Borisov877574e2017-02-20 13:50:33 +02006244 *index = dir->index_cnt;
6245 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006246
6247 return ret;
6248}
6249
Chris Masonb0d5d102014-09-08 13:08:51 -07006250static int btrfs_insert_inode_locked(struct inode *inode)
6251{
6252 struct btrfs_iget_args args;
6253 args.location = &BTRFS_I(inode)->location;
6254 args.root = BTRFS_I(inode)->root;
6255
6256 return insert_inode_locked4(inode,
6257 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6258 btrfs_find_actor, &args);
6259}
6260
Anand Jain19aee8d2017-07-18 17:37:05 +08006261/*
6262 * Inherit flags from the parent inode.
6263 *
6264 * Currently only the compression flags and the cow flags are inherited.
6265 */
6266static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6267{
6268 unsigned int flags;
6269
6270 if (!dir)
6271 return;
6272
6273 flags = BTRFS_I(dir)->flags;
6274
6275 if (flags & BTRFS_INODE_NOCOMPRESS) {
6276 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6277 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6278 } else if (flags & BTRFS_INODE_COMPRESS) {
6279 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6280 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6281 }
6282
6283 if (flags & BTRFS_INODE_NODATACOW) {
6284 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6285 if (S_ISREG(inode->i_mode))
6286 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6287 }
6288
David Sterba7b6a2212018-03-26 18:40:21 +02006289 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006290}
6291
Chris Mason39279cc2007-06-12 06:35:45 -04006292static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6293 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006294 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006295 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006296 u64 ref_objectid, u64 objectid,
6297 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006298{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006299 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006300 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006301 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006302 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006303 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006304 struct btrfs_inode_ref *ref;
6305 struct btrfs_key key[2];
6306 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006307 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006308 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006309 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006310
Chris Mason5f39d392007-10-15 16:14:19 -04006311 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006312 if (!path)
6313 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006314
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006315 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006316 if (!inode) {
6317 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006318 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006319 }
Chris Mason39279cc2007-06-12 06:35:45 -04006320
Li Zefan581bb052011-04-20 10:06:11 +08006321 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006322 * O_TMPFILE, set link count to 0, so that after this point,
6323 * we fill in an inode item with the correct link count.
6324 */
6325 if (!name)
6326 set_nlink(inode, 0);
6327
6328 /*
Li Zefan581bb052011-04-20 10:06:11 +08006329 * we have to initialize this early, so we can reclaim the inode
6330 * number if we fail afterwards in this function.
6331 */
6332 inode->i_ino = objectid;
6333
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006334 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006335 trace_btrfs_inode_request(dir);
6336
Nikolay Borisov877574e2017-02-20 13:50:33 +02006337 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006338 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006339 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006340 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006341 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006342 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006343 } else if (dir) {
6344 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006345 }
6346 /*
6347 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006348 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006349 * number
6350 */
6351 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006352 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006353 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006354 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006355 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006356
Josef Bacik5dc562c2012-08-17 13:14:17 -04006357 /*
6358 * We could have gotten an inode number from somebody who was fsynced
6359 * and then removed in this same transaction, so let's just set full
6360 * sync since it will be a full sync anyway and this will blow away the
6361 * old info in the log.
6362 */
6363 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6364
Chris Mason9c583092008-01-29 15:15:18 -05006365 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006366 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006367 key[0].offset = 0;
6368
Chris Mason9c583092008-01-29 15:15:18 -05006369 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006370
6371 if (name) {
6372 /*
6373 * Start new inodes with an inode_ref. This is slightly more
6374 * efficient for small numbers of hard links since they will
6375 * be packed into one item. Extended refs will kick in if we
6376 * add more hard links than can fit in the ref item.
6377 */
6378 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006379 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006380 key[1].offset = ref_objectid;
6381
6382 sizes[1] = name_len + sizeof(*ref);
6383 }
Chris Mason9c583092008-01-29 15:15:18 -05006384
Chris Masonb0d5d102014-09-08 13:08:51 -07006385 location = &BTRFS_I(inode)->location;
6386 location->objectid = objectid;
6387 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006388 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006389
6390 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006391 if (ret < 0) {
6392 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006393 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006394 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006395
Chris Masonb9473432009-03-13 11:00:37 -04006396 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006397 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006398 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006399 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006400
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006401 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006402 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306403
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006404 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306405 inode->i_atime = inode->i_mtime;
6406 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006407 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306408
Chris Mason5f39d392007-10-15 16:14:19 -04006409 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6410 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006411 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006412 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006413 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006414
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006415 if (name) {
6416 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6417 struct btrfs_inode_ref);
6418 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6419 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6420 ptr = (unsigned long)(ref + 1);
6421 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6422 }
Chris Mason9c583092008-01-29 15:15:18 -05006423
Chris Mason5f39d392007-10-15 16:14:19 -04006424 btrfs_mark_buffer_dirty(path->nodes[0]);
6425 btrfs_free_path(path);
6426
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006427 btrfs_inherit_iflags(inode, dir);
6428
Al Viro569254b2011-07-24 17:08:40 -04006429 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006430 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006431 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006432 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006433 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6434 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006435 }
6436
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006437 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006438
6439 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006440 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006441
Alexander Block8ea05e32012-07-25 17:35:53 +02006442 btrfs_update_root_times(trans, root);
6443
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006444 ret = btrfs_inode_inherit_props(trans, inode, dir);
6445 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006446 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006447 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006448 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006449
Chris Mason39279cc2007-06-12 06:35:45 -04006450 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006451
6452fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006453 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006454fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006455 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006456 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006457 btrfs_free_path(path);
6458 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006459}
6460
Chris Masond352ac62008-09-29 15:18:18 -04006461/*
6462 * utility function to add 'inode' into 'parent_inode' with
6463 * a give name and a given sequence number.
6464 * if 'add_backref' is true, also insert a backref from the
6465 * inode to the parent directory.
6466 */
Chris Masone02119d2008-09-05 16:13:11 -04006467int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006468 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006469 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006470{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006471 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006472 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006473 struct btrfs_root *root = parent_inode->root;
6474 u64 ino = btrfs_ino(inode);
6475 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006476
Li Zefan33345d012011-04-20 10:31:50 +08006477 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006478 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006479 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006480 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006481 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006482 key.offset = 0;
6483 }
Chris Mason39279cc2007-06-12 06:35:45 -04006484
Li Zefan33345d012011-04-20 10:31:50 +08006485 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006486 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006487 root->root_key.objectid, parent_ino,
6488 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006489 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006490 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6491 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006492 }
6493
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006494 /* Nothing to clean up yet */
6495 if (ret)
6496 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006497
Lu Fengqi684572d2018-08-04 21:10:57 +08006498 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006499 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006500 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006501 goto fail_dir_item;
6502 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006503 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006504 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006505 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006506
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006507 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006508 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006509 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006510 /*
6511 * If we are replaying a log tree, we do not want to update the mtime
6512 * and ctime of the parent directory with the current time, since the
6513 * log replay procedure is responsible for setting them to their correct
6514 * values (the ones it had when the fsync was done).
6515 */
6516 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6517 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6518
6519 parent_inode->vfs_inode.i_mtime = now;
6520 parent_inode->vfs_inode.i_ctime = now;
6521 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006522 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006523 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006524 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006525 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006526
6527fail_dir_item:
6528 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6529 u64 local_index;
6530 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006531 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006532 root->root_key.objectid, parent_ino,
6533 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006534 if (err)
6535 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006536 } else if (add_backref) {
6537 u64 local_index;
6538 int err;
6539
6540 err = btrfs_del_inode_ref(trans, root, name, name_len,
6541 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006542 if (err)
6543 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006544 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006545
6546 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006547 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006548}
6549
6550static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006551 struct btrfs_inode *dir, struct dentry *dentry,
6552 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006553{
Josef Bacika1b075d2010-11-19 20:36:11 +00006554 int err = btrfs_add_link(trans, dir, inode,
6555 dentry->d_name.name, dentry->d_name.len,
6556 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006557 if (err > 0)
6558 err = -EEXIST;
6559 return err;
6560}
6561
Josef Bacik618e21d2007-07-11 10:18:17 -04006562static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006563 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006564{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006565 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006566 struct btrfs_trans_handle *trans;
6567 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006568 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006569 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006570 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006571 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006572
Josef Bacik9ed74f22009-09-11 16:12:44 -04006573 /*
6574 * 2 for inode item and ref
6575 * 2 for dir items
6576 * 1 for xattr if selinux is on
6577 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006578 trans = btrfs_start_transaction(root, 5);
6579 if (IS_ERR(trans))
6580 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006581
Li Zefan581bb052011-04-20 10:06:11 +08006582 err = btrfs_find_free_ino(root, &objectid);
6583 if (err)
6584 goto out_unlock;
6585
Josef Bacikaec74772008-07-24 12:12:38 -04006586 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006587 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6588 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006589 if (IS_ERR(inode)) {
6590 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006591 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006592 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006593 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006594
Casey Schauflerad19db72011-12-15 10:09:07 -05006595 /*
6596 * If the active LSM wants to access the inode during
6597 * d_instantiate it needs these. Smack checks to see
6598 * if the filesystem supports xattrs by looking at the
6599 * ops vector.
6600 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006601 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006602 init_special_inode(inode, inode->i_mode, rdev);
6603
6604 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006605 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006606 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006607
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006608 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6609 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006610 if (err)
6611 goto out_unlock;
6612
6613 btrfs_update_inode(trans, root, inode);
6614 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006615
Josef Bacik618e21d2007-07-11 10:18:17 -04006616out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006617 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006618 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006619 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006620 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006621 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006622 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006623 return err;
6624}
6625
Chris Mason39279cc2007-06-12 06:35:45 -04006626static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006627 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006628{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006629 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006630 struct btrfs_trans_handle *trans;
6631 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006632 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006633 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006634 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006635 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006636
Josef Bacik9ed74f22009-09-11 16:12:44 -04006637 /*
6638 * 2 for inode item and ref
6639 * 2 for dir items
6640 * 1 for xattr if selinux is on
6641 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006642 trans = btrfs_start_transaction(root, 5);
6643 if (IS_ERR(trans))
6644 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006645
Li Zefan581bb052011-04-20 10:06:11 +08006646 err = btrfs_find_free_ino(root, &objectid);
6647 if (err)
6648 goto out_unlock;
6649
Josef Bacikaec74772008-07-24 12:12:38 -04006650 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006651 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6652 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006653 if (IS_ERR(inode)) {
6654 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006655 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006656 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006657 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006658 /*
6659 * If the active LSM wants to access the inode during
6660 * d_instantiate it needs these. Smack checks to see
6661 * if the filesystem supports xattrs by looking at the
6662 * ops vector.
6663 */
6664 inode->i_fop = &btrfs_file_operations;
6665 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006666 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006667
6668 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6669 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006670 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006671
6672 err = btrfs_update_inode(trans, root, inode);
6673 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006674 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006675
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006676 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6677 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006678 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006679 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006680
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006681 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006682 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006683
Chris Mason39279cc2007-06-12 06:35:45 -04006684out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006685 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006686 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006687 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006688 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006689 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006690 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006691 return err;
6692}
6693
6694static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6695 struct dentry *dentry)
6696{
Filipe Manana271dba452016-01-05 16:24:05 +00006697 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006698 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006699 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006700 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006701 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006702 int err;
6703 int drop_inode = 0;
6704
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006705 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006706 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006707 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006708
Mark Fashehf1863732012-08-08 11:32:27 -07006709 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006710 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006711
Nikolay Borisov877574e2017-02-20 13:50:33 +02006712 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006713 if (err)
6714 goto fail;
6715
Yan, Zhenga22285a2010-05-16 10:48:46 -04006716 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006717 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006718 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006719 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006720 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006721 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006722 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006723 if (IS_ERR(trans)) {
6724 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006725 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006726 goto fail;
6727 }
Chris Mason5f39d392007-10-15 16:14:19 -04006728
Miao Xie67de1172013-12-26 13:07:06 +08006729 /* There are several dir indexes for this inode, clear the cache. */
6730 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006731 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006732 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006733 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006734 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006735 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006736
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006737 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6738 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006739
Yan, Zhenga5719522009-09-24 09:17:31 -04006740 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006741 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006742 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006743 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006744 int ret;
6745
Yan, Zhenga5719522009-09-24 09:17:31 -04006746 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006747 if (err)
6748 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006749 if (inode->i_nlink == 1) {
6750 /*
6751 * If new hard link count is 1, it's a file created
6752 * with open(2) O_TMPFILE flag.
6753 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006754 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006755 if (err)
6756 goto fail;
6757 }
Al Viro08c422c2011-12-23 07:58:13 -05006758 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006759 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6760 true, NULL);
6761 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6762 err = btrfs_commit_transaction(trans);
6763 trans = NULL;
6764 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006765 }
Chris Mason39279cc2007-06-12 06:35:45 -04006766
Chris Mason1832a6d2007-12-21 16:27:21 -05006767fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006768 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006769 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006770 if (drop_inode) {
6771 inode_dec_link_count(inode);
6772 iput(inode);
6773 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006774 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006775 return err;
6776}
6777
Al Viro18bb1db2011-07-26 01:41:39 -04006778static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006779{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006780 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006781 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006782 struct btrfs_trans_handle *trans;
6783 struct btrfs_root *root = BTRFS_I(dir)->root;
6784 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006785 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006786 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006787
Josef Bacik9ed74f22009-09-11 16:12:44 -04006788 /*
6789 * 2 items for inode and ref
6790 * 2 items for dir items
6791 * 1 for xattr if selinux is on
6792 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006793 trans = btrfs_start_transaction(root, 5);
6794 if (IS_ERR(trans))
6795 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006796
Li Zefan581bb052011-04-20 10:06:11 +08006797 err = btrfs_find_free_ino(root, &objectid);
6798 if (err)
6799 goto out_fail;
6800
Josef Bacikaec74772008-07-24 12:12:38 -04006801 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006802 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6803 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006804 if (IS_ERR(inode)) {
6805 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006806 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006807 goto out_fail;
6808 }
Chris Mason5f39d392007-10-15 16:14:19 -04006809
Chris Masonb0d5d102014-09-08 13:08:51 -07006810 /* these must be set before we unlock the inode */
6811 inode->i_op = &btrfs_dir_inode_operations;
6812 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006813
Eric Paris2a7dba32011-02-01 11:05:39 -05006814 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006815 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006816 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006817
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006818 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006819 err = btrfs_update_inode(trans, root, inode);
6820 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006821 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006822
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006823 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6824 dentry->d_name.name,
6825 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006826 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006827 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006828
Al Viro1e2e5472018-05-04 08:23:01 -04006829 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006830
6831out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006832 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006833 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006834 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006835 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006836 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006837 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006838 return err;
6839}
6840
Chris Masonc8b97812008-10-29 14:49:59 -04006841static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006842 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006843 size_t pg_offset, u64 extent_offset,
6844 struct btrfs_file_extent_item *item)
6845{
6846 int ret;
6847 struct extent_buffer *leaf = path->nodes[0];
6848 char *tmp;
6849 size_t max_size;
6850 unsigned long inline_size;
6851 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006852 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006853
6854 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006855 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006856 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6857 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006858 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006859 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006860 if (!tmp)
6861 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006862 ptr = btrfs_file_extent_inline_start(item);
6863
6864 read_extent_buffer(leaf, tmp, ptr, inline_size);
6865
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006866 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006867 ret = btrfs_decompress(compress_type, tmp, page,
6868 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006869
6870 /*
6871 * decompression code contains a memset to fill in any space between the end
6872 * of the uncompressed data and the end of max_size in case the decompressed
6873 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6874 * the end of an inline extent and the beginning of the next block, so we
6875 * cover that region here.
6876 */
6877
6878 if (max_size + pg_offset < PAGE_SIZE) {
6879 char *map = kmap(page);
6880 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6881 kunmap(page);
6882 }
Chris Masonc8b97812008-10-29 14:49:59 -04006883 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006884 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006885}
6886
Chris Masond352ac62008-09-29 15:18:18 -04006887/*
6888 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006889 * the ugly parts come from merging extents from the disk with the in-ram
6890 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006891 * where the in-ram extents might be locked pending data=ordered completion.
6892 *
6893 * This also copies inline extents directly into the page.
6894 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006895struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006896 struct page *page,
6897 size_t pg_offset, u64 start, u64 len,
6898 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006899{
David Sterba3ffbd682018-06-29 10:56:42 +02006900 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006901 int ret;
6902 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006903 u64 extent_start = 0;
6904 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006905 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006906 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006907 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006908 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006909 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006910 struct extent_buffer *leaf;
6911 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006912 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006913 struct extent_map_tree *em_tree = &inode->extent_tree;
6914 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006915 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006916
Chris Mason890871b2009-09-02 16:24:52 -04006917 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006918 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006919 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006920 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006921 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006922
Chris Masona52d9a82007-08-27 16:49:44 -04006923 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006924 if (em->start > start || em->start + em->len <= start)
6925 free_extent_map(em);
6926 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006927 free_extent_map(em);
6928 else
6929 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006930 }
David Sterba172ddd62011-04-21 00:48:27 +02006931 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006932 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006933 err = -ENOMEM;
6934 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006935 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006936 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006937 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006938 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006939 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006940 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006941
Liu Bobee6ec82018-08-17 05:05:28 +08006942 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006943 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006944 err = -ENOMEM;
6945 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006946 }
6947
Liu Bobee6ec82018-08-17 05:05:28 +08006948 /* Chances are we'll be called again, so go ahead and do readahead */
6949 path->reada = READA_FORWARD;
6950
Liu Boe49aabd2018-08-25 13:47:09 +08006951 /*
6952 * Unless we're going to uncompress the inline extent, no sleep would
6953 * happen.
6954 */
6955 path->leave_spinning = 1;
6956
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006957 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006958 if (ret < 0) {
6959 err = ret;
6960 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006961 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006962 if (path->slots[0] == 0)
6963 goto not_found;
6964 path->slots[0]--;
6965 }
6966
Chris Mason5f39d392007-10-15 16:14:19 -04006967 leaf = path->nodes[0];
6968 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006969 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006970 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006971 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006972 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006973 /*
6974 * If we backup past the first extent we want to move forward
6975 * and see if there is an extent in front of us, otherwise we'll
6976 * say there is a hole for our whole search range which can
6977 * cause problems.
6978 */
6979 extent_end = start;
6980 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006981 }
6982
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006983 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006984 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006985 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6986 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006987 /* Only regular file could have regular/prealloc extent */
6988 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6989 ret = -EUCLEAN;
6990 btrfs_crit(fs_info,
6991 "regular/prealloc extent found for non-regular inode %llu",
6992 btrfs_ino(inode));
6993 goto out;
6994 }
Chris Masona52d9a82007-08-27 16:49:44 -04006995 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006996 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006997
6998 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6999 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007000 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04007001 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08007002
7003 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007004 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007005 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007006
7007 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7008 path->slots[0],
7009 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007010 }
Josef Bacik25a50342013-10-14 12:08:38 -04007011next:
Yan Zheng9036c102008-10-30 14:19:41 -04007012 if (start >= extent_end) {
7013 path->slots[0]++;
7014 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7015 ret = btrfs_next_leaf(root, path);
7016 if (ret < 0) {
7017 err = ret;
7018 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007019 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04007020 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02007021 }
Yan Zheng9036c102008-10-30 14:19:41 -04007022 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007023 }
Yan Zheng9036c102008-10-30 14:19:41 -04007024 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7025 if (found_key.objectid != objectid ||
7026 found_key.type != BTRFS_EXTENT_DATA_KEY)
7027 goto not_found;
7028 if (start + len <= found_key.offset)
7029 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007030 if (start > found_key.offset)
7031 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007032
7033 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04007034 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007035 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007036 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02007037 em->block_start = EXTENT_MAP_HOLE;
7038 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04007039 }
7040
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007041 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007042 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007043
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007044 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7045 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007046 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02007047 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007048 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007049 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007050 size_t size;
7051 size_t extent_offset;
7052 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007053
Filipe Manana7ffbb592014-06-09 03:48:05 +01007054 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007055 goto out;
Yan689f9342007-10-29 11:41:07 -04007056
Qu Wenruoe41ca582018-06-06 15:41:49 +08007057 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007058 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007059 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7060 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007061 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007062 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007063 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007064 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007065 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007066
7067 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007068 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007069 if (btrfs_file_extent_compression(leaf, item) !=
7070 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007071 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007072 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007073 if (ret) {
7074 err = ret;
7075 goto out;
7076 }
Chris Masonc8b97812008-10-29 14:49:59 -04007077 } else {
7078 map = kmap(page);
7079 read_extent_buffer(leaf, map + pg_offset, ptr,
7080 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007081 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007082 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007083 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007084 copy_size);
7085 }
Chris Masonc8b97812008-10-29 14:49:59 -04007086 kunmap(page);
7087 }
Chris Mason179e29e2007-11-01 11:28:41 -04007088 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007089 }
Chris Masond1310b22008-01-24 16:13:08 -05007090 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007091 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007092 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007093 }
7094not_found:
7095 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007096 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007097 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007098 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007099insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007100 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007101 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007102 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007103 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7104 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007105 err = -EIO;
7106 goto out;
7107 }
Chris Masond1310b22008-01-24 16:13:08 -05007108
7109 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007110 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007111 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007112 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007113out:
Liu Boc6414282018-08-23 07:36:17 +08007114 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007115
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007116 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007117
Chris Masona52d9a82007-08-27 16:49:44 -04007118 if (err) {
7119 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007120 return ERR_PTR(err);
7121 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007122 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007123 return em;
7124}
7125
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007126struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007127 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007128{
7129 struct extent_map *em;
7130 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007131 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007132 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007133 u64 delalloc_len;
7134 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007135 int err = 0;
7136
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007137 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05007138 if (IS_ERR(em))
7139 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007140 /*
7141 * If our em maps to:
7142 * - a hole or
7143 * - a pre-alloc extent,
7144 * there might actually be delalloc bytes behind it.
7145 */
7146 if (em->block_start != EXTENT_MAP_HOLE &&
7147 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7148 return em;
7149 else
7150 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007151
7152 /* check to see if we've wrapped (len == -1 or similar) */
7153 end = start + len;
7154 if (end < start)
7155 end = (u64)-1;
7156 else
7157 end -= 1;
7158
7159 em = NULL;
7160
7161 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007162 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007163 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007164 delalloc_end = delalloc_start + delalloc_len;
7165 if (delalloc_end < delalloc_start)
7166 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007167
7168 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007169 * We didn't find anything useful, return the original results from
7170 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007171 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007172 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007173 em = hole_em;
7174 hole_em = NULL;
7175 goto out;
7176 }
7177
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007178 /*
7179 * Adjust the delalloc_start to make sure it doesn't go backwards from
7180 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007181 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007182 delalloc_start = max(start, delalloc_start);
7183 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007184
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007185 if (delalloc_len > 0) {
7186 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007187 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007188 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007189
David Sterba172ddd62011-04-21 00:48:27 +02007190 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007191 if (!em) {
7192 err = -ENOMEM;
7193 goto out;
7194 }
Chris Masonec29ed52011-02-23 16:23:20 -05007195 em->bdev = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007196
7197 ASSERT(hole_em);
7198 /*
7199 * When btrfs_get_extent can't find anything it returns one
7200 * huge hole
7201 *
7202 * Make sure what it found really fits our range, and adjust to
7203 * make sure it is based on the start from the caller
7204 */
7205 if (hole_end <= start || hole_em->start > end) {
7206 free_extent_map(hole_em);
7207 hole_em = NULL;
7208 } else {
7209 hole_start = max(hole_em->start, start);
7210 hole_len = hole_end - hole_start;
7211 }
7212
7213 if (hole_em && delalloc_start > hole_start) {
7214 /*
7215 * Our hole starts before our delalloc, so we have to
7216 * return just the parts of the hole that go until the
7217 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007218 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007219 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007220 em->start = hole_start;
7221 em->orig_start = hole_start;
7222 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007223 * Don't adjust block start at all, it is fixed at
7224 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007225 */
7226 em->block_start = hole_em->block_start;
7227 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007228 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7229 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007230 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007231 /*
7232 * Hole is out of passed range or it starts after
7233 * delalloc range
7234 */
7235 em->start = delalloc_start;
7236 em->len = delalloc_len;
7237 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007238 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007239 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007240 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007241 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007242 return hole_em;
7243 }
7244out:
7245
7246 free_extent_map(hole_em);
7247 if (err) {
7248 free_extent_map(em);
7249 return ERR_PTR(err);
7250 }
7251 return em;
7252}
7253
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007254static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7255 const u64 start,
7256 const u64 len,
7257 const u64 orig_start,
7258 const u64 block_start,
7259 const u64 block_len,
7260 const u64 orig_block_len,
7261 const u64 ram_bytes,
7262 const int type)
7263{
7264 struct extent_map *em = NULL;
7265 int ret;
7266
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007267 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007268 em = create_io_em(inode, start, len, orig_start,
7269 block_start, block_len, orig_block_len,
7270 ram_bytes,
7271 BTRFS_COMPRESS_NONE, /* compress_type */
7272 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007273 if (IS_ERR(em))
7274 goto out;
7275 }
7276 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7277 len, block_len, type);
7278 if (ret) {
7279 if (em) {
7280 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007281 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007282 start + len - 1, 0);
7283 }
7284 em = ERR_PTR(ret);
7285 }
7286 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007287
7288 return em;
7289}
7290
Josef Bacik4b46fce2010-05-23 11:00:55 -04007291static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7292 u64 start, u64 len)
7293{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007294 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007295 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007296 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007297 struct btrfs_key ins;
7298 u64 alloc_hint;
7299 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007300
Josef Bacik4b46fce2010-05-23 11:00:55 -04007301 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007302 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007303 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007304 if (ret)
7305 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007306
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007307 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7308 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007309 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007310 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007311 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007312 btrfs_free_reserved_extent(fs_info, ins.objectid,
7313 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007314
Josef Bacik4b46fce2010-05-23 11:00:55 -04007315 return em;
7316}
7317
Chris Mason46bfbb52010-05-26 11:04:10 -04007318/*
7319 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7320 * block must be cow'd
7321 */
Josef Bacik00361582013-08-14 14:02:47 -04007322noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007323 u64 *orig_start, u64 *orig_block_len,
7324 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007325{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007326 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007327 struct btrfs_path *path;
7328 int ret;
7329 struct extent_buffer *leaf;
7330 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007331 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007332 struct btrfs_file_extent_item *fi;
7333 struct btrfs_key key;
7334 u64 disk_bytenr;
7335 u64 backref_offset;
7336 u64 extent_end;
7337 u64 num_bytes;
7338 int slot;
7339 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007340 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007341
Chris Mason46bfbb52010-05-26 11:04:10 -04007342 path = btrfs_alloc_path();
7343 if (!path)
7344 return -ENOMEM;
7345
David Sterbaf85b7372017-01-20 14:54:07 +01007346 ret = btrfs_lookup_file_extent(NULL, root, path,
7347 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007348 if (ret < 0)
7349 goto out;
7350
7351 slot = path->slots[0];
7352 if (ret == 1) {
7353 if (slot == 0) {
7354 /* can't find the item, must cow */
7355 ret = 0;
7356 goto out;
7357 }
7358 slot--;
7359 }
7360 ret = 0;
7361 leaf = path->nodes[0];
7362 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007363 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007364 key.type != BTRFS_EXTENT_DATA_KEY) {
7365 /* not our file or wrong item type, must cow */
7366 goto out;
7367 }
7368
7369 if (key.offset > offset) {
7370 /* Wrong offset, must cow */
7371 goto out;
7372 }
7373
7374 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7375 found_type = btrfs_file_extent_type(leaf, fi);
7376 if (found_type != BTRFS_FILE_EXTENT_REG &&
7377 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7378 /* not a regular extent, must cow */
7379 goto out;
7380 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007381
7382 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7383 goto out;
7384
Miao Xiee77751a2013-12-27 21:11:50 +08007385 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7386 if (extent_end <= offset)
7387 goto out;
7388
Chris Mason46bfbb52010-05-26 11:04:10 -04007389 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007390 if (disk_bytenr == 0)
7391 goto out;
7392
7393 if (btrfs_file_extent_compression(leaf, fi) ||
7394 btrfs_file_extent_encryption(leaf, fi) ||
7395 btrfs_file_extent_other_encoding(leaf, fi))
7396 goto out;
7397
Ethan Lien78d42952018-05-17 14:58:29 +08007398 /*
7399 * Do the same check as in btrfs_cross_ref_exist but without the
7400 * unnecessary search.
7401 */
7402 if (btrfs_file_extent_generation(leaf, fi) <=
7403 btrfs_root_last_snapshot(&root->root_item))
7404 goto out;
7405
Chris Mason46bfbb52010-05-26 11:04:10 -04007406 backref_offset = btrfs_file_extent_offset(leaf, fi);
7407
Josef Bacik7ee9e442013-06-21 16:37:03 -04007408 if (orig_start) {
7409 *orig_start = key.offset - backref_offset;
7410 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7411 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7412 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007413
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007414 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007415 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007416
7417 num_bytes = min(offset + *len, extent_end) - offset;
7418 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7419 u64 range_end;
7420
Jeff Mahoneyda170662016-06-15 09:22:56 -04007421 range_end = round_up(offset + num_bytes,
7422 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007423 ret = test_range_bit(io_tree, offset, range_end,
7424 EXTENT_DELALLOC, 0, NULL);
7425 if (ret) {
7426 ret = -EAGAIN;
7427 goto out;
7428 }
7429 }
7430
Josef Bacik1bda19e2013-10-18 12:10:36 -04007431 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007432
7433 /*
7434 * look for other files referencing this extent, if we
7435 * find any we must cow
7436 */
Josef Bacik00361582013-08-14 14:02:47 -04007437
Liu Boe4c3b2d2017-01-30 12:25:28 -08007438 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007439 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007440 if (ret) {
7441 ret = 0;
7442 goto out;
7443 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007444
7445 /*
7446 * adjust disk_bytenr and num_bytes to cover just the bytes
7447 * in this extent we are about to write. If there
7448 * are any csums in that range we have to cow in order
7449 * to keep the csums correct
7450 */
7451 disk_bytenr += backref_offset;
7452 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007453 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7454 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007455 /*
7456 * all of the above have passed, it is safe to overwrite this extent
7457 * without cow
7458 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007459 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007460 ret = 1;
7461out:
7462 btrfs_free_path(path);
7463 return ret;
7464}
7465
Josef Bacikeb838e72012-07-31 16:28:48 -04007466static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7467 struct extent_state **cached_state, int writing)
7468{
7469 struct btrfs_ordered_extent *ordered;
7470 int ret = 0;
7471
7472 while (1) {
7473 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007474 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007475 /*
7476 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007477 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007478 * extents in this range.
7479 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007480 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007481 lockend - lockstart + 1);
7482
7483 /*
7484 * We need to make sure there are no buffered pages in this
7485 * range either, we could have raced between the invalidate in
7486 * generic_file_direct_write and locking the extent. The
7487 * invalidate needs to happen so that reads after a write do not
7488 * get stale data.
7489 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007490 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007491 (!writing || !filemap_range_has_page(inode->i_mapping,
7492 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007493 break;
7494
7495 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007496 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007497
7498 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007499 /*
7500 * If we are doing a DIO read and the ordered extent we
7501 * found is for a buffered write, we can not wait for it
7502 * to complete and retry, because if we do so we can
7503 * deadlock with concurrent buffered writes on page
7504 * locks. This happens only if our DIO read covers more
7505 * than one extent map, if at this point has already
7506 * created an ordered extent for a previous extent map
7507 * and locked its range in the inode's io tree, and a
7508 * concurrent write against that previous extent map's
7509 * range and this range started (we unlock the ranges
7510 * in the io tree only when the bios complete and
7511 * buffered writes always lock pages before attempting
7512 * to lock range in the io tree).
7513 */
7514 if (writing ||
7515 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7516 btrfs_start_ordered_extent(inode, ordered, 1);
7517 else
7518 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007519 btrfs_put_ordered_extent(ordered);
7520 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007521 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007522 * We could trigger writeback for this range (and wait
7523 * for it to complete) and then invalidate the pages for
7524 * this range (through invalidate_inode_pages2_range()),
7525 * but that can lead us to a deadlock with a concurrent
7526 * call to readpages() (a buffered read or a defrag call
7527 * triggered a readahead) on a page lock due to an
7528 * ordered dio extent we created before but did not have
7529 * yet a corresponding bio submitted (whence it can not
7530 * complete), which makes readpages() wait for that
7531 * ordered extent to complete while holding a lock on
7532 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007533 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007534 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007535 }
7536
Filipe Mananaade77022016-02-18 14:28:55 +00007537 if (ret)
7538 break;
7539
Josef Bacikeb838e72012-07-31 16:28:48 -04007540 cond_resched();
7541 }
7542
7543 return ret;
7544}
7545
Liu Bo6f9994d2017-01-31 07:50:22 -08007546/* The callers of this must take lock_extent() */
7547static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7548 u64 orig_start, u64 block_start,
7549 u64 block_len, u64 orig_block_len,
7550 u64 ram_bytes, int compress_type,
7551 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007552{
7553 struct extent_map_tree *em_tree;
7554 struct extent_map *em;
7555 struct btrfs_root *root = BTRFS_I(inode)->root;
7556 int ret;
7557
Liu Bo6f9994d2017-01-31 07:50:22 -08007558 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7559 type == BTRFS_ORDERED_COMPRESSED ||
7560 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007561 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007562
Josef Bacik69ffb542012-09-11 15:40:07 -04007563 em_tree = &BTRFS_I(inode)->extent_tree;
7564 em = alloc_extent_map();
7565 if (!em)
7566 return ERR_PTR(-ENOMEM);
7567
7568 em->start = start;
7569 em->orig_start = orig_start;
7570 em->len = len;
7571 em->block_len = block_len;
7572 em->block_start = block_start;
7573 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007574 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007575 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007576 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007577 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007578 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007579 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007580 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007581 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7582 em->compress_type = compress_type;
7583 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007584
7585 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007586 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007587 em->start + em->len - 1, 0);
7588 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007589 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007590 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007591 /*
7592 * The caller has taken lock_extent(), who could race with us
7593 * to add em?
7594 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007595 } while (ret == -EEXIST);
7596
7597 if (ret) {
7598 free_extent_map(em);
7599 return ERR_PTR(ret);
7600 }
7601
Liu Bo6f9994d2017-01-31 07:50:22 -08007602 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007603 return em;
7604}
7605
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007606
7607static int btrfs_get_blocks_direct_read(struct extent_map *em,
7608 struct buffer_head *bh_result,
7609 struct inode *inode,
7610 u64 start, u64 len)
7611{
7612 if (em->block_start == EXTENT_MAP_HOLE ||
7613 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7614 return -ENOENT;
7615
7616 len = min(len, em->len - (start - em->start));
7617
7618 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7619 inode->i_blkbits;
7620 bh_result->b_size = len;
7621 bh_result->b_bdev = em->bdev;
7622 set_buffer_mapped(bh_result);
7623
7624 return 0;
7625}
7626
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007627static int btrfs_get_blocks_direct_write(struct extent_map **map,
7628 struct buffer_head *bh_result,
7629 struct inode *inode,
7630 struct btrfs_dio_data *dio_data,
7631 u64 start, u64 len)
7632{
7633 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7634 struct extent_map *em = *map;
7635 int ret = 0;
7636
7637 /*
7638 * We don't allocate a new extent in the following cases
7639 *
7640 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7641 * existing extent.
7642 * 2) The extent is marked as PREALLOC. We're good to go here and can
7643 * just use the extent.
7644 *
7645 */
7646 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7647 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7648 em->block_start != EXTENT_MAP_HOLE)) {
7649 int type;
7650 u64 block_start, orig_start, orig_block_len, ram_bytes;
7651
7652 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7653 type = BTRFS_ORDERED_PREALLOC;
7654 else
7655 type = BTRFS_ORDERED_NOCOW;
7656 len = min(len, em->len - (start - em->start));
7657 block_start = em->block_start + (start - em->start);
7658
7659 if (can_nocow_extent(inode, start, &len, &orig_start,
7660 &orig_block_len, &ram_bytes) == 1 &&
7661 btrfs_inc_nocow_writers(fs_info, block_start)) {
7662 struct extent_map *em2;
7663
7664 em2 = btrfs_create_dio_extent(inode, start, len,
7665 orig_start, block_start,
7666 len, orig_block_len,
7667 ram_bytes, type);
7668 btrfs_dec_nocow_writers(fs_info, block_start);
7669 if (type == BTRFS_ORDERED_PREALLOC) {
7670 free_extent_map(em);
7671 *map = em = em2;
7672 }
7673
7674 if (em2 && IS_ERR(em2)) {
7675 ret = PTR_ERR(em2);
7676 goto out;
7677 }
7678 /*
7679 * For inode marked NODATACOW or extent marked PREALLOC,
7680 * use the existing or preallocated extent, so does not
7681 * need to adjust btrfs_space_info's bytes_may_use.
7682 */
7683 btrfs_free_reserved_data_space_noquota(inode, start,
7684 len);
7685 goto skip_cow;
7686 }
7687 }
7688
7689 /* this will cow the extent */
7690 len = bh_result->b_size;
7691 free_extent_map(em);
7692 *map = em = btrfs_new_extent_direct(inode, start, len);
7693 if (IS_ERR(em)) {
7694 ret = PTR_ERR(em);
7695 goto out;
7696 }
7697
7698 len = min(len, em->len - (start - em->start));
7699
7700skip_cow:
7701 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7702 inode->i_blkbits;
7703 bh_result->b_size = len;
7704 bh_result->b_bdev = em->bdev;
7705 set_buffer_mapped(bh_result);
7706
7707 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7708 set_buffer_new(bh_result);
7709
7710 /*
7711 * Need to update the i_size under the extent lock so buffered
7712 * readers will get the updated i_size when we unlock.
7713 */
7714 if (!dio_data->overwrite && start + len > i_size_read(inode))
7715 i_size_write(inode, start + len);
7716
7717 WARN_ON(dio_data->reserve < len);
7718 dio_data->reserve -= len;
7719 dio_data->unsubmitted_oe_range_end = start + len;
7720 current->journal_info = dio_data;
7721out:
7722 return ret;
7723}
7724
Josef Bacik4b46fce2010-05-23 11:00:55 -04007725static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7726 struct buffer_head *bh_result, int create)
7727{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007728 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007729 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007730 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307731 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007732 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007733 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007734 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007735 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007736 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007737
Miao Xie172a5042013-02-21 02:48:22 -07007738 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007739 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007740 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007741 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007742
Josef Bacikc3298612012-08-03 16:49:19 -04007743 lockstart = start;
7744 lockend = start + len - 1;
7745
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007746 if (current->journal_info) {
7747 /*
7748 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007749 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007750 * confused.
7751 */
chandan50745b02015-08-28 21:10:13 +05307752 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007753 current->journal_info = NULL;
7754 }
7755
Josef Bacikeb838e72012-07-31 16:28:48 -04007756 /*
7757 * If this errors out it's because we couldn't invalidate pagecache for
7758 * this range and we need to fallback to buffered.
7759 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007760 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7761 create)) {
7762 ret = -ENOTBLK;
7763 goto err;
7764 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007765
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007766 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007767 if (IS_ERR(em)) {
7768 ret = PTR_ERR(em);
7769 goto unlock_err;
7770 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007771
7772 /*
7773 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7774 * io. INLINE is special, and we could probably kludge it in here, but
7775 * it's still buffered so for safety lets just fall back to the generic
7776 * buffered path.
7777 *
7778 * For COMPRESSED we _have_ to read the entire extent in so we can
7779 * decompress it, so there will be buffering required no matter what we
7780 * do, so go ahead and fallback to buffered.
7781 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007782 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007783 * to buffered IO. Don't blame me, this is the price we pay for using
7784 * the generic code.
7785 */
7786 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7787 em->block_start == EXTENT_MAP_INLINE) {
7788 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007789 ret = -ENOTBLK;
7790 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007791 }
7792
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007793 if (create) {
7794 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7795 dio_data, start, len);
7796 if (ret < 0)
7797 goto unlock_err;
7798
7799 /* clear and unlock the entire range */
7800 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7801 unlock_bits, 1, 0, &cached_state);
7802 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007803 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7804 start, len);
7805 /* Can be negative only if we read from a hole */
7806 if (ret < 0) {
7807 ret = 0;
7808 free_extent_map(em);
7809 goto unlock_err;
7810 }
7811 /*
7812 * We need to unlock only the end area that we aren't using.
7813 * The rest is going to be unlocked by the endio routine.
7814 */
7815 lockstart = start + bh_result->b_size;
7816 if (lockstart < lockend) {
7817 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7818 lockend, unlock_bits, 1, 0,
7819 &cached_state);
7820 } else {
7821 free_extent_state(cached_state);
7822 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007823 }
7824
Josef Bacik4b46fce2010-05-23 11:00:55 -04007825 free_extent_map(em);
7826
7827 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007828
7829unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007830 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007831 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007832err:
chandan50745b02015-08-28 21:10:13 +05307833 if (dio_data)
7834 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007835 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007836}
7837
Omar Sandoval58efbc92017-08-22 23:45:59 -07007838static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7839 struct bio *bio,
7840 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007841{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007842 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007843 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007844
Mike Christie37226b22016-06-05 14:31:52 -05007845 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007846
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007847 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007848 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007849 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007850
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007851 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007852
Miao Xie8b110e32014-09-12 18:44:03 +08007853 return ret;
7854}
7855
7856static int btrfs_check_dio_repairable(struct inode *inode,
7857 struct bio *failed_bio,
7858 struct io_failure_record *failrec,
7859 int failed_mirror)
7860{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007861 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007862 int num_copies;
7863
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007864 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007865 if (num_copies == 1) {
7866 /*
7867 * we only have a single copy of the data, so don't bother with
7868 * all the retry and error correction code that follows. no
7869 * matter what the error is, it is very likely to persist.
7870 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007871 btrfs_debug(fs_info,
7872 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7873 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007874 return 0;
7875 }
7876
7877 failrec->failed_mirror = failed_mirror;
7878 failrec->this_mirror++;
7879 if (failrec->this_mirror == failed_mirror)
7880 failrec->this_mirror++;
7881
7882 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007883 btrfs_debug(fs_info,
7884 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7885 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007886 return 0;
7887 }
7888
7889 return 1;
7890}
7891
Omar Sandoval58efbc92017-08-22 23:45:59 -07007892static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7893 struct page *page, unsigned int pgoff,
7894 u64 start, u64 end, int failed_mirror,
7895 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007896{
7897 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007898 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7899 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007900 struct bio *bio;
7901 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007902 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007903 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007904 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007905 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007906 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007907
Mike Christie37226b22016-06-05 14:31:52 -05007908 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007909
7910 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7911 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007912 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007913
7914 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7915 failed_mirror);
7916 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007917 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007918 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007919 }
7920
Liu Bo17347ce2017-05-15 15:33:27 -07007921 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007922 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007923 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007924 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007925 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007926
7927 isector = start - btrfs_io_bio(failed_bio)->logical;
7928 isector >>= inode->i_sb->s_blocksize_bits;
7929 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307930 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007931 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007932
7933 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007934 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007935 read_mode, failrec->this_mirror, failrec->in_validation);
7936
Omar Sandoval58efbc92017-08-22 23:45:59 -07007937 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7938 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007939 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007940 bio_put(bio);
7941 }
7942
Omar Sandoval58efbc92017-08-22 23:45:59 -07007943 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007944}
7945
7946struct btrfs_retry_complete {
7947 struct completion done;
7948 struct inode *inode;
7949 u64 start;
7950 int uptodate;
7951};
7952
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007953static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007954{
7955 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007956 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007957 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007958 struct extent_io_tree *io_tree, *failure_tree;
Ming Lei6dc4f102019-02-15 19:13:19 +08007959 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007960
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007961 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007962 goto end;
7963
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307964 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007965 io_tree = &BTRFS_I(inode)->io_tree;
7966 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007967 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307968
Miao Xie8b110e32014-09-12 18:44:03 +08007969 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007970 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007971 bio_for_each_segment_all(bvec, bio, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007972 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7973 io_tree, done->start, bvec->bv_page,
7974 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007975end:
7976 complete(&done->done);
7977 bio_put(bio);
7978}
7979
Omar Sandoval58efbc92017-08-22 23:45:59 -07007980static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7981 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007982{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307983 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007984 struct bio_vec bvec;
7985 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007986 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007987 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307988 unsigned int pgoff;
7989 u32 sectorsize;
7990 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007991 blk_status_t ret;
7992 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007993
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307994 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007995 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307996
Miao Xiec1dc0892014-09-12 18:43:56 +08007997 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007998 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007999 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008000
Liu Bo17347ce2017-05-15 15:33:27 -07008001 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8002 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8003 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308004
8005next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008006 done.uptodate = 0;
8007 done.start = start;
8008 init_completion(&done.done);
8009
Liu Bo17347ce2017-05-15 15:33:27 -07008010 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308011 pgoff, start, start + sectorsize - 1,
8012 io_bio->mirror_num,
8013 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008014 if (ret) {
8015 err = ret;
8016 goto next;
8017 }
Miao Xie8b110e32014-09-12 18:44:03 +08008018
David Sterba9c17f6c2017-07-19 19:26:45 +02008019 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008020
8021 if (!done.uptodate) {
8022 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308023 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008024 }
8025
Liu Bo629ebf42017-05-15 17:20:07 -07008026next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308027 start += sectorsize;
8028
Liu Bo97bf5a52017-04-07 13:11:10 -07008029 nr_sectors--;
8030 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308031 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008032 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308033 goto next_block_or_try_again;
8034 }
Miao Xie8b110e32014-09-12 18:44:03 +08008035 }
8036
Liu Bo629ebf42017-05-15 17:20:07 -07008037 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008038}
8039
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008040static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008041{
8042 struct btrfs_retry_complete *done = bio->bi_private;
8043 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008044 struct extent_io_tree *io_tree, *failure_tree;
8045 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008046 struct bio_vec *bvec;
8047 int uptodate;
8048 int ret;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008049 int i = 0;
Ming Lei6dc4f102019-02-15 19:13:19 +08008050 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08008051
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008052 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008053 goto end;
8054
8055 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308056
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308057 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008058 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308059
Josef Bacik7870d082017-05-05 11:57:15 -04008060 io_tree = &BTRFS_I(inode)->io_tree;
8061 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8062
David Sterbac09abff2017-07-13 18:10:07 +02008063 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008064 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04008065 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8066 bvec->bv_offset, done->start,
8067 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008068 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008069 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8070 failure_tree, io_tree, done->start,
8071 bvec->bv_page,
8072 btrfs_ino(BTRFS_I(inode)),
8073 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008074 else
8075 uptodate = 0;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008076 i++;
Miao Xie8b110e32014-09-12 18:44:03 +08008077 }
8078
8079 done->uptodate = uptodate;
8080end:
8081 complete(&done->done);
8082 bio_put(bio);
8083}
8084
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008085static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8086 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008087{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308088 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008089 struct bio_vec bvec;
8090 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008091 struct btrfs_retry_complete done;
8092 u64 start;
8093 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308094 u32 sectorsize;
8095 int nr_sectors;
8096 unsigned int pgoff;
8097 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008098 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008099 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008100 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008101
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008103 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308104
Omar Sandoval58efbc92017-08-22 23:45:59 -07008105 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008106 start = io_bio->logical;
8107 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008108 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008109
Liu Bo17347ce2017-05-15 15:33:27 -07008110 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8111 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308112
Liu Bo17347ce2017-05-15 15:33:27 -07008113 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308114next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008115 if (uptodate) {
8116 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8117 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8118 bvec.bv_page, pgoff, start, sectorsize);
8119 if (likely(!ret))
8120 goto next;
8121 }
Miao Xie8b110e32014-09-12 18:44:03 +08008122try_again:
8123 done.uptodate = 0;
8124 done.start = start;
8125 init_completion(&done.done);
8126
Omar Sandoval58efbc92017-08-22 23:45:59 -07008127 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8128 pgoff, start, start + sectorsize - 1,
8129 io_bio->mirror_num, btrfs_retry_endio,
8130 &done);
8131 if (status) {
8132 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008133 goto next;
8134 }
8135
David Sterba9c17f6c2017-07-19 19:26:45 +02008136 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008137
8138 if (!done.uptodate) {
8139 /* We might have another mirror, so try again */
8140 goto try_again;
8141 }
8142next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308143 offset += sectorsize;
8144 start += sectorsize;
8145
8146 ASSERT(nr_sectors);
8147
Liu Bo97bf5a52017-04-07 13:11:10 -07008148 nr_sectors--;
8149 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308150 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008151 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308152 goto next_block;
8153 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008154 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008155
8156 return err;
8157}
8158
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008159static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8160 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008161{
8162 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8163
8164 if (skip_csum) {
8165 if (unlikely(err))
8166 return __btrfs_correct_data_nocsum(inode, io_bio);
8167 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008168 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008169 } else {
8170 return __btrfs_subio_endio_read(inode, io_bio, err);
8171 }
8172}
8173
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008174static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008175{
8176 struct btrfs_dio_private *dip = bio->bi_private;
8177 struct inode *inode = dip->inode;
8178 struct bio *dio_bio;
8179 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008180 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008181
Liu Bo99c4e3b92017-09-15 15:06:51 -06008182 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008183 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008184
Josef Bacik4b46fce2010-05-23 11:00:55 -04008185 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008186 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008187 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008188
Josef Bacik4b46fce2010-05-23 11:00:55 -04008189 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008190
Liu Bo99c4e3b92017-09-15 15:06:51 -06008191 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008192 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008193 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008194 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008195}
8196
Qu Wenruo524272602017-03-08 10:25:52 +08008197static void __endio_write_update_ordered(struct inode *inode,
8198 const u64 offset, const u64 bytes,
8199 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008200{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008201 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008202 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008203 struct btrfs_workqueue *wq;
8204 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008205 u64 ordered_offset = offset;
8206 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008207 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008208
Qu Wenruo524272602017-03-08 10:25:52 +08008209 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8210 wq = fs_info->endio_freespace_worker;
8211 func = btrfs_freespace_write_helper;
8212 } else {
8213 wq = fs_info->endio_write_workers;
8214 func = btrfs_endio_write_helper;
8215 }
8216
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008217 while (ordered_offset < offset + bytes) {
8218 last_offset = ordered_offset;
8219 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8220 &ordered_offset,
8221 ordered_bytes,
8222 uptodate)) {
8223 btrfs_init_work(&ordered->work, func,
8224 finish_ordered_fn,
8225 NULL, NULL);
8226 btrfs_queue_work(wq, &ordered->work);
8227 }
8228 /*
8229 * If btrfs_dec_test_ordered_pending does not find any ordered
8230 * extent in the range, we can exit.
8231 */
8232 if (ordered_offset == last_offset)
8233 return;
8234 /*
8235 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008236 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008237 */
8238 if (ordered_offset < offset + bytes) {
8239 ordered_bytes = offset + bytes - ordered_offset;
8240 ordered = NULL;
8241 }
Chris Mason163cf092010-11-28 19:56:33 -05008242 }
Filipe Manana14543772015-11-24 16:23:54 +00008243}
8244
8245static void btrfs_endio_direct_write(struct bio *bio)
8246{
8247 struct btrfs_dio_private *dip = bio->bi_private;
8248 struct bio *dio_bio = dip->dio_bio;
8249
Qu Wenruo524272602017-03-08 10:25:52 +08008250 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008251 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008252
Josef Bacik4b46fce2010-05-23 11:00:55 -04008253 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008254
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008255 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008256 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008257 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008258}
8259
David Sterbad0ee3932018-03-08 14:35:48 +01008260static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008261 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008262{
Josef Bacikc6100a42017-05-05 11:57:13 -04008263 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008264 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008265 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008266 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008267 return 0;
8268}
8269
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008270static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008271{
8272 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008273 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008274
Miao Xie8b110e32014-09-12 18:44:03 +08008275 if (err)
8276 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008277 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008278 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8279 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008280 (unsigned long long)bio->bi_iter.bi_sector,
8281 bio->bi_iter.bi_size, err);
8282
8283 if (dip->subio_endio)
8284 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008285
8286 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008287 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008288 * We want to perceive the errors flag being set before
8289 * decrementing the reference count. We don't need a barrier
8290 * since atomic operations with a return value are fully
8291 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008292 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008293 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008294 }
8295
8296 /* if there are more bios still pending for this dio, just exit */
8297 if (!atomic_dec_and_test(&dip->pending_bios))
8298 goto out;
8299
Chris Mason9be33952013-05-17 18:30:14 -04008300 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008301 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008302 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008303 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008304 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008305 }
8306out:
8307 bio_put(bio);
8308}
8309
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008310static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008311 struct btrfs_dio_private *dip,
8312 struct bio *bio,
8313 u64 file_offset)
8314{
8315 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8316 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008317 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008318
8319 /*
8320 * We load all the csum data we need when we submit
8321 * the first bio to reduce the csum tree search and
8322 * contention.
8323 */
8324 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008325 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008326 file_offset);
8327 if (ret)
8328 return ret;
8329 }
8330
8331 if (bio == dip->orig_bio)
8332 return 0;
8333
8334 file_offset -= dip->logical_offset;
8335 file_offset >>= inode->i_sb->s_blocksize_bits;
8336 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8337
8338 return 0;
8339}
8340
David Sterbad0ee3932018-03-08 14:35:48 +01008341static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8342 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008343{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008344 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008345 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008346 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008347 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008348
Liu Bo4c274bc2017-11-01 17:19:27 -06008349 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008350 if (async_submit)
8351 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8352
Josef Bacik5fd02042012-05-02 14:00:54 -04008353 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008354 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008355 if (ret)
8356 goto err;
8357 }
Miao Xiee65e1532010-11-22 03:04:43 +00008358
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008359 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008360 goto map;
8361
8362 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008363 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8364 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008365 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008366 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008367 } else if (write) {
8368 /*
8369 * If we aren't doing async submit, calculate the csum of the
8370 * bio now.
8371 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008372 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008373 if (ret)
8374 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008375 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008376 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008377 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008378 if (ret)
8379 goto err;
8380 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008381map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008382 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008383err:
Miao Xiee65e1532010-11-22 03:04:43 +00008384 return ret;
8385}
8386
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008387static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008388{
8389 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008390 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008391 struct bio *bio;
8392 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008393 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008394 u64 file_offset = dip->logical_offset;
Josef Bacik1ae39932011-04-06 14:41:34 -04008395 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008396 u64 submit_len;
8397 int clone_offset = 0;
8398 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308399 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008400 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008401 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00008402
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008403 submit_len = orig_bio->bi_iter.bi_size;
8404 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8405 start_sector << 9, submit_len, &geom);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008406 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008407 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008408
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008409 if (geom.len >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008410 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008411 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008412 goto submit;
8413 }
8414
David Woodhouse53b381b2013-01-29 18:40:14 -05008415 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008416 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008417 async_submit = 0;
8418 else
8419 async_submit = 1;
8420
Liu Bo725130b2017-05-16 09:51:39 -07008421 /* bio split */
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008422 ASSERT(geom.len <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008423 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008424 do {
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008425 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008426
Liu Bo725130b2017-05-16 09:51:39 -07008427 /*
8428 * This will never fail as it's passing GPF_NOFS and
8429 * the allocation is backed by btrfs_bioset.
8430 */
Liu Boe4770942017-05-16 10:57:14 -07008431 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008432 clone_len);
8433 bio->bi_private = dip;
8434 bio->bi_end_io = btrfs_end_dio_bio;
8435 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008436
Liu Bo725130b2017-05-16 09:51:39 -07008437 ASSERT(submit_len >= clone_len);
8438 submit_len -= clone_len;
8439 if (submit_len == 0)
8440 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008441
Liu Bo725130b2017-05-16 09:51:39 -07008442 /*
8443 * Increase the count before we submit the bio so we know
8444 * the end IO handler won't happen before we increase the
8445 * count. Otherwise, the dip might get freed before we're
8446 * done setting it up.
8447 */
8448 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008449
David Sterbad0ee3932018-03-08 14:35:48 +01008450 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008451 async_submit);
8452 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008453 bio_put(bio);
8454 atomic_dec(&dip->pending_bios);
8455 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008456 }
Liu Bo725130b2017-05-16 09:51:39 -07008457
8458 clone_offset += clone_len;
8459 start_sector += clone_len >> 9;
8460 file_offset += clone_len;
8461
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008462 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8463 start_sector << 9, submit_len, &geom);
Liu Bo725130b2017-05-16 09:51:39 -07008464 if (ret)
8465 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008466 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008467
Josef Bacik02f57c72011-04-06 14:25:44 -04008468submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008469 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008470 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008471 return 0;
8472
8473 bio_put(bio);
8474out_err:
8475 dip->errors = 1;
8476 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008477 * Before atomic variable goto zero, we must make sure dip->errors is
8478 * perceived to be set. This ordering is ensured by the fact that an
8479 * atomic operations with a return value are fully ordered as per
8480 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008481 */
Miao Xiee65e1532010-11-22 03:04:43 +00008482 if (atomic_dec_and_test(&dip->pending_bios))
8483 bio_io_error(dip->orig_bio);
8484
8485 /* bio_end_io() will handle error, so we needn't return it */
8486 return 0;
8487}
8488
Mike Christie8a4c1e42016-06-05 14:31:50 -05008489static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8490 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008491{
Filipe Manana61de7182015-07-01 12:13:10 +01008492 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008493 struct bio *bio = NULL;
8494 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008495 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008496 int ret = 0;
8497
David Sterba8b6c1d52017-06-02 17:48:13 +02008498 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008499
Miao Xiec1dc0892014-09-12 18:43:56 +08008500 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008501 if (!dip) {
8502 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008503 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008504 }
8505
8506 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008507 dip->inode = inode;
8508 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008509 dip->bytes = dio_bio->bi_iter.bi_size;
8510 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008511 bio->bi_private = dip;
8512 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008513 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008514 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008515 io_bio = btrfs_io_bio(bio);
8516 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008517
Miao Xiec1dc0892014-09-12 18:43:56 +08008518 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008519 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008520 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008521 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008522 dip->subio_endio = btrfs_subio_endio_read;
8523 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008524
Filipe Mananaf28a4922015-12-08 19:23:20 +00008525 /*
8526 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8527 * even if we fail to submit a bio, because in such case we do the
8528 * corresponding error handling below and it must not be done a second
8529 * time by btrfs_direct_IO().
8530 */
8531 if (write) {
8532 struct btrfs_dio_data *dio_data = current->journal_info;
8533
8534 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8535 dip->bytes;
8536 dio_data->unsubmitted_oe_range_start =
8537 dio_data->unsubmitted_oe_range_end;
8538 }
8539
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008540 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008541 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008542 return;
Chris Mason9be33952013-05-17 18:30:14 -04008543
David Sterbab3a0dd52018-11-22 17:16:49 +01008544 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008545
Josef Bacik4b46fce2010-05-23 11:00:55 -04008546free_ordered:
8547 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008548 * If we arrived here it means either we failed to submit the dip
8549 * or we either failed to clone the dio_bio or failed to allocate the
8550 * dip. If we cloned the dio_bio and allocated the dip, we can just
8551 * call bio_endio against our io_bio so that we get proper resource
8552 * cleanup if we fail to submit the dip, otherwise, we must do the
8553 * same as btrfs_endio_direct_[write|read] because we can't call these
8554 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008555 */
Liu Bo3892ac92017-04-17 15:00:28 -07008556 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008557 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008558 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008559 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008560 * and all the cleanup and final put for dio_bio (through
8561 * dio_end_io()).
8562 */
8563 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008564 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008565 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008566 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008567 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008568 file_offset,
8569 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008570 false);
Filipe Manana14543772015-11-24 16:23:54 +00008571 else
Filipe Manana61de7182015-07-01 12:13:10 +01008572 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8573 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008574
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008575 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008576 /*
8577 * Releases and cleans up our dio_bio, no need to bio_put()
8578 * nor bio_endio()/bio_io_error() against dio_bio.
8579 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008580 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008581 }
Liu Bo3892ac92017-04-17 15:00:28 -07008582 if (bio)
8583 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008584 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008585}
8586
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008587static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008588 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008589{
8590 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008591 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008592 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008593 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008594
8595 if (offset & blocksize_mask)
8596 goto out;
8597
Al Viro28060d52014-03-22 05:15:17 -04008598 if (iov_iter_alignment(iter) & blocksize_mask)
8599 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008600
Al Viro28060d52014-03-22 05:15:17 -04008601 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008602 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008603 return 0;
8604 /*
8605 * Check to make sure we don't have duplicate iov_base's in this
8606 * iovec, if so return EINVAL, otherwise we'll get csum errors
8607 * when reading back.
8608 */
8609 for (seg = 0; seg < iter->nr_segs; seg++) {
8610 for (i = seg + 1; i < iter->nr_segs; i++) {
8611 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008612 goto out;
8613 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008614 }
8615 retval = 0;
8616out:
8617 return retval;
8618}
Josef Bacikeb838e72012-07-31 16:28:48 -04008619
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008620static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008621{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008622 struct file *file = iocb->ki_filp;
8623 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008624 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308625 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008626 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008627 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008628 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008629 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008630 bool wakeup = true;
8631 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008632 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008633
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008634 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008635 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008636
Jens Axboefe0f07d2015-04-15 17:05:48 -06008637 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008638
Josef Bacik0e267c42013-07-02 10:38:02 -04008639 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008640 * The generic stuff only does filemap_write_and_wait_range, which
8641 * isn't enough if we've written compressed pages to this area, so
8642 * we need to flush the dirty pages again to make absolutely sure
8643 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008644 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008645 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008646 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8647 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008648 filemap_fdatawrite_range(inode->i_mapping, offset,
8649 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008650
Omar Sandoval6f673762015-03-16 04:33:52 -07008651 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008652 /*
8653 * If the write DIO is beyond the EOF, we need update
8654 * the isize, but it is protected by i_mutex. So we can
8655 * not unlock the i_mutex at this case.
8656 */
8657 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008658 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008659 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008660 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008661 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8662 ret = -EAGAIN;
8663 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008664 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008665 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8666 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008667 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008668 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008669
8670 /*
8671 * We need to know how many extents we reserved so that we can
8672 * do the accounting properly if we go over the number we
8673 * originally calculated. Abuse current->journal_info for this.
8674 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008675 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008676 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008677 dio_data.unsubmitted_oe_range_start = (u64)offset;
8678 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308679 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308680 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008681 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8682 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008683 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008684 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8685 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008686 }
8687
Omar Sandoval17f8c842015-03-16 04:33:50 -07008688 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008689 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008690 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008691 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008692 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308693 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008694 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008695 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308696 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008697 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008698 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008699 /*
8700 * On error we might have left some ordered extents
8701 * without submitting corresponding bios for them, so
8702 * cleanup them up to avoid other tasks getting them
8703 * and waiting for them to complete forever.
8704 */
8705 if (dio_data.unsubmitted_oe_range_start <
8706 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008707 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008708 dio_data.unsubmitted_oe_range_start,
8709 dio_data.unsubmitted_oe_range_end -
8710 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008711 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008712 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008713 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008714 offset, count - (size_t)ret, true);
8715 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008716 }
Miao Xie38851cc2013-02-08 07:04:11 +00008717out:
Miao Xie2e60a512013-02-08 07:01:08 +00008718 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008719 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008720 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008721 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008722
Qu Wenruo364ecf32017-02-27 15:10:38 +08008723 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008724 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008725}
8726
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008727#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8728
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008729static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8730 __u64 start, __u64 len)
8731{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008732 int ret;
8733
8734 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8735 if (ret)
8736 return ret;
8737
David Sterba2135fb92017-06-23 04:09:57 +02008738 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008739}
8740
Chris Mason9ebefb182007-06-15 13:50:00 -04008741int btrfs_readpage(struct file *file, struct page *page)
8742{
Chris Masond1310b22008-01-24 16:13:08 -05008743 struct extent_io_tree *tree;
8744 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008745 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008746}
Chris Mason1832a6d2007-12-21 16:27:21 -05008747
Chris Mason39279cc2007-06-12 06:35:45 -04008748static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8749{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008750 struct inode *inode = page->mapping->host;
8751 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008752
8753 if (current->flags & PF_MEMALLOC) {
8754 redirty_page_for_writepage(wbc, page);
8755 unlock_page(page);
8756 return 0;
8757 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008758
8759 /*
8760 * If we are under memory pressure we will call this directly from the
8761 * VM, we need to make sure we have the inode referenced for the ordered
8762 * extent. If not just return like we didn't do anything.
8763 */
8764 if (!igrab(inode)) {
8765 redirty_page_for_writepage(wbc, page);
8766 return AOP_WRITEPAGE_ACTIVATE;
8767 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008768 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008769 btrfs_add_delayed_iput(inode);
8770 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008771}
Chris Mason39279cc2007-06-12 06:35:45 -04008772
Eric Sandeen48a3b632013-04-25 20:41:01 +00008773static int btrfs_writepages(struct address_space *mapping,
8774 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008775{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008776 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008777}
8778
Chris Mason3ab2fb52007-11-08 10:59:22 -05008779static int
8780btrfs_readpages(struct file *file, struct address_space *mapping,
8781 struct list_head *pages, unsigned nr_pages)
8782{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008783 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008784}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008785
Chris Masone6dcd2d2008-07-17 12:53:50 -04008786static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008787{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008788 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008789 if (ret == 1) {
8790 ClearPagePrivate(page);
8791 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008792 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008793 }
8794 return ret;
8795}
Chris Mason39279cc2007-06-12 06:35:45 -04008796
Chris Masone6dcd2d2008-07-17 12:53:50 -04008797static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8798{
Chris Mason98509cf2008-09-11 15:51:43 -04008799 if (PageWriteback(page) || PageDirty(page))
8800 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008801 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008802}
8803
Lukas Czernerd47992f2013-05-21 23:17:23 -04008804static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8805 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008806{
Josef Bacik5fd02042012-05-02 14:00:54 -04008807 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008808 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008809 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008810 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008811 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008812 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308813 u64 start;
8814 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008815 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008816
Chris Mason8b62b722009-09-02 16:53:46 -04008817 /*
8818 * we have the page locked, so new writeback can't start,
8819 * and the dirty bit won't be cleared while we are here.
8820 *
8821 * Wait for IO on this page so that we can safely clear
8822 * the PagePrivate2 bit and do ordered accounting
8823 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008824 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008825
Josef Bacik5fd02042012-05-02 14:00:54 -04008826 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008827 if (offset) {
8828 btrfs_releasepage(page, GFP_NOFS);
8829 return;
8830 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008831
8832 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008833 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308834again:
8835 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008836 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308837 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008838 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308839 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008840 /*
8841 * IO on this page will never be started, so we need
8842 * to account for any ordered extents now
8843 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008844 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308845 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008846 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008847 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008848 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008849 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008850 /*
8851 * whoever cleared the private bit is responsible
8852 * for the finish_ordered_io
8853 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008854 if (TestClearPagePrivate2(page)) {
8855 struct btrfs_ordered_inode_tree *tree;
8856 u64 new_len;
8857
8858 tree = &BTRFS_I(inode)->ordered_tree;
8859
8860 spin_lock_irq(&tree->lock);
8861 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308862 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008863 if (new_len < ordered->truncated_len)
8864 ordered->truncated_len = new_len;
8865 spin_unlock_irq(&tree->lock);
8866
8867 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308868 start,
8869 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008870 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008871 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008872 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008873 if (!inode_evicting) {
8874 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308875 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008876 &cached_state);
8877 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308878
8879 start = end + 1;
8880 if (start < page_end)
8881 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008882 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008883
Qu Wenruob9d0b382015-09-29 10:35:16 +08008884 /*
8885 * Qgroup reserved space handler
8886 * Page here will be either
8887 * 1) Already written to disk
8888 * In this case, its reserved space is released from data rsv map
8889 * and will be freed by delayed_ref handler finally.
8890 * So even we call qgroup_free_data(), it won't decrease reserved
8891 * space.
8892 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008893 * This means the reserved space should be freed here. However,
8894 * if a truncate invalidates the page (by clearing PageDirty)
8895 * and the page is accounted for while allocating extent
8896 * in btrfs_check_data_free_space() we let delayed_ref to
8897 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008898 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008899 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008900 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008901 if (!inode_evicting) {
8902 clear_extent_bit(tree, page_start, page_end,
8903 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008904 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8905 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008906 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008907
8908 __btrfs_releasepage(page, GFP_NOFS);
8909 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008910
Chris Mason4a096752008-07-21 10:29:44 -04008911 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008912 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008913 ClearPagePrivate(page);
8914 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008915 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008916 }
Chris Mason39279cc2007-06-12 06:35:45 -04008917}
8918
Chris Mason9ebefb182007-06-15 13:50:00 -04008919/*
8920 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8921 * called from a page fault handler when a page is first dirtied. Hence we must
8922 * be careful to check for EOF conditions here. We set the page up correctly
8923 * for a written page which means we get ENOSPC checking when writing into
8924 * holes and correct delalloc and unwritten extent mapping on filesystems that
8925 * support these features.
8926 *
8927 * We are not allowed to take the i_mutex here so we have to play games to
8928 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008929 * truncate_setsize() writes the inode size before removing pages, once we have
8930 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008931 * beyond EOF, then the page is guaranteed safe against truncation until we
8932 * unlock the page.
8933 */
Souptick Joardera528a242018-06-06 19:54:44 +05308934vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008935{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008936 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008937 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008938 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008939 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8940 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008941 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008942 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008943 char *kaddr;
8944 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008945 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308946 vm_fault_t ret;
8947 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008948 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308949 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008950 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008951 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308952 u64 end;
8953
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008954 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008955
Jan Karab2b5ef52012-06-12 16:20:45 +02008956 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008957 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008958 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308959 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008960
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308961 /*
8962 * Reserving delalloc space after obtaining the page lock can lead to
8963 * deadlock. For example, if a dirty page is locked by this function
8964 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8965 * dirty page write out, then the btrfs_writepage() function could
8966 * end up waiting indefinitely to get a lock on the page currently
8967 * being processed by btrfs_page_mkwrite() function.
8968 */
Souptick Joardera528a242018-06-06 19:54:44 +05308969 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308970 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308971 if (!ret2) {
8972 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008973 reserved = 1;
8974 }
Souptick Joardera528a242018-06-06 19:54:44 +05308975 if (ret2) {
8976 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008977 if (reserved)
8978 goto out;
8979 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008980 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008981
Nick Piggin56a76f82009-03-31 15:23:23 -07008982 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008983again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008984 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008985 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008986
Chris Mason9ebefb182007-06-15 13:50:00 -04008987 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008988 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008989 /* page got truncated out from underneath us */
8990 goto out_unlock;
8991 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008992 wait_on_page_writeback(page);
8993
David Sterbaff13db42015-12-03 14:30:40 +01008994 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008995 set_page_extent_mapped(page);
8996
Chris Masoneb84ae02008-07-17 13:53:27 -04008997 /*
8998 * we can't set the delalloc bits if there are pending ordered
8999 * extents. Drop our locks and wait for them to finish
9000 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009001 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9002 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009003 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009004 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009005 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009006 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009007 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009008 btrfs_put_ordered_extent(ordered);
9009 goto again;
9010 }
9011
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009012 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009013 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009014 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009015 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309016 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009017 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08009018 page_start, PAGE_SIZE - reserved_space,
9019 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309020 }
9021 }
9022
Josef Bacikfbf19082009-10-01 17:10:23 -04009023 /*
Liu Bo54160342016-12-13 12:15:19 -08009024 * page_mkwrite gets called when the page is firstly dirtied after it's
9025 * faulted in, but write(2) could also dirty a page and set delalloc
9026 * bits, thus in this case for space account reason, we still need to
9027 * clear any delalloc bits within this page range since we have to
9028 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009029 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309030 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009031 EXTENT_DIRTY | EXTENT_DELALLOC |
9032 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009033 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009034
Souptick Joardera528a242018-06-06 19:54:44 +05309035 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03009036 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05309037 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009038 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009039 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009040 ret = VM_FAULT_SIGBUS;
9041 goto out_unlock;
9042 }
Souptick Joardera528a242018-06-06 19:54:44 +05309043 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009044
9045 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009046 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01009047 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04009048 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009049 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009050
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009051 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009052 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009053 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009054 flush_dcache_page(page);
9055 kunmap(page);
9056 }
Chris Mason247e7432008-07-17 12:53:51 -04009057 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009058 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009059 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009060
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009061 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009062 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009063 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009064
David Sterbae43bbe52017-12-12 21:43:52 +01009065 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009066
Souptick Joardera528a242018-06-06 19:54:44 +05309067 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08009068 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02009069 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009070 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009071 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009072 }
Chris Mason717beb92018-06-25 10:03:41 -07009073
9074out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04009075 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009076out:
Qu Wenruo43b18592017-12-12 15:34:32 +08009077 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08009078 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009079 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009080out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009081 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009082 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009083 return ret;
9084}
9085
Filipe Manana213e8c52018-02-06 20:40:31 +00009086static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009087{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009088 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009089 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009090 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009091 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009092 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009093 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04009094 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009095
Filipe Manana213e8c52018-02-06 20:40:31 +00009096 if (!skip_writeback) {
9097 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9098 (u64)-1);
9099 if (ret)
9100 return ret;
9101 }
Chris Mason39279cc2007-06-12 06:35:45 -04009102
Josef Bacikfcb80c22011-05-03 10:40:22 -04009103 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009104 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9105 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009106 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009107 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009108 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009109 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009110 * be free'd up by the truncate operation, but also have some slack
9111 * space reserved in case it uses space during the truncate (thank you
9112 * very much snapshotting).
9113 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009114 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009115 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009116 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009117 * doesn't end up using space reserved for updating the inode. We also
9118 * need to be able to stop the transaction and start a new one, which
9119 * means we need to be able to update the inode several times, and we
9120 * have no idea of knowing how many times that will be, so we can't just
9121 * reserve 1 item for the entirety of the operation, so that has to be
9122 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009123 *
9124 * So that leaves us with
9125 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009126 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009127 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009128 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009129 * updating the inode.
9130 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009131 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009132 if (!rsv)
9133 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009134 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009135 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009136
Josef Bacik907cbce2011-08-08 13:46:15 -04009137 /*
Josef Bacik07127182011-08-19 10:29:59 -04009138 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009139 * 1 for updating the inode.
9140 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009141 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009142 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009143 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009144 goto out;
9145 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009146
Josef Bacik907cbce2011-08-08 13:46:15 -04009147 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009148 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009149 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009150 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009151
Chris Mason5a3f23d2009-03-31 13:27:11 -04009152 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009153 * So if we truncate and then write and fsync we normally would just
9154 * write the extents that changed, which is a problem if we need to
9155 * first truncate that entire inode. So set this flag so we write out
9156 * all of the extents in the inode to the sync log so we're completely
9157 * safe.
9158 */
9159 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009160 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009161
Yan, Zheng80825102009-11-12 09:35:36 +00009162 while (1) {
9163 ret = btrfs_truncate_inode_items(trans, root, inode,
9164 inode->i_size,
9165 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009166 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009167 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009168 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009169
Yan, Zheng80825102009-11-12 09:35:36 +00009170 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009171 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009172 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009173
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009174 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009175 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009176
9177 trans = btrfs_start_transaction(root, 2);
9178 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009179 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009180 trans = NULL;
9181 break;
9182 }
9183
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009184 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009185 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009186 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009187 BUG_ON(ret); /* shouldn't happen */
9188 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009189 }
9190
Josef Bacikddfae632017-10-19 14:16:02 -04009191 /*
9192 * We can't call btrfs_truncate_block inside a trans handle as we could
9193 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9194 * we've truncated everything except the last little bit, and can do
9195 * btrfs_truncate_block and then update the disk_i_size.
9196 */
9197 if (ret == NEED_TRUNCATE_BLOCK) {
9198 btrfs_end_transaction(trans);
9199 btrfs_btree_balance_dirty(fs_info);
9200
9201 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9202 if (ret)
9203 goto out;
9204 trans = btrfs_start_transaction(root, 1);
9205 if (IS_ERR(trans)) {
9206 ret = PTR_ERR(trans);
9207 goto out;
9208 }
9209 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9210 }
9211
Chris Mason917c16b2011-11-08 14:49:59 -05009212 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009213 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009214
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009215 trans->block_rsv = &fs_info->trans_block_rsv;
9216 ret2 = btrfs_update_inode(trans, root, inode);
9217 if (ret2 && !ret)
9218 ret = ret2;
9219
9220 ret2 = btrfs_end_transaction(trans);
9221 if (ret2 && !ret)
9222 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009223 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009224 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009225out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009226 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009227
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009228 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009229}
9230
Sven Wegener3b963622008-06-09 21:57:42 -04009231/*
Chris Masond352ac62008-09-29 15:18:18 -04009232 * create a new subvolume directory/inode (helper for the ioctl).
9233 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009234int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009235 struct btrfs_root *new_root,
9236 struct btrfs_root *parent_root,
9237 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009238{
Chris Mason39279cc2007-06-12 06:35:45 -04009239 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009240 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009241 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009242
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009243 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9244 new_dirid, new_dirid,
9245 S_IFDIR | (~current_umask() & S_IRWXUGO),
9246 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009247 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009248 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009249 inode->i_op = &btrfs_dir_inode_operations;
9250 inode->i_fop = &btrfs_dir_file_operations;
9251
Miklos Szeredibfe86842011-10-28 14:13:29 +02009252 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009253 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009254 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009255
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009256 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9257 if (err)
9258 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009259 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009260 new_root->root_key.objectid, err);
9261
Yan, Zheng76dda932009-09-21 16:00:26 -04009262 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009263
Yan, Zheng76dda932009-09-21 16:00:26 -04009264 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009265 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009266}
9267
Chris Mason39279cc2007-06-12 06:35:45 -04009268struct inode *btrfs_alloc_inode(struct super_block *sb)
9269{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009270 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009271 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009272 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009273
David Sterba712e36c2017-10-31 17:08:27 +01009274 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009275 if (!ei)
9276 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009277
9278 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009279 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009280 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009281 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009282 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009283 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009284 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009285 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009286 ei->disk_i_size = 0;
9287 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009288 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009289 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009290 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009291 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009292 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009293
Josef Bacik9e0baf62011-07-15 15:16:44 +00009294 spin_lock_init(&ei->lock);
9295 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009296 if (sb->s_magic != BTRFS_TEST_MAGIC)
9297 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9298 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009299 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009300 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009301 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009302
Miao Xie16cdcec2011-04-22 18:12:22 +08009303 ei->delayed_node = NULL;
9304
chandan r9cc97d62012-07-04 12:48:07 +05309305 ei->i_otime.tv_sec = 0;
9306 ei->i_otime.tv_nsec = 0;
9307
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009308 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009309 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009310 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9311 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9312 IO_TREE_INODE_IO_FAILURE, inode);
David Sterba7b439732019-03-11 15:58:30 +01009313 ei->io_tree.track_uptodate = true;
9314 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009315 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009316 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009317 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009318 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009319 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009320 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009321 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009322 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009323
9324 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009325}
9326
Josef Bacikaaedb552013-10-11 14:44:09 -04009327#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9328void btrfs_test_destroy_inode(struct inode *inode)
9329{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009330 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009331 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9332}
9333#endif
9334
Al Viro26602ca2019-04-10 15:14:41 -04009335void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009336{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009337 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9338}
9339
Chris Mason39279cc2007-06-12 06:35:45 -04009340void btrfs_destroy_inode(struct inode *inode)
9341{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009342 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009343 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009344 struct btrfs_root *root = BTRFS_I(inode)->root;
9345
Al Virob3d9b7a2012-06-09 13:51:19 -04009346 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009347 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009348 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9349 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009350 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009351 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009352 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009353 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009354 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009355
Chris Mason5a3f23d2009-03-31 13:27:11 -04009356 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009357 * This can happen where we create an inode, but somebody else also
9358 * created the same inode and we need to destroy the one we already
9359 * created.
9360 */
9361 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009362 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009363
Chris Masond3977122009-01-05 21:25:51 -05009364 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009365 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9366 if (!ordered)
9367 break;
9368 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009369 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009370 "found ordered extent %llu %llu on inode cleanup",
9371 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009372 btrfs_remove_ordered_extent(inode, ordered);
9373 btrfs_put_ordered_extent(ordered);
9374 btrfs_put_ordered_extent(ordered);
9375 }
9376 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009377 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009378 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009379 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04009380}
9381
Al Viro45321ac2010-06-07 13:43:19 -04009382int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009383{
9384 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009385
Naohiro Aota6379ef92013-06-06 09:56:34 +00009386 if (root == NULL)
9387 return 1;
9388
Liu Bofa6ac872013-02-20 14:10:23 +00009389 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009390 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009391 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009392 else
Al Viro45321ac2010-06-07 13:43:19 -04009393 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009394}
9395
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009396static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009397{
9398 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9399
9400 inode_init_once(&ei->vfs_inode);
9401}
9402
David Sterbae67c7182018-02-19 17:24:18 +01009403void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009404{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009405 /*
9406 * Make sure all delayed rcu free inodes are flushed before we
9407 * destroy cache.
9408 */
9409 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009410 kmem_cache_destroy(btrfs_inode_cachep);
9411 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009412 kmem_cache_destroy(btrfs_path_cachep);
9413 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00009414 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009415}
9416
Liu Bof5c29bd2017-11-02 17:21:50 -06009417int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009418{
David Sterba837e1972012-09-07 03:00:48 -06009419 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009420 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009421 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9422 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009423 if (!btrfs_inode_cachep)
9424 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009425
David Sterba837e1972012-09-07 03:00:48 -06009426 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009427 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009428 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009429 if (!btrfs_trans_handle_cachep)
9430 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009431
David Sterba837e1972012-09-07 03:00:48 -06009432 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009433 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009434 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009435 if (!btrfs_path_cachep)
9436 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009437
David Sterba837e1972012-09-07 03:00:48 -06009438 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009439 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009440 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009441 if (!btrfs_free_space_cachep)
9442 goto fail;
9443
Christophe Leroy3acd4852019-08-21 15:05:55 +00009444 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9445 PAGE_SIZE, PAGE_SIZE,
9446 SLAB_RED_ZONE, NULL);
9447 if (!btrfs_free_space_bitmap_cachep)
9448 goto fail;
9449
Chris Mason39279cc2007-06-12 06:35:45 -04009450 return 0;
9451fail:
9452 btrfs_destroy_cachep();
9453 return -ENOMEM;
9454}
9455
David Howellsa528d352017-01-31 16:46:22 +00009456static int btrfs_getattr(const struct path *path, struct kstat *stat,
9457 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009458{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009459 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009460 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009461 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009462 u32 bi_flags = BTRFS_I(inode)->flags;
9463
9464 stat->result_mask |= STATX_BTIME;
9465 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9466 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9467 if (bi_flags & BTRFS_INODE_APPEND)
9468 stat->attributes |= STATX_ATTR_APPEND;
9469 if (bi_flags & BTRFS_INODE_COMPRESS)
9470 stat->attributes |= STATX_ATTR_COMPRESSED;
9471 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9472 stat->attributes |= STATX_ATTR_IMMUTABLE;
9473 if (bi_flags & BTRFS_INODE_NODUMP)
9474 stat->attributes |= STATX_ATTR_NODUMP;
9475
9476 stat->attributes_mask |= (STATX_ATTR_APPEND |
9477 STATX_ATTR_COMPRESSED |
9478 STATX_ATTR_IMMUTABLE |
9479 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009480
Chris Mason39279cc2007-06-12 06:35:45 -04009481 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009482 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009483
9484 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009485 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009486 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009487 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009488 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009489 return 0;
9490}
9491
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009492static int btrfs_rename_exchange(struct inode *old_dir,
9493 struct dentry *old_dentry,
9494 struct inode *new_dir,
9495 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009496{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009497 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009498 struct btrfs_trans_handle *trans;
9499 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009500 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009501 struct inode *new_inode = new_dentry->d_inode;
9502 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009503 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009504 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009505 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9506 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009507 u64 old_idx = 0;
9508 u64 new_idx = 0;
9509 u64 root_objectid;
9510 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009511 bool root_log_pinned = false;
9512 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009513 struct btrfs_log_ctx ctx_root;
9514 struct btrfs_log_ctx ctx_dest;
9515 bool sync_log_root = false;
9516 bool sync_log_dest = false;
9517 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009518
9519 /* we only allow rename subvolume link between subvolumes */
9520 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9521 return -EXDEV;
9522
Filipe Mananad4682ba2018-06-11 19:24:28 +01009523 btrfs_init_log_ctx(&ctx_root, old_inode);
9524 btrfs_init_log_ctx(&ctx_dest, new_inode);
9525
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009526 /* close the race window with snapshot create/destroy ioctl */
9527 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009528 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009529 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009530 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009531
9532 /*
9533 * We want to reserve the absolute worst case amount of items. So if
9534 * both inodes are subvols and we need to unlink them then that would
9535 * require 4 item modifications, but if they are both normal inodes it
9536 * would require 5 item modifications, so we'll assume their normal
9537 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9538 * should cover the worst case number of items we'll modify.
9539 */
9540 trans = btrfs_start_transaction(root, 12);
9541 if (IS_ERR(trans)) {
9542 ret = PTR_ERR(trans);
9543 goto out_notrans;
9544 }
9545
9546 /*
9547 * We need to find a free sequence number both in the source and
9548 * in the destination directory for the exchange.
9549 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009550 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009551 if (ret)
9552 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009553 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009554 if (ret)
9555 goto out_fail;
9556
9557 BTRFS_I(old_inode)->dir_index = 0ULL;
9558 BTRFS_I(new_inode)->dir_index = 0ULL;
9559
9560 /* Reference for the source. */
9561 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9562 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009563 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009564 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009565 btrfs_pin_log_trans(root);
9566 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567 ret = btrfs_insert_inode_ref(trans, dest,
9568 new_dentry->d_name.name,
9569 new_dentry->d_name.len,
9570 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009571 btrfs_ino(BTRFS_I(new_dir)),
9572 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009573 if (ret)
9574 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009575 }
9576
9577 /* And now for the dest. */
9578 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9579 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009580 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009581 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009582 btrfs_pin_log_trans(dest);
9583 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009584 ret = btrfs_insert_inode_ref(trans, root,
9585 old_dentry->d_name.name,
9586 old_dentry->d_name.len,
9587 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009588 btrfs_ino(BTRFS_I(old_dir)),
9589 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009590 if (ret)
9591 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009592 }
9593
9594 /* Update inode version and ctime/mtime. */
9595 inode_inc_iversion(old_dir);
9596 inode_inc_iversion(new_dir);
9597 inode_inc_iversion(old_inode);
9598 inode_inc_iversion(new_inode);
9599 old_dir->i_ctime = old_dir->i_mtime = ctime;
9600 new_dir->i_ctime = new_dir->i_mtime = ctime;
9601 old_inode->i_ctime = ctime;
9602 new_inode->i_ctime = ctime;
9603
9604 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009605 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9606 BTRFS_I(old_inode), 1);
9607 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9608 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009609 }
9610
9611 /* src is a subvolume */
9612 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9613 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009614 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009615 old_dentry->d_name.name,
9616 old_dentry->d_name.len);
9617 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009618 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9619 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 old_dentry->d_name.name,
9621 old_dentry->d_name.len);
9622 if (!ret)
9623 ret = btrfs_update_inode(trans, root, old_inode);
9624 }
9625 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009626 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009627 goto out_fail;
9628 }
9629
9630 /* dest is a subvolume */
9631 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9632 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009633 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009634 new_dentry->d_name.name,
9635 new_dentry->d_name.len);
9636 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009637 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9638 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009639 new_dentry->d_name.name,
9640 new_dentry->d_name.len);
9641 if (!ret)
9642 ret = btrfs_update_inode(trans, dest, new_inode);
9643 }
9644 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009645 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009646 goto out_fail;
9647 }
9648
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009649 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 new_dentry->d_name.name,
9651 new_dentry->d_name.len, 0, old_idx);
9652 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009653 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654 goto out_fail;
9655 }
9656
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009657 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009658 old_dentry->d_name.name,
9659 old_dentry->d_name.len, 0, new_idx);
9660 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009661 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009662 goto out_fail;
9663 }
9664
9665 if (old_inode->i_nlink == 1)
9666 BTRFS_I(old_inode)->dir_index = old_idx;
9667 if (new_inode->i_nlink == 1)
9668 BTRFS_I(new_inode)->dir_index = new_idx;
9669
Filipe Manana86e8aa02016-05-05 02:02:27 +01009670 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009671 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009672 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9673 BTRFS_I(old_dir), parent,
9674 false, &ctx_root);
9675 if (ret == BTRFS_NEED_LOG_SYNC)
9676 sync_log_root = true;
9677 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9678 commit_transaction = true;
9679 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009680 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009681 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009682 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009683 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009684 if (!commit_transaction) {
9685 parent = old_dentry->d_parent;
9686 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9687 BTRFS_I(new_dir), parent,
9688 false, &ctx_dest);
9689 if (ret == BTRFS_NEED_LOG_SYNC)
9690 sync_log_dest = true;
9691 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9692 commit_transaction = true;
9693 ret = 0;
9694 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009695 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009696 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009697 }
9698out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009699 /*
9700 * If we have pinned a log and an error happened, we unpin tasks
9701 * trying to sync the log and force them to fallback to a transaction
9702 * commit if the log currently contains any of the inodes involved in
9703 * this rename operation (to ensure we do not persist a log with an
9704 * inconsistent state for any of these inodes or leading to any
9705 * inconsistencies when replayed). If the transaction was aborted, the
9706 * abortion reason is propagated to userspace when attempting to commit
9707 * the transaction. If the log does not contain any of these inodes, we
9708 * allow the tasks to sync it.
9709 */
9710 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009711 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9712 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9713 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009714 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009715 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009716 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009717
9718 if (root_log_pinned) {
9719 btrfs_end_log_trans(root);
9720 root_log_pinned = false;
9721 }
9722 if (dest_log_pinned) {
9723 btrfs_end_log_trans(dest);
9724 dest_log_pinned = false;
9725 }
9726 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009727 if (!ret && sync_log_root && !commit_transaction) {
9728 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9729 &ctx_root);
9730 if (ret)
9731 commit_transaction = true;
9732 }
9733 if (!ret && sync_log_dest && !commit_transaction) {
9734 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9735 &ctx_dest);
9736 if (ret)
9737 commit_transaction = true;
9738 }
9739 if (commit_transaction) {
9740 ret = btrfs_commit_transaction(trans);
9741 } else {
9742 int ret2;
9743
9744 ret2 = btrfs_end_transaction(trans);
9745 ret = ret ? ret : ret2;
9746 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009747out_notrans:
9748 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009749 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009750 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009751 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009752
9753 return ret;
9754}
9755
9756static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9757 struct btrfs_root *root,
9758 struct inode *dir,
9759 struct dentry *dentry)
9760{
9761 int ret;
9762 struct inode *inode;
9763 u64 objectid;
9764 u64 index;
9765
9766 ret = btrfs_find_free_ino(root, &objectid);
9767 if (ret)
9768 return ret;
9769
9770 inode = btrfs_new_inode(trans, root, dir,
9771 dentry->d_name.name,
9772 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009773 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009774 objectid,
9775 S_IFCHR | WHITEOUT_MODE,
9776 &index);
9777
9778 if (IS_ERR(inode)) {
9779 ret = PTR_ERR(inode);
9780 return ret;
9781 }
9782
9783 inode->i_op = &btrfs_special_inode_operations;
9784 init_special_inode(inode, inode->i_mode,
9785 WHITEOUT_DEV);
9786
9787 ret = btrfs_init_inode_security(trans, inode, dir,
9788 &dentry->d_name);
9789 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009790 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009791
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009792 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9793 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009794 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009795 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009796
9797 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009798out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009799 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009800 if (ret)
9801 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009802 iput(inode);
9803
Filipe Mananac9901612016-05-05 01:41:57 +01009804 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805}
9806
Chris Mason39279cc2007-06-12 06:35:45 -04009807static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009808 struct inode *new_dir, struct dentry *new_dentry,
9809 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009810{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009811 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009812 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009813 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009814 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9815 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009816 struct inode *new_inode = d_inode(new_dentry);
9817 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009818 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009819 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009820 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009821 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009822 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009823 struct btrfs_log_ctx ctx;
9824 bool sync_log = false;
9825 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009826
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009827 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009828 return -EPERM;
9829
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009830 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009831 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009832 return -EXDEV;
9833
Li Zefan33345d012011-04-20 10:31:50 +08009834 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009835 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009836 return -ENOTEMPTY;
9837
Chris Mason39279cc2007-06-12 06:35:45 -04009838 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009839 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009840 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009841
9842
9843 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009844 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009845 new_dentry->d_name.name,
9846 new_dentry->d_name.len);
9847
9848 if (ret) {
9849 if (ret == -EEXIST) {
9850 /* we shouldn't get
9851 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309852 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009853 return ret;
9854 }
9855 } else {
9856 /* maybe -EOVERFLOW */
9857 return ret;
9858 }
9859 }
9860 ret = 0;
9861
Chris Mason5a3f23d2009-03-31 13:27:11 -04009862 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009863 * we're using rename to replace one file with another. Start IO on it
9864 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009865 */
Chris Mason8d875f92014-08-12 10:47:42 -07009866 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009867 filemap_flush(old_inode->i_mapping);
9868
Yan, Zheng76dda932009-09-21 16:00:26 -04009869 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009870 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009871 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009872 /*
9873 * We want to reserve the absolute worst case amount of items. So if
9874 * both inodes are subvols and we need to unlink them then that would
9875 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009876 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009877 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9878 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009879 * If our rename has the whiteout flag, we need more 5 units for the
9880 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9881 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009882 */
Filipe Manana5062af32016-05-05 10:26:26 +01009883 trans_num_items = 11;
9884 if (flags & RENAME_WHITEOUT)
9885 trans_num_items += 5;
9886 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009887 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009888 ret = PTR_ERR(trans);
9889 goto out_notrans;
9890 }
Chris Mason5f39d392007-10-15 16:14:19 -04009891
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009892 if (dest != root)
9893 btrfs_record_root_in_trans(trans, dest);
9894
Nikolay Borisov877574e2017-02-20 13:50:33 +02009895 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009896 if (ret)
9897 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009898
Miao Xie67de1172013-12-26 13:07:06 +08009899 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009900 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009901 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009902 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009903 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009904 btrfs_pin_log_trans(root);
9905 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009906 ret = btrfs_insert_inode_ref(trans, dest,
9907 new_dentry->d_name.name,
9908 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009909 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009910 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009911 if (ret)
9912 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009913 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009914
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009915 inode_inc_iversion(old_dir);
9916 inode_inc_iversion(new_dir);
9917 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009918 old_dir->i_ctime = old_dir->i_mtime =
9919 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009920 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009921
Chris Mason12fcfd22009-03-24 10:24:20 -04009922 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009923 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9924 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009925
Li Zefan33345d012011-04-20 10:31:50 +08009926 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009927 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009928 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009929 old_dentry->d_name.name,
9930 old_dentry->d_name.len);
9931 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009932 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9933 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009934 old_dentry->d_name.name,
9935 old_dentry->d_name.len);
9936 if (!ret)
9937 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009938 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009939 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009940 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009941 goto out_fail;
9942 }
Chris Mason39279cc2007-06-12 06:35:45 -04009943
9944 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009945 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009946 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009947 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009948 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9949 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009950 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009951 new_dentry->d_name.name,
9952 new_dentry->d_name.len);
9953 BUG_ON(new_inode->i_nlink == 0);
9954 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009955 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9956 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009957 new_dentry->d_name.name,
9958 new_dentry->d_name.len);
9959 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009960 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009961 ret = btrfs_orphan_add(trans,
9962 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009963 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009964 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009965 goto out_fail;
9966 }
Chris Mason39279cc2007-06-12 06:35:45 -04009967 }
Josef Bacikaec74772008-07-24 12:12:38 -04009968
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009969 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009970 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009971 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009972 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009973 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009974 goto out_fail;
9975 }
Chris Mason39279cc2007-06-12 06:35:45 -04009976
Miao Xie67de1172013-12-26 13:07:06 +08009977 if (old_inode->i_nlink == 1)
9978 BTRFS_I(old_inode)->dir_index = index;
9979
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009980 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009981 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009982
Filipe Mananad4682ba2018-06-11 19:24:28 +01009983 btrfs_init_log_ctx(&ctx, old_inode);
9984 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9985 BTRFS_I(old_dir), parent,
9986 false, &ctx);
9987 if (ret == BTRFS_NEED_LOG_SYNC)
9988 sync_log = true;
9989 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9990 commit_transaction = true;
9991 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009992 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009993 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009994 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009995
9996 if (flags & RENAME_WHITEOUT) {
9997 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9998 old_dentry);
9999
10000 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010001 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010002 goto out_fail;
10003 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010004 }
Chris Mason39279cc2007-06-12 06:35:45 -040010005out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010006 /*
10007 * If we have pinned the log and an error happened, we unpin tasks
10008 * trying to sync the log and force them to fallback to a transaction
10009 * commit if the log currently contains any of the inodes involved in
10010 * this rename operation (to ensure we do not persist a log with an
10011 * inconsistent state for any of these inodes or leading to any
10012 * inconsistencies when replayed). If the transaction was aborted, the
10013 * abortion reason is propagated to userspace when attempting to commit
10014 * the transaction. If the log does not contain any of these inodes, we
10015 * allow the tasks to sync it.
10016 */
10017 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010018 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10019 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10020 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010021 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010022 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +010010023 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010024
10025 btrfs_end_log_trans(root);
10026 log_pinned = false;
10027 }
Filipe Mananad4682ba2018-06-11 19:24:28 +010010028 if (!ret && sync_log) {
10029 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
10030 if (ret)
10031 commit_transaction = true;
10032 }
10033 if (commit_transaction) {
10034 ret = btrfs_commit_transaction(trans);
10035 } else {
10036 int ret2;
10037
10038 ret2 = btrfs_end_transaction(trans);
10039 ret = ret ? ret : ret2;
10040 }
Johann Lombardib44c59a2011-03-31 13:23:47 +000010041out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010042 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010043 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010044
Chris Mason39279cc2007-06-12 06:35:45 -040010045 return ret;
10046}
10047
Miklos Szeredi80ace852014-07-23 15:15:32 +020010048static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10049 struct inode *new_dir, struct dentry *new_dentry,
10050 unsigned int flags)
10051{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010052 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010053 return -EINVAL;
10054
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010055 if (flags & RENAME_EXCHANGE)
10056 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10057 new_dentry);
10058
10059 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010060}
10061
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010062struct btrfs_delalloc_work {
10063 struct inode *inode;
10064 struct completion completion;
10065 struct list_head list;
10066 struct btrfs_work work;
10067};
10068
Miao Xie8ccf6f192012-10-25 09:28:04 +000010069static void btrfs_run_delalloc_work(struct btrfs_work *work)
10070{
10071 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010072 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010073
10074 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10075 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010076 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010077 filemap_flush(inode->i_mapping);
10078 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10079 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010080 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010081
Nikolay Borisov076da912018-04-23 10:54:16 +030010082 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010083 complete(&delalloc_work->completion);
10084}
10085
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010086static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010087{
10088 struct btrfs_delalloc_work *work;
10089
David Sterba100d5702015-12-08 14:39:32 +010010090 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010091 if (!work)
10092 return NULL;
10093
10094 init_completion(&work->completion);
10095 INIT_LIST_HEAD(&work->list);
10096 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +080010097 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10098 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010099
10100 return work;
10101}
10102
Chris Masond352ac62008-09-29 15:18:18 -040010103/*
10104 * some fairly slow code that needs optimization. This walks the list
10105 * of all the inodes with pending delalloc and forces them to disk.
10106 */
Ethan Lien3cd24c62018-11-01 14:49:03 +080010107static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -040010108{
Chris Masonea8c2812008-08-04 23:17:27 -040010109 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010110 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010111 struct btrfs_delalloc_work *work, *next;
10112 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010113 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010114 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010115
Miao Xie8ccf6f192012-10-25 09:28:04 +000010116 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010117 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010118
Miao Xie573bfb72014-03-06 13:55:03 +080010119 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010120 spin_lock(&root->delalloc_lock);
10121 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010122 while (!list_empty(&splice)) {
10123 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010124 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010125
Miao Xieeb73c1b2013-05-15 07:48:22 +000010126 list_move_tail(&binode->delalloc_inodes,
10127 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010128 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010129 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010130 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010131 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010132 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010133 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010134
Ethan Lien3cd24c62018-11-01 14:49:03 +080010135 if (snapshot)
10136 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10137 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +030010138 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010139 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010140 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010141 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010142 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010143 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010144 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010145 btrfs_queue_work(root->fs_info->flush_workers,
10146 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010147 ret++;
10148 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010149 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010150 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010151 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010152 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010153 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010154
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010155out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010156 list_for_each_entry_safe(work, next, &works, list) {
10157 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010158 wait_for_completion(&work->completion);
10159 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010160 }
10161
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010162 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010163 spin_lock(&root->delalloc_lock);
10164 list_splice_tail(&splice, &root->delalloc_inodes);
10165 spin_unlock(&root->delalloc_lock);
10166 }
Miao Xie573bfb72014-03-06 13:55:03 +080010167 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010168 return ret;
10169}
10170
Ethan Lien3cd24c62018-11-01 14:49:03 +080010171int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010172{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010173 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010174 int ret;
10175
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010176 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010177 return -EROFS;
10178
Ethan Lien3cd24c62018-11-01 14:49:03 +080010179 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010180 if (ret > 0)
10181 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010182 return ret;
10183}
10184
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010185int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010186{
10187 struct btrfs_root *root;
10188 struct list_head splice;
10189 int ret;
10190
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010191 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010192 return -EROFS;
10193
10194 INIT_LIST_HEAD(&splice);
10195
Miao Xie573bfb72014-03-06 13:55:03 +080010196 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010197 spin_lock(&fs_info->delalloc_root_lock);
10198 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010199 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010200 root = list_first_entry(&splice, struct btrfs_root,
10201 delalloc_root);
10202 root = btrfs_grab_fs_root(root);
10203 BUG_ON(!root);
10204 list_move_tail(&root->delalloc_root,
10205 &fs_info->delalloc_roots);
10206 spin_unlock(&fs_info->delalloc_root_lock);
10207
Ethan Lien3cd24c62018-11-01 14:49:03 +080010208 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010209 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010210 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010211 goto out;
10212
Miao Xie6c255e62014-03-06 13:55:01 +080010213 if (nr != -1) {
10214 nr -= ret;
10215 WARN_ON(nr < 0);
10216 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010217 spin_lock(&fs_info->delalloc_root_lock);
10218 }
10219 spin_unlock(&fs_info->delalloc_root_lock);
10220
Miao Xie6c255e62014-03-06 13:55:01 +080010221 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010222out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010223 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010224 spin_lock(&fs_info->delalloc_root_lock);
10225 list_splice_tail(&splice, &fs_info->delalloc_roots);
10226 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010227 }
Miao Xie573bfb72014-03-06 13:55:03 +080010228 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010229 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010230}
10231
Chris Mason39279cc2007-06-12 06:35:45 -040010232static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10233 const char *symname)
10234{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010235 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010236 struct btrfs_trans_handle *trans;
10237 struct btrfs_root *root = BTRFS_I(dir)->root;
10238 struct btrfs_path *path;
10239 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010240 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010241 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010242 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010243 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010244 int name_len;
10245 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010246 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010247 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010248 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010249
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010250 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010251 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010252 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010253
Josef Bacik9ed74f22009-09-11 16:12:44 -040010254 /*
10255 * 2 items for inode item and ref
10256 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010257 * 1 item for updating parent inode item
10258 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010259 * 1 item for xattr if selinux is on
10260 */
Filipe Manana9269d122015-12-31 18:16:29 +000010261 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010262 if (IS_ERR(trans))
10263 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010264
Li Zefan581bb052011-04-20 10:06:11 +080010265 err = btrfs_find_free_ino(root, &objectid);
10266 if (err)
10267 goto out_unlock;
10268
Josef Bacikaec74772008-07-24 12:12:38 -040010269 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010270 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10271 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010272 if (IS_ERR(inode)) {
10273 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010274 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010275 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010276 }
Chris Mason39279cc2007-06-12 06:35:45 -040010277
Casey Schauflerad19db72011-12-15 10:09:07 -050010278 /*
10279 * If the active LSM wants to access the inode during
10280 * d_instantiate it needs these. Smack checks to see
10281 * if the filesystem supports xattrs by looking at the
10282 * ops vector.
10283 */
10284 inode->i_fop = &btrfs_file_operations;
10285 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010286 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010287 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10288
10289 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10290 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010291 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010292
Chris Mason39279cc2007-06-12 06:35:45 -040010293 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010294 if (!path) {
10295 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010296 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010297 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010298 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010299 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010300 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010301 datasize = btrfs_file_extent_calc_inline_size(name_len);
10302 err = btrfs_insert_empty_item(trans, root, path, &key,
10303 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010304 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010305 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010306 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010307 }
Chris Mason5f39d392007-10-15 16:14:19 -040010308 leaf = path->nodes[0];
10309 ei = btrfs_item_ptr(leaf, path->slots[0],
10310 struct btrfs_file_extent_item);
10311 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10312 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010313 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010314 btrfs_set_file_extent_encryption(leaf, ei, 0);
10315 btrfs_set_file_extent_compression(leaf, ei, 0);
10316 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10317 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10318
Chris Mason39279cc2007-06-12 06:35:45 -040010319 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010320 write_extent_buffer(leaf, symname, ptr, name_len);
10321 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010322 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010323
Chris Mason39279cc2007-06-12 06:35:45 -040010324 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010325 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010326 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010327 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010328 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010329 /*
10330 * Last step, add directory indexes for our symlink inode. This is the
10331 * last step to avoid extra cleanup of these indexes if an error happens
10332 * elsewhere above.
10333 */
10334 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010335 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10336 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010337 if (err)
10338 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010339
Al Viro1e2e5472018-05-04 08:23:01 -040010340 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010341
10342out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010343 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010344 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010345 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010346 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010347 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010348 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010349 return err;
10350}
Chris Mason16432982008-04-10 10:23:21 -040010351
Josef Bacik0af3d002010-06-21 14:48:16 -040010352static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10353 u64 start, u64 num_bytes, u64 min_size,
10354 loff_t actual_len, u64 *alloc_hint,
10355 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010356{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010357 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010358 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10359 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010360 struct btrfs_root *root = BTRFS_I(inode)->root;
10361 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010362 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010363 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010364 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010365 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010366 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010367 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010368 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010369
Josef Bacik0af3d002010-06-21 14:48:16 -040010370 if (trans)
10371 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010372 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010373 if (own_trans) {
10374 trans = btrfs_start_transaction(root, 3);
10375 if (IS_ERR(trans)) {
10376 ret = PTR_ERR(trans);
10377 break;
10378 }
Yan Zhengd899e052008-10-30 14:25:28 -040010379 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010380
Byongho Leeee221842015-12-15 01:42:10 +090010381 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010382 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010383 /*
10384 * If we are severely fragmented we could end up with really
10385 * small allocations, so if the allocator is returning small
10386 * chunks lets make its job easier by only searching for those
10387 * sized chunks.
10388 */
10389 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010390 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10391 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010392 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010393 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010394 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010395 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010396 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010397 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010398
Josef Bacik0b670dc2015-09-23 17:11:16 -040010399 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010400 ret = insert_reserved_file_extent(trans, inode,
10401 cur_offset, ins.objectid,
10402 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010403 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010404 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010405 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010406 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010407 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010408 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010409 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010410 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010411 break;
10412 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010413
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010414 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010415 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010416
Josef Bacik5dc562c2012-08-17 13:14:17 -040010417 em = alloc_extent_map();
10418 if (!em) {
10419 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10420 &BTRFS_I(inode)->runtime_flags);
10421 goto next;
10422 }
10423
10424 em->start = cur_offset;
10425 em->orig_start = cur_offset;
10426 em->len = ins.offset;
10427 em->block_start = ins.objectid;
10428 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010429 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010430 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010431 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010432 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10433 em->generation = trans->transid;
10434
10435 while (1) {
10436 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010437 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010438 write_unlock(&em_tree->lock);
10439 if (ret != -EEXIST)
10440 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010441 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010442 cur_offset + ins.offset - 1,
10443 0);
10444 }
10445 free_extent_map(em);
10446next:
Yan Zhengd899e052008-10-30 14:25:28 -040010447 num_bytes -= ins.offset;
10448 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010449 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010450
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010451 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010452 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010453 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010454 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010455 (actual_len > inode->i_size) &&
10456 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010457 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010458 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010459 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010460 i_size = cur_offset;
10461 i_size_write(inode, i_size);
10462 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010463 }
10464
Yan Zhengd899e052008-10-30 14:25:28 -040010465 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010466
10467 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010468 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010469 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010470 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010471 break;
10472 }
Yan Zhengd899e052008-10-30 14:25:28 -040010473
Josef Bacik0af3d002010-06-21 14:48:16 -040010474 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010475 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010476 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010477 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010478 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010479 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010480 return ret;
10481}
10482
Josef Bacik0af3d002010-06-21 14:48:16 -040010483int btrfs_prealloc_file_range(struct inode *inode, int mode,
10484 u64 start, u64 num_bytes, u64 min_size,
10485 loff_t actual_len, u64 *alloc_hint)
10486{
10487 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10488 min_size, actual_len, alloc_hint,
10489 NULL);
10490}
10491
10492int btrfs_prealloc_file_range_trans(struct inode *inode,
10493 struct btrfs_trans_handle *trans, int mode,
10494 u64 start, u64 num_bytes, u64 min_size,
10495 loff_t actual_len, u64 *alloc_hint)
10496{
10497 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10498 min_size, actual_len, alloc_hint, trans);
10499}
10500
Chris Masone6dcd2d2008-07-17 12:53:50 -040010501static int btrfs_set_page_dirty(struct page *page)
10502{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010503 return __set_page_dirty_nobuffers(page);
10504}
10505
Al Viro10556cb22011-06-20 19:28:19 -040010506static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010507{
Li Zefanb83cc962010-12-20 16:04:08 +080010508 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010509 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010510
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010511 if (mask & MAY_WRITE &&
10512 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10513 if (btrfs_root_readonly(root))
10514 return -EROFS;
10515 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10516 return -EACCES;
10517 }
Al Viro2830ba72011-06-20 19:16:29 -040010518 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010519}
Chris Mason39279cc2007-06-12 06:35:45 -040010520
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010521static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10522{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010523 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010524 struct btrfs_trans_handle *trans;
10525 struct btrfs_root *root = BTRFS_I(dir)->root;
10526 struct inode *inode = NULL;
10527 u64 objectid;
10528 u64 index;
10529 int ret = 0;
10530
10531 /*
10532 * 5 units required for adding orphan entry
10533 */
10534 trans = btrfs_start_transaction(root, 5);
10535 if (IS_ERR(trans))
10536 return PTR_ERR(trans);
10537
10538 ret = btrfs_find_free_ino(root, &objectid);
10539 if (ret)
10540 goto out;
10541
10542 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010543 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010544 if (IS_ERR(inode)) {
10545 ret = PTR_ERR(inode);
10546 inode = NULL;
10547 goto out;
10548 }
10549
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010550 inode->i_fop = &btrfs_file_operations;
10551 inode->i_op = &btrfs_file_inode_operations;
10552
10553 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010554 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10555
Chris Masonb0d5d102014-09-08 13:08:51 -070010556 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10557 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010558 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010559
10560 ret = btrfs_update_inode(trans, root, inode);
10561 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010562 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010563 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010564 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010565 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010566
Filipe Manana5762b5c2014-08-01 00:10:32 +010010567 /*
10568 * We set number of links to 0 in btrfs_new_inode(), and here we set
10569 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10570 * through:
10571 *
10572 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10573 */
10574 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010575 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010576 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010577 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010578out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010579 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010580 if (ret && inode)
10581 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010582 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010583 return ret;
10584}
10585
David Sterba5cdc84b2018-07-18 20:32:52 +020010586void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010587{
David Sterba5cdc84b2018-07-18 20:32:52 +020010588 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010589 unsigned long index = start >> PAGE_SHIFT;
10590 unsigned long end_index = end >> PAGE_SHIFT;
10591 struct page *page;
10592
10593 while (index <= end_index) {
10594 page = find_get_page(inode->i_mapping, index);
10595 ASSERT(page); /* Pages should be in the extent_io_tree */
10596 set_page_writeback(page);
10597 put_page(page);
10598 index++;
10599 }
10600}
10601
Omar Sandovaled46ff32016-11-03 10:28:14 -070010602#ifdef CONFIG_SWAP
10603/*
10604 * Add an entry indicating a block group or device which is pinned by a
10605 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10606 * negative errno on failure.
10607 */
10608static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10609 bool is_block_group)
10610{
10611 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10612 struct btrfs_swapfile_pin *sp, *entry;
10613 struct rb_node **p;
10614 struct rb_node *parent = NULL;
10615
10616 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10617 if (!sp)
10618 return -ENOMEM;
10619 sp->ptr = ptr;
10620 sp->inode = inode;
10621 sp->is_block_group = is_block_group;
10622
10623 spin_lock(&fs_info->swapfile_pins_lock);
10624 p = &fs_info->swapfile_pins.rb_node;
10625 while (*p) {
10626 parent = *p;
10627 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10628 if (sp->ptr < entry->ptr ||
10629 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10630 p = &(*p)->rb_left;
10631 } else if (sp->ptr > entry->ptr ||
10632 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10633 p = &(*p)->rb_right;
10634 } else {
10635 spin_unlock(&fs_info->swapfile_pins_lock);
10636 kfree(sp);
10637 return 1;
10638 }
10639 }
10640 rb_link_node(&sp->node, parent, p);
10641 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10642 spin_unlock(&fs_info->swapfile_pins_lock);
10643 return 0;
10644}
10645
10646/* Free all of the entries pinned by this swapfile. */
10647static void btrfs_free_swapfile_pins(struct inode *inode)
10648{
10649 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10650 struct btrfs_swapfile_pin *sp;
10651 struct rb_node *node, *next;
10652
10653 spin_lock(&fs_info->swapfile_pins_lock);
10654 node = rb_first(&fs_info->swapfile_pins);
10655 while (node) {
10656 next = rb_next(node);
10657 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10658 if (sp->inode == inode) {
10659 rb_erase(&sp->node, &fs_info->swapfile_pins);
10660 if (sp->is_block_group)
10661 btrfs_put_block_group(sp->ptr);
10662 kfree(sp);
10663 }
10664 node = next;
10665 }
10666 spin_unlock(&fs_info->swapfile_pins_lock);
10667}
10668
10669struct btrfs_swap_info {
10670 u64 start;
10671 u64 block_start;
10672 u64 block_len;
10673 u64 lowest_ppage;
10674 u64 highest_ppage;
10675 unsigned long nr_pages;
10676 int nr_extents;
10677};
10678
10679static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10680 struct btrfs_swap_info *bsi)
10681{
10682 unsigned long nr_pages;
10683 u64 first_ppage, first_ppage_reported, next_ppage;
10684 int ret;
10685
10686 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10687 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10688 PAGE_SIZE) >> PAGE_SHIFT;
10689
10690 if (first_ppage >= next_ppage)
10691 return 0;
10692 nr_pages = next_ppage - first_ppage;
10693
10694 first_ppage_reported = first_ppage;
10695 if (bsi->start == 0)
10696 first_ppage_reported++;
10697 if (bsi->lowest_ppage > first_ppage_reported)
10698 bsi->lowest_ppage = first_ppage_reported;
10699 if (bsi->highest_ppage < (next_ppage - 1))
10700 bsi->highest_ppage = next_ppage - 1;
10701
10702 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10703 if (ret < 0)
10704 return ret;
10705 bsi->nr_extents += ret;
10706 bsi->nr_pages += nr_pages;
10707 return 0;
10708}
10709
10710static void btrfs_swap_deactivate(struct file *file)
10711{
10712 struct inode *inode = file_inode(file);
10713
10714 btrfs_free_swapfile_pins(inode);
10715 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10716}
10717
10718static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10719 sector_t *span)
10720{
10721 struct inode *inode = file_inode(file);
10722 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10723 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10724 struct extent_state *cached_state = NULL;
10725 struct extent_map *em = NULL;
10726 struct btrfs_device *device = NULL;
10727 struct btrfs_swap_info bsi = {
10728 .lowest_ppage = (sector_t)-1ULL,
10729 };
10730 int ret = 0;
10731 u64 isize;
10732 u64 start;
10733
10734 /*
10735 * If the swap file was just created, make sure delalloc is done. If the
10736 * file changes again after this, the user is doing something stupid and
10737 * we don't really care.
10738 */
10739 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10740 if (ret)
10741 return ret;
10742
10743 /*
10744 * The inode is locked, so these flags won't change after we check them.
10745 */
10746 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10747 btrfs_warn(fs_info, "swapfile must not be compressed");
10748 return -EINVAL;
10749 }
10750 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10751 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10752 return -EINVAL;
10753 }
10754 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10755 btrfs_warn(fs_info, "swapfile must not be checksummed");
10756 return -EINVAL;
10757 }
10758
10759 /*
10760 * Balance or device remove/replace/resize can move stuff around from
10761 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10762 * concurrently while we are mapping the swap extents, and
10763 * fs_info->swapfile_pins prevents them from running while the swap file
10764 * is active and moving the extents. Note that this also prevents a
10765 * concurrent device add which isn't actually necessary, but it's not
10766 * really worth the trouble to allow it.
10767 */
10768 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10769 btrfs_warn(fs_info,
10770 "cannot activate swapfile while exclusive operation is running");
10771 return -EBUSY;
10772 }
10773 /*
10774 * Snapshots can create extents which require COW even if NODATACOW is
10775 * set. We use this counter to prevent snapshots. We must increment it
10776 * before walking the extents because we don't want a concurrent
10777 * snapshot to run after we've already checked the extents.
10778 */
10779 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10780
10781 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10782
10783 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10784 start = 0;
10785 while (start < isize) {
10786 u64 logical_block_start, physical_block_start;
10787 struct btrfs_block_group_cache *bg;
10788 u64 len = isize - start;
10789
10790 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10791 if (IS_ERR(em)) {
10792 ret = PTR_ERR(em);
10793 goto out;
10794 }
10795
10796 if (em->block_start == EXTENT_MAP_HOLE) {
10797 btrfs_warn(fs_info, "swapfile must not have holes");
10798 ret = -EINVAL;
10799 goto out;
10800 }
10801 if (em->block_start == EXTENT_MAP_INLINE) {
10802 /*
10803 * It's unlikely we'll ever actually find ourselves
10804 * here, as a file small enough to fit inline won't be
10805 * big enough to store more than the swap header, but in
10806 * case something changes in the future, let's catch it
10807 * here rather than later.
10808 */
10809 btrfs_warn(fs_info, "swapfile must not be inline");
10810 ret = -EINVAL;
10811 goto out;
10812 }
10813 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10814 btrfs_warn(fs_info, "swapfile must not be compressed");
10815 ret = -EINVAL;
10816 goto out;
10817 }
10818
10819 logical_block_start = em->block_start + (start - em->start);
10820 len = min(len, em->len - (start - em->start));
10821 free_extent_map(em);
10822 em = NULL;
10823
10824 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10825 if (ret < 0) {
10826 goto out;
10827 } else if (ret) {
10828 ret = 0;
10829 } else {
10830 btrfs_warn(fs_info,
10831 "swapfile must not be copy-on-write");
10832 ret = -EINVAL;
10833 goto out;
10834 }
10835
10836 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10837 if (IS_ERR(em)) {
10838 ret = PTR_ERR(em);
10839 goto out;
10840 }
10841
10842 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10843 btrfs_warn(fs_info,
10844 "swapfile must have single data profile");
10845 ret = -EINVAL;
10846 goto out;
10847 }
10848
10849 if (device == NULL) {
10850 device = em->map_lookup->stripes[0].dev;
10851 ret = btrfs_add_swapfile_pin(inode, device, false);
10852 if (ret == 1)
10853 ret = 0;
10854 else if (ret)
10855 goto out;
10856 } else if (device != em->map_lookup->stripes[0].dev) {
10857 btrfs_warn(fs_info, "swapfile must be on one device");
10858 ret = -EINVAL;
10859 goto out;
10860 }
10861
10862 physical_block_start = (em->map_lookup->stripes[0].physical +
10863 (logical_block_start - em->start));
10864 len = min(len, em->len - (logical_block_start - em->start));
10865 free_extent_map(em);
10866 em = NULL;
10867
10868 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10869 if (!bg) {
10870 btrfs_warn(fs_info,
10871 "could not find block group containing swapfile");
10872 ret = -EINVAL;
10873 goto out;
10874 }
10875
10876 ret = btrfs_add_swapfile_pin(inode, bg, true);
10877 if (ret) {
10878 btrfs_put_block_group(bg);
10879 if (ret == 1)
10880 ret = 0;
10881 else
10882 goto out;
10883 }
10884
10885 if (bsi.block_len &&
10886 bsi.block_start + bsi.block_len == physical_block_start) {
10887 bsi.block_len += len;
10888 } else {
10889 if (bsi.block_len) {
10890 ret = btrfs_add_swap_extent(sis, &bsi);
10891 if (ret)
10892 goto out;
10893 }
10894 bsi.start = start;
10895 bsi.block_start = physical_block_start;
10896 bsi.block_len = len;
10897 }
10898
10899 start += len;
10900 }
10901
10902 if (bsi.block_len)
10903 ret = btrfs_add_swap_extent(sis, &bsi);
10904
10905out:
10906 if (!IS_ERR_OR_NULL(em))
10907 free_extent_map(em);
10908
10909 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10910
10911 if (ret)
10912 btrfs_swap_deactivate(file);
10913
10914 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10915
10916 if (ret)
10917 return ret;
10918
10919 if (device)
10920 sis->bdev = device->bdev;
10921 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10922 sis->max = bsi.nr_pages;
10923 sis->pages = bsi.nr_pages - 1;
10924 sis->highest_bit = bsi.nr_pages - 1;
10925 return bsi.nr_extents;
10926}
10927#else
10928static void btrfs_swap_deactivate(struct file *file)
10929{
10930}
10931
10932static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10933 sector_t *span)
10934{
10935 return -EOPNOTSUPP;
10936}
10937#endif
10938
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010939static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010940 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010941 .lookup = btrfs_lookup,
10942 .create = btrfs_create,
10943 .unlink = btrfs_unlink,
10944 .link = btrfs_link,
10945 .mkdir = btrfs_mkdir,
10946 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010947 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010948 .symlink = btrfs_symlink,
10949 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010950 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010951 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010952 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010953 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010954 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010955 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010956 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010957};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010958static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010959 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010960 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010961 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010962};
Yan, Zheng76dda932009-09-21 16:00:26 -040010963
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010964static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010965 .llseek = generic_file_llseek,
10966 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010967 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010968 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010969 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010970#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010971 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010972#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010973 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010974 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010975};
10976
David Sterba20e55062015-11-19 11:42:28 +010010977static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010978 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010979 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010980 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10981};
10982
Chris Mason35054392009-01-21 13:11:13 -050010983/*
10984 * btrfs doesn't support the bmap operation because swapfiles
10985 * use bmap to make a mapping of extents in the file. They assume
10986 * these extents won't change over the life of the file and they
10987 * use the bmap result to do IO directly to the drive.
10988 *
10989 * the btrfs bmap call would return logical addresses that aren't
10990 * suitable for IO and they also will change frequently as COW
10991 * operations happen. So, swapfile + btrfs == corruption.
10992 *
10993 * For now we're avoiding this by dropping bmap.
10994 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010995static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010996 .readpage = btrfs_readpage,
10997 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010998 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010999 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040011000 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040011001 .invalidatepage = btrfs_invalidatepage,
11002 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040011003 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020011004 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070011005 .swap_activate = btrfs_swap_activate,
11006 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040011007};
11008
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011009static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040011010 .getattr = btrfs_getattr,
11011 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050011012 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050011013 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050011014 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011015 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011016 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011017 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011018};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011019static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040011020 .getattr = btrfs_getattr,
11021 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011022 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040011023 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020011024 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080011025 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040011026 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040011027};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070011028static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050011029 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000011030 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050011031 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050011032 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050011033 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040011034 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040011035};
Yan, Zheng76dda932009-09-21 16:00:26 -040011036
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040011037const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040011038 .d_delete = btrfs_dentry_delete,
11039};