blob: c4116bc588274878e810b53ba64af99e4d408f43 [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>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include "ctree.h"
34#include "disk-io.h"
35#include "transaction.h"
36#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040037#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040038#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040039#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040040#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020041#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040042#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050043#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050044#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080045#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000046#include "backref.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000047#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080048#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040049#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040050#include "block-group.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051
52struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080053 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040054 struct btrfs_root *root;
55};
56
Filipe Mananaf28a4922015-12-08 19:23:20 +000057struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000058 u64 reserve;
59 u64 unsubmitted_oe_range_start;
60 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080061 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000062};
63
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070064static const struct inode_operations btrfs_dir_inode_operations;
65static const struct inode_operations btrfs_symlink_inode_operations;
66static const struct inode_operations btrfs_dir_ro_inode_operations;
67static const struct inode_operations btrfs_special_inode_operations;
68static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070069static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010071static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
74struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050076struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077
Eric Sandeen3972f262013-01-12 02:57:22 +000078static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000079static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040080static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050081static noinline int cow_file_range(struct inode *inode,
82 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030083 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030084 unsigned long *nr_written, int unlock);
Liu Bo6f9994d2017-01-31 07:50:22 -080085static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
86 u64 orig_start, u64 block_start,
87 u64 block_len, u64 orig_block_len,
88 u64 ram_bytes, int compress_type,
89 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040090
Qu Wenruo524272602017-03-08 10:25:52 +080091static void __endio_write_update_ordered(struct inode *inode,
92 const u64 offset, const u64 bytes,
93 const bool uptodate);
94
95/*
96 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +010097 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +080098 *
99 * NOTE: caller must ensure that when an error happens, it can not call
100 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
101 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
102 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200103 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800104 */
105static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200106 struct page *locked_page,
107 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800108{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900109 unsigned long index = offset >> PAGE_SHIFT;
110 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200111 u64 page_start = page_offset(locked_page);
112 u64 page_end = page_start + PAGE_SIZE - 1;
113
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900114 struct page *page;
115
116 while (index <= end_index) {
117 page = find_get_page(inode->i_mapping, index);
118 index++;
119 if (!page)
120 continue;
121 ClearPagePrivate2(page);
122 put_page(page);
123 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200124
125 /*
126 * In case this page belongs to the delalloc range being instantiated
127 * then skip it, since the first page of a range is going to be
128 * properly cleaned up by the caller of run_delalloc_range
129 */
130 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
131 offset += PAGE_SIZE;
132 bytes -= PAGE_SIZE;
133 }
134
135 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800136}
137
Eric Sandeen48a3b632013-04-25 20:41:01 +0000138static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400139
Josef Bacik6a3891c2015-03-16 17:38:52 -0400140#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
141void btrfs_test_inode_set_ops(struct inode *inode)
142{
143 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
144}
145#endif
146
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000147static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500148 struct inode *inode, struct inode *dir,
149 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500150{
151 int err;
152
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000153 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500154 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500155 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500156 return err;
157}
158
Chris Masond352ac62008-09-29 15:18:18 -0400159/*
Chris Masonc8b97812008-10-29 14:49:59 -0400160 * this does all the hard work for inserting an inline extent into
161 * the btree. The caller should have done a btrfs_drop_extents so that
162 * no overlapping inline items exist in the btree
163 */
Chris Mason40f76582014-05-21 13:35:51 -0700164static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400166 struct btrfs_root *root, struct inode *inode,
167 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000168 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400169 struct page **compressed_pages)
170{
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct extent_buffer *leaf;
172 struct page *page = NULL;
173 char *kaddr;
174 unsigned long ptr;
175 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400176 int ret;
177 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400178 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400179
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800180 ASSERT((compressed_size > 0 && compressed_pages) ||
181 (compressed_size == 0 && !compressed_pages));
182
Li Zefanfe3f5662011-03-28 08:30:38 +0000183 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400184 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400185
Chris Masonc8b97812008-10-29 14:49:59 -0400186 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000187
188 if (!extent_inserted) {
189 struct btrfs_key key;
190 size_t datasize;
191
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200192 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000193 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200194 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000195
196 datasize = btrfs_file_extent_calc_inline_size(cur_size);
197 path->leave_spinning = 1;
198 ret = btrfs_insert_empty_item(trans, root, path, &key,
199 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200200 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000201 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400202 }
203 leaf = path->nodes[0];
204 ei = btrfs_item_ptr(leaf, path->slots[0],
205 struct btrfs_file_extent_item);
206 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
207 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
208 btrfs_set_file_extent_encryption(leaf, ei, 0);
209 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
210 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
211 ptr = btrfs_file_extent_inline_start(ei);
212
Li Zefan261507a02010-12-17 14:21:50 +0800213 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400214 struct page *cpage;
215 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500216 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400217 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500218 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400220
Cong Wang7ac687d2011-11-25 23:14:28 +0800221 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400222 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800223 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400224
225 i++;
226 ptr += cur_size;
227 compressed_size -= cur_size;
228 }
229 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800230 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400231 } else {
232 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400234 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800235 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100236 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800238 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300239 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400240 }
241 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000242 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400243
Yan, Zhengc2167752009-11-12 09:34:21 +0000244 /*
245 * we're an inline extent, so nobody can
246 * extend the file past i_size without locking
247 * a page we already have locked.
248 *
249 * We must do any isize and inode updates
250 * before we unlock the pages. Otherwise we
251 * could end up racing with unlink.
252 */
Chris Masonc8b97812008-10-29 14:49:59 -0400253 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100254 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000255
Chris Masonc8b97812008-10-29 14:49:59 -0400256fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200257 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400258}
259
260
261/*
262 * conditionally insert an inline extent into the file. This
263 * does the checks required to make sure the data is small enough
264 * to fit as an inline extent.
265 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200266static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400267 u64 end, size_t compressed_size,
268 int compress_type,
269 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400270{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200271 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400272 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400273 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400274 u64 isize = i_size_read(inode);
275 u64 actual_end = min(end + 1, isize);
276 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400277 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400278 u64 data_len = inline_len;
279 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000280 struct btrfs_path *path;
281 int extent_inserted = 0;
282 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400283
284 if (compressed_size)
285 data_len = compressed_size;
286
287 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400288 actual_end > fs_info->sectorsize ||
289 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400290 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400291 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400292 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400293 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400294 return 1;
295 }
296
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000297 path = btrfs_alloc_path();
298 if (!path)
299 return -ENOMEM;
300
Josef Bacik00361582013-08-14 14:02:47 -0400301 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000302 if (IS_ERR(trans)) {
303 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400304 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400306 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400307
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000308 if (compressed_size && compressed_pages)
309 extent_item_size = btrfs_file_extent_calc_inline_size(
310 compressed_size);
311 else
312 extent_item_size = btrfs_file_extent_calc_inline_size(
313 inline_len);
314
315 ret = __btrfs_drop_extents(trans, root, inode, path,
316 start, aligned_end, NULL,
317 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400318 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400319 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400320 goto out;
321 }
Chris Masonc8b97812008-10-29 14:49:59 -0400322
323 if (isize > actual_end)
324 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000325 ret = insert_inline_extent(trans, path, extent_inserted,
326 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400327 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000328 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400329 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400330 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400331 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400332 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400333 ret = 1;
334 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100335 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400336
Josef Bacikbdc20e62013-02-28 13:23:38 -0500337 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200338 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400339out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800340 /*
341 * Don't forget to free the reserved space, as for inlined extent
342 * it won't count as data extent, free them directly here.
343 * And at reserve time, it's always aligned to page size, so
344 * just free one page here.
345 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800346 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000347 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400348 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400349 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400350}
351
Chris Mason771ed682008-11-06 22:02:51 -0500352struct async_extent {
353 u64 start;
354 u64 ram_size;
355 u64 compressed_size;
356 struct page **pages;
357 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800358 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500359 struct list_head list;
360};
361
Nikolay Borisov97db1202019-03-12 17:20:24 +0200362struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500363 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500364 struct page *locked_page;
365 u64 start;
366 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600367 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500368 struct list_head extents;
369 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200370 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500371};
372
Nikolay Borisov97db1202019-03-12 17:20:24 +0200373struct async_cow {
374 /* Number of chunks in flight; must be first in the structure */
375 atomic_t num_chunks;
376 struct async_chunk chunks[];
377};
378
379static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500380 u64 start, u64 ram_size,
381 u64 compressed_size,
382 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800383 unsigned long nr_pages,
384 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500385{
386 struct async_extent *async_extent;
387
388 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100389 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500390 async_extent->start = start;
391 async_extent->ram_size = ram_size;
392 async_extent->compressed_size = compressed_size;
393 async_extent->pages = pages;
394 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800395 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500396 list_add_tail(&async_extent->list, &cow->extents);
397 return 0;
398}
399
Qu Wenruo42c16da2019-07-01 05:12:46 +0000400/*
401 * Check if the inode has flags compatible with compression
402 */
403static inline bool inode_can_compress(struct inode *inode)
404{
405 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
406 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
407 return false;
408 return true;
409}
410
411/*
412 * Check if the inode needs to be submitted to compression, based on mount
413 * options, defragmentation, properties or heuristics.
414 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300415static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800416{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400417 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800418
Qu Wenruo42c16da2019-07-01 05:12:46 +0000419 if (!inode_can_compress(inode)) {
420 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
421 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
422 btrfs_ino(BTRFS_I(inode)));
423 return 0;
424 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800425 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400426 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800427 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200428 /* defrag ioctl */
429 if (BTRFS_I(inode)->defrag_compress)
430 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800431 /* bad compression ratios */
432 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
433 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400434 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800435 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200436 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300437 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800438 return 0;
439}
440
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200441static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800442 u64 start, u64 end, u64 num_bytes, u64 small_write)
443{
444 /* If this is a small write inside eof, kick off a defrag */
445 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200446 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800447 btrfs_add_inode_defrag(NULL, inode);
448}
449
Chris Masonc8b97812008-10-29 14:49:59 -0400450/*
Chris Mason771ed682008-11-06 22:02:51 -0500451 * we create compressed extents in two phases. The first
452 * phase compresses a range of pages that have already been
453 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400454 *
Chris Mason771ed682008-11-06 22:02:51 -0500455 * This is done inside an ordered work queue, and the compression
456 * is spread across many cpus. The actual IO submission is step
457 * two, and the ordered work queue takes care of making sure that
458 * happens in the same order things were put onto the queue by
459 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400460 *
Chris Mason771ed682008-11-06 22:02:51 -0500461 * If this code finds it can't get good compression, it puts an
462 * entry onto the work queue to write the uncompressed bytes. This
463 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300464 * are written in the same order that the flusher thread sent them
465 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400466 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300467static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400468{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200469 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400470 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400471 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200472 u64 start = async_chunk->start;
473 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400474 u64 actual_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400475 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400476 struct page **pages = NULL;
477 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400478 unsigned long total_compressed = 0;
479 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400480 int i;
481 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400482 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300483 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400484 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400485
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200486 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
487 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400488
Nikolay Borisov62b37622019-01-03 10:50:00 +0200489 actual_end = min_t(u64, i_size_read(inode), end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400490again:
491 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300492 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100493 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
494 nr_pages = min_t(unsigned long, nr_pages,
495 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400496
Chris Masonf03d9301f2009-02-04 09:31:06 -0500497 /*
498 * we don't want to send crud past the end of i_size through
499 * compression, that's just a waste of CPU time. So, if the
500 * end of the file is before the start of our current
501 * requested range of bytes, we bail out to the uncompressed
502 * cleanup code that can deal with all of this.
503 *
504 * It isn't really the fastest way to fix things, but this is a
505 * very uncommon corner.
506 */
507 if (actual_end <= start)
508 goto cleanup_and_bail_uncompressed;
509
Chris Masonc8b97812008-10-29 14:49:59 -0400510 total_compressed = actual_end - start;
511
Shilong Wang4bcbb332014-10-07 18:44:35 -0400512 /*
513 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400514 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400515 */
516 if (total_compressed <= blocksize &&
517 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
518 goto cleanup_and_bail_uncompressed;
519
David Sterba069eac72017-02-14 19:36:54 +0100520 total_compressed = min_t(unsigned long, total_compressed,
521 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400522 total_in = 0;
523 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400524
Chris Mason771ed682008-11-06 22:02:51 -0500525 /*
526 * we do compression for mount -o compress and when the
527 * inode has not been flagged as nocompress. This flag can
528 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400529 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300530 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400531 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100532 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800533 if (!pages) {
534 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100535 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800536 goto cont;
537 }
Chris Mason179e29e2007-11-01 11:28:41 -0400538
David Sterbaeec63c62017-07-17 19:41:31 +0200539 if (BTRFS_I(inode)->defrag_compress)
540 compress_type = BTRFS_I(inode)->defrag_compress;
541 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200542 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800543
Chris Mason4adaa612013-03-26 13:07:00 -0400544 /*
545 * we need to call clear_page_dirty_for_io on each
546 * page in the range. Otherwise applications with the file
547 * mmap'd can wander in and change the page contents while
548 * we are compressing them.
549 *
550 * If the compression fails for any reason, we set the pages
551 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300552 *
553 * Note that the remaining part is redirtied, the start pointer
554 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400555 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300556 if (!redirty) {
557 extent_range_clear_dirty_for_io(inode, start, end);
558 redirty = 1;
559 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200560
561 /* Compression level is applied here and only here */
562 ret = btrfs_compress_pages(
563 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800564 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100565 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100566 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800567 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100568 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400569
570 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100571 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100572 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400573 char *kaddr;
574
575 /* zero the tail end of the last page, we might be
576 * sending it down to disk
577 */
578 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800579 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400580 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300581 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800582 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400583 }
584 will_compress = 1;
585 }
586 }
Li Zefan560f7d72011-09-08 10:22:01 +0800587cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400588 if (start == 0) {
589 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300590 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400591 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500592 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400593 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200594 ret = cow_file_range_inline(inode, start, end, 0,
595 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400596 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500597 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200598 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000599 total_compressed,
600 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400601 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100602 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400603 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400604 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
605 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100606 unsigned long page_error_op;
607
Filipe Mananae6eb4312014-10-10 10:45:12 +0100608 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400609
Chris Mason771ed682008-11-06 22:02:51 -0500610 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100611 * inline extent creation worked or returned error,
612 * we don't need to create any more async work items.
613 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400614 *
615 * We use DO_ACCOUNTING here because we need the
616 * delalloc_release_metadata to be done _after_ we drop
617 * our outstanding extent for clearing delalloc for this
618 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500619 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300620 extent_clear_unlock_delalloc(inode, start, end, NULL,
621 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800622 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400623 PAGE_CLEAR_DIRTY |
624 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100625 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400626 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300627
628 for (i = 0; i < nr_pages; i++) {
629 WARN_ON(pages[i]->mapping);
630 put_page(pages[i]);
631 }
632 kfree(pages);
633
634 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400635 }
636 }
637
638 if (will_compress) {
639 /*
640 * we aren't doing an inline extent round the compressed size
641 * up to a block size boundary so the allocator does sane
642 * things
643 */
Qu Wenruofda28322013-02-26 08:10:22 +0000644 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400645
646 /*
647 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300648 * win, compare the page count read with the blocks on disk,
649 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400650 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300651 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300652 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300653 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700654
655 /*
656 * The async work queues will take care of doing actual
657 * allocation on disk for these compressed pages, and
658 * will submit them to the elevator.
659 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200660 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100661 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700662 compress_type);
663
Timofey Titovets11708622017-10-03 18:06:01 +0300664 if (start + total_in < end) {
665 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700666 pages = NULL;
667 cond_resched();
668 goto again;
669 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300670 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400671 }
672 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700673 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400674 /*
675 * the compression code ran but failed to make things smaller,
676 * free any pages it allocated and our page pointer array
677 */
David Sterba4d3a8002017-02-14 19:04:07 +0100678 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400679 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300680 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400681 }
682 kfree(pages);
683 pages = NULL;
684 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100685 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400686
687 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400688 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200689 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500690 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500691 }
Chris Masonc8b97812008-10-29 14:49:59 -0400692 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500693cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700694 /*
695 * No compression, but we still need to write the pages in the file
696 * we've been given so far. redirty the locked page if it corresponds
697 * to our extent and set things up for the async work queue to run
698 * cow_file_range to do the normal delalloc dance.
699 */
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200700 if (page_offset(async_chunk->locked_page) >= start &&
701 page_offset(async_chunk->locked_page) <= end)
702 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700703 /* unlocked later on in the async handlers */
704
705 if (redirty)
706 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200707 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700708 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300709 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500710
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300711 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500712}
Chris Mason771ed682008-11-06 22:02:51 -0500713
Filipe Manana40ae8372014-10-06 22:14:24 +0100714static void free_async_extent_pages(struct async_extent *async_extent)
715{
716 int i;
717
718 if (!async_extent->pages)
719 return;
720
721 for (i = 0; i < async_extent->nr_pages; i++) {
722 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300723 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100724 }
725 kfree(async_extent->pages);
726 async_extent->nr_pages = 0;
727 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500728}
729
730/*
731 * phase two of compressed writeback. This is the ordered portion
732 * of the code, which only gets called in the order the work was
733 * queued. We walk all the async extents created by compress_file_range
734 * and send them down to the disk.
735 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200736static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500737{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200738 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400739 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500740 struct async_extent *async_extent;
741 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500742 struct btrfs_key ins;
743 struct extent_map *em;
744 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200745 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500746 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500747
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500748again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200749 while (!list_empty(&async_chunk->extents)) {
750 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500751 struct async_extent, list);
752 list_del(&async_extent->list);
753
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500754retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200755 lock_extent(io_tree, async_extent->start,
756 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500757 /* did the compression code fall back to uncompressed IO? */
758 if (!async_extent->pages) {
759 int page_started = 0;
760 unsigned long nr_written = 0;
761
Chris Mason771ed682008-11-06 22:02:51 -0500762 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200763 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500764 async_extent->start,
765 async_extent->start +
766 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300767 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500768
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100769 /* JDM XXX */
770
Chris Mason771ed682008-11-06 22:02:51 -0500771 /*
772 * if page_started, cow_file_range inserted an
773 * inline extent and took care of all the unlocking
774 * and IO for us. Otherwise, we need to submit
775 * all those pages down to the drive.
776 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500777 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200778 extent_write_locked_range(inode,
779 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500780 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500781 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500782 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500783 else if (ret)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200784 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500785 kfree(async_extent);
786 cond_resched();
787 continue;
788 }
789
Wang Xiaoguang18513092016-07-25 15:51:40 +0800790 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500791 async_extent->compressed_size,
792 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800793 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500794 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100795 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500796
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400797 if (ret == -ENOSPC) {
798 unlock_extent(io_tree, async_extent->start,
799 async_extent->start +
800 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800801
802 /*
803 * we need to redirty the pages if we decide to
804 * fallback to uncompressed IO, otherwise we
805 * will not submit these pages down to lower
806 * layers.
807 */
808 extent_range_redirty_for_io(inode,
809 async_extent->start,
810 async_extent->start +
811 async_extent->ram_size - 1);
812
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100813 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400814 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500815 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500816 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000817 /*
818 * here we're doing allocation and writeback of the
819 * compressed pages
820 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800821 em = create_io_em(inode, async_extent->start,
822 async_extent->ram_size, /* len */
823 async_extent->start, /* orig_start */
824 ins.objectid, /* block_start */
825 ins.offset, /* block_len */
826 ins.offset, /* orig_block_len */
827 async_extent->ram_size, /* ram_bytes */
828 async_extent->compress_type,
829 BTRFS_ORDERED_COMPRESSED);
830 if (IS_ERR(em))
831 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500832 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800833 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500834
Li Zefan261507a02010-12-17 14:21:50 +0800835 ret = btrfs_add_ordered_extent_compress(inode,
836 async_extent->start,
837 ins.objectid,
838 async_extent->ram_size,
839 ins.offset,
840 BTRFS_ORDERED_COMPRESSED,
841 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100842 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200843 btrfs_drop_extent_cache(BTRFS_I(inode),
844 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100845 async_extent->start +
846 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500847 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100848 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400849 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500850
Chris Mason771ed682008-11-06 22:02:51 -0500851 /*
852 * clear dirty, set writeback and unlock the pages.
853 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400854 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400855 async_extent->start +
856 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400857 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
858 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400859 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200860 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500861 async_extent->start,
862 async_extent->ram_size,
863 ins.objectid,
864 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600865 async_extent->nr_pages,
Nikolay Borisovb5326272019-03-12 17:20:25 +0200866 async_chunk->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100867 struct page *p = async_extent->pages[0];
868 const u64 start = async_extent->start;
869 const u64 end = start + async_extent->ram_size - 1;
870
871 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200872 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200873
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100874 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300875 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800876 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100877 PAGE_END_WRITEBACK |
878 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100879 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100880 }
Chris Mason771ed682008-11-06 22:02:51 -0500881 alloc_hint = ins.objectid + ins.offset;
882 kfree(async_extent);
883 cond_resched();
884 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100885 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500886out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400887 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400888 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400890 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500891 async_extent->start +
892 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400893 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100894 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400895 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
896 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100897 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
898 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100899 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100900 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500901 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500902}
903
Josef Bacik4b46fce2010-05-23 11:00:55 -0400904static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
905 u64 num_bytes)
906{
907 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
908 struct extent_map *em;
909 u64 alloc_hint = 0;
910
911 read_lock(&em_tree->lock);
912 em = search_extent_mapping(em_tree, start, num_bytes);
913 if (em) {
914 /*
915 * if block start isn't an actual block number then find the
916 * first block in this inode and use that as a hint. If that
917 * block is also bogus then just don't worry about it.
918 */
919 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
920 free_extent_map(em);
921 em = search_extent_mapping(em_tree, 0, 0);
922 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
923 alloc_hint = em->block_start;
924 if (em)
925 free_extent_map(em);
926 } else {
927 alloc_hint = em->block_start;
928 free_extent_map(em);
929 }
930 }
931 read_unlock(&em_tree->lock);
932
933 return alloc_hint;
934}
935
Chris Mason771ed682008-11-06 22:02:51 -0500936/*
937 * when extent_io.c finds a delayed allocation range in the file,
938 * the call backs end up in this code. The basic idea is to
939 * allocate extents on disk for the range, and create ordered data structs
940 * in ram to track those extents.
941 *
942 * locked_page is the page that writepage had locked already. We use
943 * it to make sure we don't do extra locks or unlocks.
944 *
945 * *page_started is set to one if we unlock locked_page and do everything
946 * required to start IO on it. It may be clean and already done with
947 * IO when we return.
948 */
Josef Bacik00361582013-08-14 14:02:47 -0400949static noinline int cow_file_range(struct inode *inode,
950 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300951 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300952 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500953{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400954 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400955 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500956 u64 alloc_hint = 0;
957 u64 num_bytes;
958 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000959 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400960 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500961 struct btrfs_key ins;
962 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000963 unsigned clear_bits;
964 unsigned long page_ops;
965 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500966 int ret = 0;
967
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200968 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400969 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500970 ret = -EINVAL;
971 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400972 }
Chris Mason771ed682008-11-06 22:02:51 -0500973
Qu Wenruofda28322013-02-26 08:10:22 +0000974 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500975 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800976 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500977
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200978 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400979
Chris Mason771ed682008-11-06 22:02:51 -0500980 if (start == 0) {
981 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200982 ret = cow_file_range_inline(inode, start, end, 0,
983 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500984 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400985 /*
986 * We use DO_ACCOUNTING here because we need the
987 * delalloc_release_metadata to be run _after_ we drop
988 * our outstanding extent for clearing delalloc for this
989 * range.
990 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300991 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400992 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400993 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
994 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400995 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
996 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500997 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300998 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500999 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001000 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001001 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001002 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001003 }
1004 }
Chris Masonc8b97812008-10-29 14:49:59 -04001005
Josef Bacik4b46fce2010-05-23 11:00:55 -04001006 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001007 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1008 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001009
Anand Jain3752d222018-02-15 12:29:38 +08001010 while (num_bytes > 0) {
1011 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001012 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001013 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001014 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001015 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001016 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001017 cur_alloc_size = ins.offset;
1018 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001019
Chris Mason771ed682008-11-06 22:02:51 -05001020 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001021 em = create_io_em(inode, start, ins.offset, /* len */
1022 start, /* orig_start */
1023 ins.objectid, /* block_start */
1024 ins.offset, /* block_len */
1025 ins.offset, /* orig_block_len */
1026 ram_size, /* ram_bytes */
1027 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001028 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001029 if (IS_ERR(em)) {
1030 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001031 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001032 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001033 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001034
Chris Masone6dcd2d2008-07-17 12:53:50 -04001035 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001036 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001037 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001038 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001039
Yan Zheng17d217f2008-12-12 10:03:38 -05001040 if (root->root_key.objectid ==
1041 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1042 ret = btrfs_reloc_clone_csums(inode, start,
1043 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001044 /*
1045 * Only drop cache here, and process as normal.
1046 *
1047 * We must not allow extent_clear_unlock_delalloc()
1048 * at out_unlock label to free meta of this ordered
1049 * extent, as its meta should be freed by
1050 * btrfs_finish_ordered_io().
1051 *
1052 * So we must continue until @start is increased to
1053 * skip current ordered extent.
1054 */
Josef Bacik00361582013-08-14 14:02:47 -04001055 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001056 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1057 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001058 }
1059
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001060 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001061
Chris Masonc8b97812008-10-29 14:49:59 -04001062 /* we're not doing compressed IO, don't unlock the first
1063 * page (which the caller expects to stay locked), don't
1064 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001065 *
1066 * Do set the Private2 bit so we know this page was properly
1067 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001068 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001069 page_ops = unlock ? PAGE_UNLOCK : 0;
1070 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001071
Josef Bacikc2790a22013-07-29 11:20:47 -04001072 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001073 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001074 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001075 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001076 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001077 if (num_bytes < cur_alloc_size)
1078 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001079 else
Anand Jain3752d222018-02-15 12:29:38 +08001080 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001081 alloc_hint = ins.objectid + ins.offset;
1082 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001083 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001084
1085 /*
1086 * btrfs_reloc_clone_csums() error, since start is increased
1087 * extent_clear_unlock_delalloc() at out_unlock label won't
1088 * free metadata of current ordered extent, we're OK to exit.
1089 */
1090 if (ret)
1091 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001092 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001093out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001094 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001095
Filipe Mananad9f85962014-08-25 10:43:00 +01001096out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001097 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001098out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001099 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001100 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001101out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001102 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1103 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001104 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1105 PAGE_END_WRITEBACK;
1106 /*
1107 * If we reserved an extent for our delalloc range (or a subrange) and
1108 * failed to create the respective ordered extent, then it means that
1109 * when we reserved the extent we decremented the extent's size from
1110 * the data space_info's bytes_may_use counter and incremented the
1111 * space_info's bytes_reserved counter by the same amount. We must make
1112 * sure extent_clear_unlock_delalloc() does not try to decrement again
1113 * the data space_info's bytes_may_use counter, therefore we do not pass
1114 * it the flag EXTENT_CLEAR_DATA_RESV.
1115 */
1116 if (extent_reserved) {
1117 extent_clear_unlock_delalloc(inode, start,
1118 start + cur_alloc_size,
Filipe Mananaa315e682017-03-06 23:04:20 +00001119 locked_page,
1120 clear_bits,
1121 page_ops);
1122 start += cur_alloc_size;
1123 if (start >= end)
1124 goto out;
1125 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001126 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001127 clear_bits | EXTENT_CLEAR_DATA_RESV,
1128 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001129 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001130}
Chris Masonc8b97812008-10-29 14:49:59 -04001131
Chris Mason771ed682008-11-06 22:02:51 -05001132/*
1133 * work queue call back to started compression on a file and pages
1134 */
1135static noinline void async_cow_start(struct btrfs_work *work)
1136{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001137 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001138 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001139
Nikolay Borisovb5326272019-03-12 17:20:25 +02001140 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001141
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001142 compressed_extents = compress_file_range(async_chunk);
1143 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001144 btrfs_add_delayed_iput(async_chunk->inode);
1145 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001146 }
Chris Mason771ed682008-11-06 22:02:51 -05001147}
1148
1149/*
1150 * work queue call back to submit previously compressed pages
1151 */
1152static noinline void async_cow_submit(struct btrfs_work *work)
1153{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001154 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1155 work);
1156 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001157 unsigned long nr_pages;
1158
Nikolay Borisovb5326272019-03-12 17:20:25 +02001159 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001160 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001161
David Sterba093258e2018-02-26 16:15:17 +01001162 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001163 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001164 5 * SZ_1M)
1165 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001166
Nikolay Borisov4546d172019-01-03 10:50:03 +02001167 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001168 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001169 * in which case we don't have anything to submit, yet we need to
1170 * always adjust ->async_delalloc_pages as its paired with the init
1171 * happening in cow_file_range_async
1172 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001173 if (async_chunk->inode)
1174 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001175}
Chris Masonc8b97812008-10-29 14:49:59 -04001176
Chris Mason771ed682008-11-06 22:02:51 -05001177static noinline void async_cow_free(struct btrfs_work *work)
1178{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001179 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001180
Nikolay Borisovb5326272019-03-12 17:20:25 +02001181 async_chunk = container_of(work, struct async_chunk, work);
1182 if (async_chunk->inode)
1183 btrfs_add_delayed_iput(async_chunk->inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001184 /*
1185 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001186 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001187 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001188 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001189 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001190}
1191
1192static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1193 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001194 unsigned long *nr_written,
1195 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001196{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001197 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001198 struct async_cow *ctx;
1199 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001200 unsigned long nr_pages;
1201 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001202 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1203 int i;
1204 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001205 unsigned nofs_flag;
Chris Mason771ed682008-11-06 22:02:51 -05001206
Nikolay Borisov69684c52019-03-12 17:20:28 +02001207 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001208
1209 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1210 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1211 num_chunks = 1;
1212 should_compress = false;
1213 } else {
1214 should_compress = true;
1215 }
1216
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001217 nofs_flag = memalloc_nofs_save();
1218 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1219 memalloc_nofs_restore(nofs_flag);
1220
Nikolay Borisov97db1202019-03-12 17:20:24 +02001221 if (!ctx) {
1222 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1223 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1224 EXTENT_DO_ACCOUNTING;
1225 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1226 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1227 PAGE_SET_ERROR;
1228
Nikolay Borisov74e91942019-07-17 16:18:16 +03001229 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001230 clear_bits, page_ops);
1231 return -ENOMEM;
1232 }
1233
1234 async_chunk = ctx->chunks;
1235 atomic_set(&ctx->num_chunks, num_chunks);
1236
1237 for (i = 0; i < num_chunks; i++) {
1238 if (should_compress)
1239 cur_end = min(end, start + SZ_512K - 1);
1240 else
1241 cur_end = end;
1242
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001243 /*
1244 * igrab is called higher up in the call chain, take only the
1245 * lightweight reference for the callback lifetime
1246 */
1247 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001248 async_chunk[i].pending = &ctx->num_chunks;
1249 async_chunk[i].inode = inode;
1250 async_chunk[i].start = start;
1251 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001252 async_chunk[i].locked_page = locked_page;
1253 async_chunk[i].write_flags = write_flags;
1254 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001255
Nikolay Borisov97db1202019-03-12 17:20:24 +02001256 btrfs_init_work(&async_chunk[i].work,
Liu Bo9e0af232014-08-15 23:36:53 +08001257 btrfs_delalloc_helper,
1258 async_cow_start, async_cow_submit,
1259 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001260
Nikolay Borisov97db1202019-03-12 17:20:24 +02001261 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001262 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001263
Nikolay Borisov97db1202019-03-12 17:20:24 +02001264 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001265
Chris Mason771ed682008-11-06 22:02:51 -05001266 *nr_written += nr_pages;
1267 start = cur_end + 1;
1268 }
1269 *page_started = 1;
1270 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001271}
1272
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001273static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001274 u64 bytenr, u64 num_bytes)
1275{
1276 int ret;
1277 struct btrfs_ordered_sum *sums;
1278 LIST_HEAD(list);
1279
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001280 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001281 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001282 if (ret == 0 && list_empty(&list))
1283 return 0;
1284
1285 while (!list_empty(&list)) {
1286 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1287 list_del(&sums->list);
1288 kfree(sums);
1289 }
Liu Bo58113752018-01-31 17:09:13 -07001290 if (ret < 0)
1291 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001292 return 1;
1293}
1294
Chris Masond352ac62008-09-29 15:18:18 -04001295/*
1296 * when nowcow writeback call back. This checks for snapshots or COW copies
1297 * of the extents that exist in the file, and COWs the file as required.
1298 *
1299 * If no cow copies or snapshots exist, we write directly to the existing
1300 * blocks on disk
1301 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001302static noinline int run_delalloc_nocow(struct inode *inode,
1303 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001304 u64 start, u64 end, int *page_started, int force,
1305 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001306{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001307 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001308 struct btrfs_root *root = BTRFS_I(inode)->root;
1309 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001310 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001312 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001313 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 u64 cow_start;
1315 u64 cur_offset;
1316 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001317 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 u64 disk_bytenr;
1319 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001320 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001321 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 int extent_type;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001323 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001324 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 int nocow;
1326 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001327 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001328 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001329
1330 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001331 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001332 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001333 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001334 EXTENT_DO_ACCOUNTING |
1335 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001336 PAGE_CLEAR_DIRTY |
1337 PAGE_SET_WRITEBACK |
1338 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001339 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001340 }
Li Zefan82d59022011-04-20 10:33:24 +08001341
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001342 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001343
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 cow_start = (u64)-1;
1345 cur_offset = start;
1346 while (1) {
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;
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1352 leaf = path->nodes[0];
1353 btrfs_item_key_to_cpu(leaf, &found_key,
1354 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001355 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001356 found_key.type == BTRFS_EXTENT_DATA_KEY)
1357 path->slots[0]--;
1358 }
1359 check_prev = 0;
1360next_slot:
1361 leaf = path->nodes[0];
1362 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1363 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001364 if (ret < 0) {
1365 if (cow_start != (u64)-1)
1366 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001367 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001368 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 if (ret > 0)
1370 break;
1371 leaf = path->nodes[0];
1372 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001373
Yan Zheng80ff3852008-10-30 14:20:02 -04001374 nocow = 0;
1375 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001376 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001378
Filipe Manana1d512cb2015-11-09 00:33:58 +00001379 if (found_key.objectid > ino)
1380 break;
1381 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1382 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1383 path->slots[0]++;
1384 goto next_slot;
1385 }
1386 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001387 found_key.offset > end)
1388 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001389
Yan Zheng80ff3852008-10-30 14:20:02 -04001390 if (found_key.offset > cur_offset) {
1391 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001392 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001393 goto out_check;
1394 }
Chris Masonc31f8832008-01-08 15:46:31 -05001395
Yan Zheng80ff3852008-10-30 14:20:02 -04001396 fi = btrfs_item_ptr(leaf, path->slots[0],
1397 struct btrfs_file_extent_item);
1398 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001399
Josef Bacikcc95bef2013-04-04 14:31:27 -04001400 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001401 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1402 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001403 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001404 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001405 extent_end = found_key.offset +
1406 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001407 disk_num_bytes =
1408 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001409 if (extent_end <= start) {
1410 path->slots[0]++;
1411 goto next_slot;
1412 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001413 if (disk_bytenr == 0)
1414 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001415 if (btrfs_file_extent_compression(leaf, fi) ||
1416 btrfs_file_extent_encryption(leaf, fi) ||
1417 btrfs_file_extent_other_encoding(leaf, fi))
1418 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001419 /*
1420 * Do the same check as in btrfs_cross_ref_exist but
1421 * without the unnecessary search.
1422 */
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001423 if (!nolock &&
1424 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001425 btrfs_root_last_snapshot(&root->root_item))
1426 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001427 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1428 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001429 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001430 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001431 ret = btrfs_cross_ref_exist(root, ino,
1432 found_key.offset -
1433 extent_offset, disk_bytenr);
1434 if (ret) {
1435 /*
1436 * ret could be -EIO if the above fails to read
1437 * metadata.
1438 */
1439 if (ret < 0) {
1440 if (cow_start != (u64)-1)
1441 cur_offset = cow_start;
1442 goto error;
1443 }
1444
1445 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001446 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001447 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001448 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001449 disk_bytenr += cur_offset - found_key.offset;
1450 num_bytes = min(end + 1, extent_end) - cur_offset;
1451 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001452 * if there are pending snapshots for this root,
1453 * we fall into common COW way.
1454 */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001455 if (!nolock && atomic_read(&root->snapshot_force_cow))
1456 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001457 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001458 * force cow if csum exists in the range.
1459 * this ensure that csum for a given extent are
1460 * either valid or do not exist.
1461 */
Liu Bo58113752018-01-31 17:09:13 -07001462 ret = csum_exist_in_range(fs_info, disk_bytenr,
1463 num_bytes);
1464 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001465 /*
1466 * ret could be -EIO if the above fails to read
1467 * metadata.
1468 */
1469 if (ret < 0) {
1470 if (cow_start != (u64)-1)
1471 cur_offset = cow_start;
1472 goto error;
1473 }
1474 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001475 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001476 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001477 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001478 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001479 nocow = 1;
1480 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1481 extent_end = found_key.offset +
Qu Wenruoe41ca582018-06-06 15:41:49 +08001482 btrfs_file_extent_ram_bytes(leaf, fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001483 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001484 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001485 } else {
Arnd Bergmann290342f2019-03-25 14:02:25 +01001486 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001487 }
1488out_check:
1489 if (extent_end <= start) {
1490 path->slots[0]++;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001491 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001492 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001493 goto next_slot;
1494 }
1495 if (!nocow) {
1496 if (cow_start == (u64)-1)
1497 cow_start = cur_offset;
1498 cur_offset = extent_end;
1499 if (cur_offset > end)
1500 break;
1501 path->slots[0]++;
1502 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001503 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001504
David Sterbab3b4aa72011-04-21 01:20:15 +02001505 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001506 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001507 ret = cow_file_range(inode, locked_page,
1508 cow_start, found_key.offset - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001509 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001510 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001511 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001512 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001513 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001514 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001515 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001516 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001517 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001518
Yan Zhengd899e052008-10-30 14:25:28 -04001519 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001520 u64 orig_start = found_key.offset - extent_offset;
1521
1522 em = create_io_em(inode, cur_offset, num_bytes,
1523 orig_start,
1524 disk_bytenr, /* block_start */
1525 num_bytes, /* block_len */
1526 disk_num_bytes, /* orig_block_len */
1527 ram_bytes, BTRFS_COMPRESS_NONE,
1528 BTRFS_ORDERED_PREALLOC);
1529 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001530 if (nocow)
1531 btrfs_dec_nocow_writers(fs_info,
1532 disk_bytenr);
1533 ret = PTR_ERR(em);
1534 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001535 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001536 free_extent_map(em);
1537 }
1538
1539 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001540 type = BTRFS_ORDERED_PREALLOC;
1541 } else {
1542 type = BTRFS_ORDERED_NOCOW;
1543 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001544
1545 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001546 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001547 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001548 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001549 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001550
Yan, Zhengefa56462010-05-16 10:49:59 -04001551 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001552 BTRFS_DATA_RELOC_TREE_OBJECTID)
1553 /*
1554 * Error handled later, as we must prevent
1555 * extent_clear_unlock_delalloc() in error handler
1556 * from freeing metadata of created ordered extent.
1557 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001558 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1559 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001560
Josef Bacikc2790a22013-07-29 11:20:47 -04001561 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001562 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001563 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001564 EXTENT_DELALLOC |
1565 EXTENT_CLEAR_DATA_RESV,
1566 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1567
Yan Zheng80ff3852008-10-30 14:20:02 -04001568 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001569
1570 /*
1571 * btrfs_reloc_clone_csums() error, now we're OK to call error
1572 * handler, as metadata for created ordered extent will only
1573 * be freed by btrfs_finish_ordered_io().
1574 */
1575 if (ret)
1576 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001577 if (cur_offset > end)
1578 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001579 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001580 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001581
Robbie Ko506481b2018-10-30 18:04:04 +08001582 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001583 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001584
Yan Zheng80ff3852008-10-30 14:20:02 -04001585 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001586 cur_offset = end;
Nikolay Borisov74e91942019-07-17 16:18:16 +03001587 ret = cow_file_range(inode, locked_page, cow_start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001588 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001589 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001590 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001591 }
1592
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001593error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001594 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001595 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001596 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001597 EXTENT_DELALLOC | EXTENT_DEFRAG |
1598 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1599 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001600 PAGE_SET_WRITEBACK |
1601 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001602 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001603 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001604}
1605
Wang Shilong47059d92014-07-03 18:22:07 +08001606static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1607{
1608
1609 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1610 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1611 return 0;
1612
1613 /*
1614 * @defrag_bytes is a hint value, no spinlock held here,
1615 * if is not zero, it means the file is defragging.
1616 * Force cow if given extent needs to be defragged.
1617 */
1618 if (BTRFS_I(inode)->defrag_bytes &&
1619 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1620 EXTENT_DEFRAG, 0, NULL))
1621 return 1;
1622
1623 return 0;
1624}
1625
Chris Masond352ac62008-09-29 15:18:18 -04001626/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001627 * Function to process delayed allocation (create CoW) for ranges which are
1628 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001629 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001630int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001631 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1632 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001633{
Chris Masonbe20aa92007-12-17 20:14:01 -05001634 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001635 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001636 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001637
Wang Shilong47059d92014-07-03 18:22:07 +08001638 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001639 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001640 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001641 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001642 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001643 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001644 } else if (!inode_can_compress(inode) ||
1645 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001646 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001647 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001648 } else {
1649 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1650 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001651 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001652 page_started, nr_written,
1653 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001654 }
Qu Wenruo524272602017-03-08 10:25:52 +08001655 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001656 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1657 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001658 return ret;
1659}
1660
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001661void btrfs_split_delalloc_extent(struct inode *inode,
1662 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001663{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001664 u64 size;
1665
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001666 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001667 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001668 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001669
Josef Bacikdcab6a32015-02-11 15:08:59 -05001670 size = orig->end - orig->start + 1;
1671 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001672 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001673 u64 new_size;
1674
1675 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001676 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001677 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001678 */
1679 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001680 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001681 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001682 num_extents += count_max_extents(new_size);
1683 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001684 return;
1685 }
1686
Josef Bacik9e0baf62011-07-15 15:16:44 +00001687 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001688 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001689 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001690}
1691
1692/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001693 * Handle merged delayed allocation extents so we can keep track of new extents
1694 * that are just merged onto old extents, such as when we are doing sequential
1695 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001696 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001697void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1698 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001699{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001700 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001701 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001702
Josef Bacik9ed74f22009-09-11 16:12:44 -04001703 /* not delalloc, ignore it */
1704 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001705 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001706
Josef Bacik8461a3d2015-03-13 15:12:08 -04001707 if (new->start > other->start)
1708 new_size = new->end - other->start + 1;
1709 else
1710 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001711
1712 /* we're not bigger than the max, unreserve the space and go */
1713 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1714 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001715 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001716 spin_unlock(&BTRFS_I(inode)->lock);
1717 return;
1718 }
1719
1720 /*
Josef Bacikba117212015-03-13 15:01:24 -04001721 * We have to add up either side to figure out how many extents were
1722 * accounted for before we merged into one big extent. If the number of
1723 * extents we accounted for is <= the amount we need for the new range
1724 * then we can return, otherwise drop. Think of it like this
1725 *
1726 * [ 4k][MAX_SIZE]
1727 *
1728 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1729 * need 2 outstanding extents, on one side we have 1 and the other side
1730 * we have 1 so they are == and we can return. But in this case
1731 *
1732 * [MAX_SIZE+4k][MAX_SIZE+4k]
1733 *
1734 * Each range on their own accounts for 2 extents, but merged together
1735 * they are only 3 extents worth of accounting, so we need to drop in
1736 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001737 */
Josef Bacikba117212015-03-13 15:01:24 -04001738 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001739 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001740 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001741 num_extents += count_max_extents(old_size);
1742 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001743 return;
1744
Josef Bacik9e0baf62011-07-15 15:16:44 +00001745 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001746 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001747 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001748}
1749
Miao Xieeb73c1b2013-05-15 07:48:22 +00001750static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1751 struct inode *inode)
1752{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001753 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1754
Miao Xieeb73c1b2013-05-15 07:48:22 +00001755 spin_lock(&root->delalloc_lock);
1756 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1757 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1758 &root->delalloc_inodes);
1759 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1760 &BTRFS_I(inode)->runtime_flags);
1761 root->nr_delalloc_inodes++;
1762 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001763 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001764 BUG_ON(!list_empty(&root->delalloc_root));
1765 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001766 &fs_info->delalloc_roots);
1767 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001768 }
1769 }
1770 spin_unlock(&root->delalloc_lock);
1771}
1772
Nikolay Borisov2b877332018-04-27 12:21:51 +03001773
1774void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1775 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001776{
David Sterba3ffbd682018-06-29 10:56:42 +02001777 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001778
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001779 if (!list_empty(&inode->delalloc_inodes)) {
1780 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001781 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001782 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001783 root->nr_delalloc_inodes--;
1784 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001785 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001786 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001787 BUG_ON(list_empty(&root->delalloc_root));
1788 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001789 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001790 }
1791 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001792}
1793
1794static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1795 struct btrfs_inode *inode)
1796{
1797 spin_lock(&root->delalloc_lock);
1798 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001799 spin_unlock(&root->delalloc_lock);
1800}
1801
Chris Masond352ac62008-09-29 15:18:18 -04001802/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001803 * Properly track delayed allocation bytes in the inode and to maintain the
1804 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001805 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001806void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1807 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001808{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001809 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1810
Wang Shilong47059d92014-07-03 18:22:07 +08001811 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1812 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001813 /*
1814 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001815 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001816 * bit, which is only set or cleared with irqs on
1817 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001818 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001819 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001820 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001821 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001822 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001823
Josef Bacik8b62f872017-10-19 14:15:55 -04001824 spin_lock(&BTRFS_I(inode)->lock);
1825 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1826 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001827
Josef Bacik6a3891c2015-03-16 17:38:52 -04001828 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001829 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001830 return;
1831
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001832 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1833 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001834 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001835 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001836 if (*bits & EXTENT_DEFRAG)
1837 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001838 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001839 &BTRFS_I(inode)->runtime_flags))
1840 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001841 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001842 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001843
1844 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1845 (*bits & EXTENT_DELALLOC_NEW)) {
1846 spin_lock(&BTRFS_I(inode)->lock);
1847 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1848 state->start;
1849 spin_unlock(&BTRFS_I(inode)->lock);
1850 }
Chris Mason291d6732008-01-29 15:55:23 -05001851}
1852
Chris Masond352ac62008-09-29 15:18:18 -04001853/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001854 * Once a range is no longer delalloc this function ensures that proper
1855 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001856 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001857void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1858 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001859{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001860 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1861 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001862 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001863 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001864
Filipe Manana4a4b9642017-07-27 19:52:55 +01001865 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1866 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001867 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001868 spin_unlock(&inode->lock);
1869 }
Wang Shilong47059d92014-07-03 18:22:07 +08001870
Chris Mason75eff682008-12-15 15:54:40 -05001871 /*
1872 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001873 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001874 * bit, which is only set or cleared with irqs on
1875 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001876 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001877 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001878 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001879
Josef Bacik8b62f872017-10-19 14:15:55 -04001880 spin_lock(&inode->lock);
1881 btrfs_mod_outstanding_extents(inode, -num_extents);
1882 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001883
Josef Bacikb6d08f02013-09-27 14:57:43 -04001884 /*
1885 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001886 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001887 * error.
1888 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001889 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001890 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001891 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001892
Josef Bacik6a3891c2015-03-16 17:38:52 -04001893 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001894 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001895 return;
1896
Filipe Mananaa315e682017-03-06 23:04:20 +00001897 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1898 do_list && !(state->state & EXTENT_NORESERVE) &&
1899 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001900 btrfs_free_reserved_data_space_noquota(
1901 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001902 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001903
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001904 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1905 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001906 spin_lock(&inode->lock);
1907 inode->delalloc_bytes -= len;
1908 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001909 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001910 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001911 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001912 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001913 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001914
1915 if ((state->state & EXTENT_DELALLOC_NEW) &&
1916 (*bits & EXTENT_DELALLOC_NEW)) {
1917 spin_lock(&inode->lock);
1918 ASSERT(inode->new_delalloc_bytes >= len);
1919 inode->new_delalloc_bytes -= len;
1920 spin_unlock(&inode->lock);
1921 }
Chris Mason291d6732008-01-29 15:55:23 -05001922}
1923
Chris Masond352ac62008-09-29 15:18:18 -04001924/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001925 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
1926 * in a chunk's stripe. This function ensures that bios do not span a
1927 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07001928 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001929 * @page - The page we are about to add to the bio
1930 * @size - size we want to add to the bio
1931 * @bio - bio we want to ensure is smaller than a stripe
1932 * @bio_flags - flags of the bio
1933 *
1934 * return 1 if page cannot be added to the bio
1935 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07001936 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001937 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001938int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
1939 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001940{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001941 struct inode *inode = page->mapping->host;
1942 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001943 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001944 u64 length = 0;
1945 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001946 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03001947 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04001948
Chris Mason771ed682008-11-06 22:02:51 -05001949 if (bio_flags & EXTENT_BIO_COMPRESSED)
1950 return 0;
1951
Kent Overstreet4f024f32013-10-11 15:44:27 -07001952 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001953 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03001954 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
1955 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07001956 if (ret < 0)
1957 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03001958
1959 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001960 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001961 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001962}
1963
Chris Masond352ac62008-09-29 15:18:18 -04001964/*
1965 * in order to insert checksums into the metadata in large chunks,
1966 * we wait until bio submission time. All the pages in the bio are
1967 * checksummed and sums are attached onto the ordered extent record.
1968 *
1969 * At IO completion time the cums attached on the ordered extent record
1970 * are inserted into the btree
1971 */
David Sterbad0ee3932018-03-08 14:35:48 +01001972static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001973 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001974{
Josef Bacikc6100a42017-05-05 11:57:13 -04001975 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001976 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001977
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001978 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001979 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001980 return 0;
1981}
Chris Masone0156402008-04-16 11:15:20 -04001982
Chris Mason4a69a412008-11-06 22:03:00 -05001983/*
Chris Masoncad321a2008-12-17 14:51:42 -05001984 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001985 * on write, or reading the csums from the tree before a read.
1986 *
1987 * Rules about async/sync submit,
1988 * a) read: sync submit
1989 *
1990 * b) write without checksum: sync submit
1991 *
1992 * c) write with checksum:
1993 * c-1) if bio is issued by fsync: sync submit
1994 * (sync_writers != 0)
1995 *
1996 * c-2) if root is reloc root: sync submit
1997 * (only in case of buffered IO)
1998 *
1999 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002000 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002001static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002002 int mirror_num,
2003 unsigned long bio_flags)
2004
Chris Mason44b8bd72008-04-16 11:14:51 -04002005{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002006 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002007 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302008 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002009 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002010 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002011 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002012
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002013 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002014
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002015 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302016 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002017
Mike Christie37226b22016-06-05 14:31:52 -05002018 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002019 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002020 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002021 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002022
Chris Masond20f7042008-12-08 16:58:54 -05002023 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002024 ret = btrfs_submit_compressed_read(inode, bio,
2025 mirror_num,
2026 bio_flags);
2027 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002028 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002029 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002030 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002031 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002032 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002033 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002034 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002035 /* csum items have already been cloned */
2036 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2037 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002038 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002039 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002040 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002041 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002042 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002043 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002044 if (ret)
2045 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002046 }
2047
Chris Mason0b86a832008-03-24 15:01:56 -04002048mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002049 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002050
2051out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002052 if (ret) {
2053 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002054 bio_endio(bio);
2055 }
Stefan Behrens61891922012-11-05 18:51:52 +01002056 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002057}
Chris Mason6885f302008-02-20 16:11:05 -05002058
Chris Masond352ac62008-09-29 15:18:18 -04002059/*
2060 * given a list of ordered sums record them in the inode. This happens
2061 * at IO completion time based on sums calculated at bio submission time.
2062 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002063static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002064 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002065{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002066 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002067 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002068
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002069 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002070 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002071 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002072 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002073 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002074 if (ret)
2075 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002076 }
2077 return 0;
2078}
2079
Josef Bacik2ac55d42010-02-03 19:33:23 +00002080int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002081 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002082 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002083{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002084 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002085 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002086 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002087}
2088
Chris Masond352ac62008-09-29 15:18:18 -04002089/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002090struct btrfs_writepage_fixup {
2091 struct page *page;
2092 struct btrfs_work work;
2093};
2094
Christoph Hellwigb2950862008-12-02 09:54:17 -05002095static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002096{
2097 struct btrfs_writepage_fixup *fixup;
2098 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002099 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002100 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002101 struct page *page;
2102 struct inode *inode;
2103 u64 page_start;
2104 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002105 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002106
2107 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2108 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002109again:
Chris Mason247e7432008-07-17 12:53:51 -04002110 lock_page(page);
2111 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2112 ClearPageChecked(page);
2113 goto out_page;
2114 }
2115
2116 inode = page->mapping->host;
2117 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002118 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002119
David Sterbaff13db42015-12-03 14:30:40 +01002120 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002121 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002122
2123 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002124 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002125 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002126
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002127 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002128 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002129 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002130 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002131 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002132 unlock_page(page);
2133 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002134 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002135 goto again;
2136 }
Chris Mason247e7432008-07-17 12:53:51 -04002137
Qu Wenruo364ecf32017-02-27 15:10:38 +08002138 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002139 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002140 if (ret) {
2141 mapping_set_error(page->mapping, ret);
2142 end_extent_writepage(page, ret, page_start, page_end);
2143 ClearPageChecked(page);
2144 goto out;
2145 }
2146
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002147 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002148 &cached_state);
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002149 if (ret) {
2150 mapping_set_error(page->mapping, ret);
2151 end_extent_writepage(page, ret, page_start, page_end);
2152 ClearPageChecked(page);
2153 goto out;
2154 }
2155
Chris Mason247e7432008-07-17 12:53:51 -04002156 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002157 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002158 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002159out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002160 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002161 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002162out_page:
2163 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002164 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002165 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002166 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002167}
2168
2169/*
2170 * There are a few paths in the higher layers of the kernel that directly
2171 * set the page dirty bit without asking the filesystem if it is a
2172 * good idea. This causes problems because we want to make sure COW
2173 * properly happens and the data=ordered rules are followed.
2174 *
Chris Masonc8b97812008-10-29 14:49:59 -04002175 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002176 * hasn't been properly setup for IO. We kick off an async process
2177 * to fix it up. The async helper will wait for ordered extents, set
2178 * the delalloc bit and make it safe to write the page.
2179 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002180int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002181{
2182 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002183 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002184 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002185
Chris Mason8b62b722009-09-02 16:53:46 -04002186 /* this page is properly in the ordered list */
2187 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002188 return 0;
2189
2190 if (PageChecked(page))
2191 return -EAGAIN;
2192
2193 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2194 if (!fixup)
2195 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002196
Chris Mason247e7432008-07-17 12:53:51 -04002197 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002198 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002199 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2200 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002201 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002202 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002203 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002204}
2205
Yan Zhengd899e052008-10-30 14:25:28 -04002206static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2207 struct inode *inode, u64 file_pos,
2208 u64 disk_bytenr, u64 disk_num_bytes,
2209 u64 num_bytes, u64 ram_bytes,
2210 u8 compression, u8 encryption,
2211 u16 other_encoding, int extent_type)
2212{
2213 struct btrfs_root *root = BTRFS_I(inode)->root;
2214 struct btrfs_file_extent_item *fi;
2215 struct btrfs_path *path;
2216 struct extent_buffer *leaf;
2217 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002218 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002219 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002220 int ret;
2221
2222 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002223 if (!path)
2224 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002225
Chris Masona1ed8352009-09-11 12:27:37 -04002226 /*
2227 * we may be replacing one extent in the tree with another.
2228 * The new extent is pinned in the extent map, and we don't want
2229 * to drop it from the cache until it is completely in the btree.
2230 *
2231 * So, tell btrfs_drop_extents to leave this extent in the cache.
2232 * the caller is expected to unpin it and allow it to be merged
2233 * with the others.
2234 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002235 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2236 file_pos + num_bytes, NULL, 0,
2237 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002238 if (ret)
2239 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002240
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002241 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002242 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002243 ins.offset = file_pos;
2244 ins.type = BTRFS_EXTENT_DATA_KEY;
2245
2246 path->leave_spinning = 1;
2247 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2248 sizeof(*fi));
2249 if (ret)
2250 goto out;
2251 }
Yan Zhengd899e052008-10-30 14:25:28 -04002252 leaf = path->nodes[0];
2253 fi = btrfs_item_ptr(leaf, path->slots[0],
2254 struct btrfs_file_extent_item);
2255 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2256 btrfs_set_file_extent_type(leaf, fi, extent_type);
2257 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2258 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2259 btrfs_set_file_extent_offset(leaf, fi, 0);
2260 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2261 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2262 btrfs_set_file_extent_compression(leaf, fi, compression);
2263 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2264 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002265
Yan Zhengd899e052008-10-30 14:25:28 -04002266 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002267 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002268
2269 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002270
2271 ins.objectid = disk_bytenr;
2272 ins.offset = disk_num_bytes;
2273 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002274
Qu Wenruo297d7502015-09-08 17:08:37 +08002275 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002276 * Release the reserved range from inode dirty range map, as it is
2277 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002278 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002279 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2280 if (ret < 0)
2281 goto out;
2282 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002283 ret = btrfs_alloc_reserved_file_extent(trans, root,
2284 btrfs_ino(BTRFS_I(inode)),
2285 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002286out:
Yan Zhengd899e052008-10-30 14:25:28 -04002287 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002288
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002289 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002290}
2291
Liu Bo38c227d2013-01-29 03:18:40 +00002292/* snapshot-aware defrag */
2293struct sa_defrag_extent_backref {
2294 struct rb_node node;
2295 struct old_sa_defrag_extent *old;
2296 u64 root_id;
2297 u64 inum;
2298 u64 file_pos;
2299 u64 extent_offset;
2300 u64 num_bytes;
2301 u64 generation;
2302};
2303
2304struct old_sa_defrag_extent {
2305 struct list_head list;
2306 struct new_sa_defrag_extent *new;
2307
2308 u64 extent_offset;
2309 u64 bytenr;
2310 u64 offset;
2311 u64 len;
2312 int count;
2313};
2314
2315struct new_sa_defrag_extent {
2316 struct rb_root root;
2317 struct list_head head;
2318 struct btrfs_path *path;
2319 struct inode *inode;
2320 u64 file_pos;
2321 u64 len;
2322 u64 bytenr;
2323 u64 disk_len;
2324 u8 compress_type;
2325};
2326
2327static int backref_comp(struct sa_defrag_extent_backref *b1,
2328 struct sa_defrag_extent_backref *b2)
2329{
2330 if (b1->root_id < b2->root_id)
2331 return -1;
2332 else if (b1->root_id > b2->root_id)
2333 return 1;
2334
2335 if (b1->inum < b2->inum)
2336 return -1;
2337 else if (b1->inum > b2->inum)
2338 return 1;
2339
2340 if (b1->file_pos < b2->file_pos)
2341 return -1;
2342 else if (b1->file_pos > b2->file_pos)
2343 return 1;
2344
2345 /*
2346 * [------------------------------] ===> (a range of space)
2347 * |<--->| |<---->| =============> (fs/file tree A)
2348 * |<---------------------------->| ===> (fs/file tree B)
2349 *
2350 * A range of space can refer to two file extents in one tree while
2351 * refer to only one file extent in another tree.
2352 *
2353 * So we may process a disk offset more than one time(two extents in A)
2354 * and locate at the same extent(one extent in B), then insert two same
2355 * backrefs(both refer to the extent in B).
2356 */
2357 return 0;
2358}
2359
2360static void backref_insert(struct rb_root *root,
2361 struct sa_defrag_extent_backref *backref)
2362{
2363 struct rb_node **p = &root->rb_node;
2364 struct rb_node *parent = NULL;
2365 struct sa_defrag_extent_backref *entry;
2366 int ret;
2367
2368 while (*p) {
2369 parent = *p;
2370 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2371
2372 ret = backref_comp(backref, entry);
2373 if (ret < 0)
2374 p = &(*p)->rb_left;
2375 else
2376 p = &(*p)->rb_right;
2377 }
2378
2379 rb_link_node(&backref->node, parent, p);
2380 rb_insert_color(&backref->node, root);
2381}
2382
2383/*
2384 * Note the backref might has changed, and in this case we just return 0.
2385 */
2386static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2387 void *ctx)
2388{
2389 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002390 struct old_sa_defrag_extent *old = ctx;
2391 struct new_sa_defrag_extent *new = old->new;
2392 struct btrfs_path *path = new->path;
2393 struct btrfs_key key;
2394 struct btrfs_root *root;
2395 struct sa_defrag_extent_backref *backref;
2396 struct extent_buffer *leaf;
2397 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002398 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002399 int slot;
2400 int ret;
2401 u64 extent_offset;
2402 u64 num_bytes;
2403
2404 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002405 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002406 return 0;
2407
2408 key.objectid = root_id;
2409 key.type = BTRFS_ROOT_ITEM_KEY;
2410 key.offset = (u64)-1;
2411
Liu Bo38c227d2013-01-29 03:18:40 +00002412 root = btrfs_read_fs_root_no_name(fs_info, &key);
2413 if (IS_ERR(root)) {
2414 if (PTR_ERR(root) == -ENOENT)
2415 return 0;
2416 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002417 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002418 inum, offset, root_id);
2419 return PTR_ERR(root);
2420 }
2421
2422 key.objectid = inum;
2423 key.type = BTRFS_EXTENT_DATA_KEY;
2424 if (offset > (u64)-1 << 32)
2425 key.offset = 0;
2426 else
2427 key.offset = offset;
2428
2429 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302430 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002431 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002432 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002433
2434 while (1) {
2435 cond_resched();
2436
2437 leaf = path->nodes[0];
2438 slot = path->slots[0];
2439
2440 if (slot >= btrfs_header_nritems(leaf)) {
2441 ret = btrfs_next_leaf(root, path);
2442 if (ret < 0) {
2443 goto out;
2444 } else if (ret > 0) {
2445 ret = 0;
2446 goto out;
2447 }
2448 continue;
2449 }
2450
2451 path->slots[0]++;
2452
2453 btrfs_item_key_to_cpu(leaf, &key, slot);
2454
2455 if (key.objectid > inum)
2456 goto out;
2457
2458 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2459 continue;
2460
2461 extent = btrfs_item_ptr(leaf, slot,
2462 struct btrfs_file_extent_item);
2463
2464 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2465 continue;
2466
Liu Boe68afa42013-07-01 22:13:26 +08002467 /*
2468 * 'offset' refers to the exact key.offset,
2469 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2470 * (key.offset - extent_offset).
2471 */
2472 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002473 continue;
2474
Liu Boe68afa42013-07-01 22:13:26 +08002475 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002476 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002477
Liu Bo38c227d2013-01-29 03:18:40 +00002478 if (extent_offset >= old->extent_offset + old->offset +
2479 old->len || extent_offset + num_bytes <=
2480 old->extent_offset + old->offset)
2481 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002482 break;
2483 }
2484
2485 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2486 if (!backref) {
2487 ret = -ENOENT;
2488 goto out;
2489 }
2490
2491 backref->root_id = root_id;
2492 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002493 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002494 backref->num_bytes = num_bytes;
2495 backref->extent_offset = extent_offset;
2496 backref->generation = btrfs_file_extent_generation(leaf, extent);
2497 backref->old = old;
2498 backref_insert(&new->root, backref);
2499 old->count++;
2500out:
2501 btrfs_release_path(path);
2502 WARN_ON(ret);
2503 return ret;
2504}
2505
2506static noinline bool record_extent_backrefs(struct btrfs_path *path,
2507 struct new_sa_defrag_extent *new)
2508{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002509 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002510 struct old_sa_defrag_extent *old, *tmp;
2511 int ret;
2512
2513 new->path = path;
2514
2515 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002516 ret = iterate_inodes_from_logical(old->bytenr +
2517 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002518 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002519 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002520 if (ret < 0 && ret != -ENOENT)
2521 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002522
2523 /* no backref to be processed for this extent */
2524 if (!old->count) {
2525 list_del(&old->list);
2526 kfree(old);
2527 }
2528 }
2529
2530 if (list_empty(&new->head))
2531 return false;
2532
2533 return true;
2534}
2535
2536static int relink_is_mergable(struct extent_buffer *leaf,
2537 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002538 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002539{
Liu Bo116e0022013-08-02 16:30:40 +08002540 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002541 return 0;
2542
2543 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2544 return 0;
2545
Liu Bo116e0022013-08-02 16:30:40 +08002546 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2547 return 0;
2548
2549 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002550 btrfs_file_extent_other_encoding(leaf, fi))
2551 return 0;
2552
2553 return 1;
2554}
2555
2556/*
2557 * Note the backref might has changed, and in this case we just return 0.
2558 */
2559static noinline int relink_extent_backref(struct btrfs_path *path,
2560 struct sa_defrag_extent_backref *prev,
2561 struct sa_defrag_extent_backref *backref)
2562{
2563 struct btrfs_file_extent_item *extent;
2564 struct btrfs_file_extent_item *item;
2565 struct btrfs_ordered_extent *ordered;
2566 struct btrfs_trans_handle *trans;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002567 struct btrfs_ref ref = { 0 };
Liu Bo38c227d2013-01-29 03:18:40 +00002568 struct btrfs_root *root;
2569 struct btrfs_key key;
2570 struct extent_buffer *leaf;
2571 struct old_sa_defrag_extent *old = backref->old;
2572 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002573 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002574 struct inode *inode;
2575 struct extent_state *cached = NULL;
2576 int ret = 0;
2577 u64 start;
2578 u64 len;
2579 u64 lock_start;
2580 u64 lock_end;
2581 bool merge = false;
2582 int index;
2583
2584 if (prev && prev->root_id == backref->root_id &&
2585 prev->inum == backref->inum &&
2586 prev->file_pos + prev->num_bytes == backref->file_pos)
2587 merge = true;
2588
2589 /* step 1: get root */
2590 key.objectid = backref->root_id;
2591 key.type = BTRFS_ROOT_ITEM_KEY;
2592 key.offset = (u64)-1;
2593
Liu Bo38c227d2013-01-29 03:18:40 +00002594 index = srcu_read_lock(&fs_info->subvol_srcu);
2595
2596 root = btrfs_read_fs_root_no_name(fs_info, &key);
2597 if (IS_ERR(root)) {
2598 srcu_read_unlock(&fs_info->subvol_srcu, index);
2599 if (PTR_ERR(root) == -ENOENT)
2600 return 0;
2601 return PTR_ERR(root);
2602 }
Liu Bo38c227d2013-01-29 03:18:40 +00002603
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002604 if (btrfs_root_readonly(root)) {
2605 srcu_read_unlock(&fs_info->subvol_srcu, index);
2606 return 0;
2607 }
2608
Liu Bo38c227d2013-01-29 03:18:40 +00002609 /* step 2: get inode */
2610 key.objectid = backref->inum;
2611 key.type = BTRFS_INODE_ITEM_KEY;
2612 key.offset = 0;
2613
2614 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2615 if (IS_ERR(inode)) {
2616 srcu_read_unlock(&fs_info->subvol_srcu, index);
2617 return 0;
2618 }
2619
2620 srcu_read_unlock(&fs_info->subvol_srcu, index);
2621
2622 /* step 3: relink backref */
2623 lock_start = backref->file_pos;
2624 lock_end = backref->file_pos + backref->num_bytes - 1;
2625 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002626 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002627
2628 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2629 if (ordered) {
2630 btrfs_put_ordered_extent(ordered);
2631 goto out_unlock;
2632 }
2633
2634 trans = btrfs_join_transaction(root);
2635 if (IS_ERR(trans)) {
2636 ret = PTR_ERR(trans);
2637 goto out_unlock;
2638 }
2639
2640 key.objectid = backref->inum;
2641 key.type = BTRFS_EXTENT_DATA_KEY;
2642 key.offset = backref->file_pos;
2643
2644 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2645 if (ret < 0) {
2646 goto out_free_path;
2647 } else if (ret > 0) {
2648 ret = 0;
2649 goto out_free_path;
2650 }
2651
2652 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2653 struct btrfs_file_extent_item);
2654
2655 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2656 backref->generation)
2657 goto out_free_path;
2658
2659 btrfs_release_path(path);
2660
2661 start = backref->file_pos;
2662 if (backref->extent_offset < old->extent_offset + old->offset)
2663 start += old->extent_offset + old->offset -
2664 backref->extent_offset;
2665
2666 len = min(backref->extent_offset + backref->num_bytes,
2667 old->extent_offset + old->offset + old->len);
2668 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2669
2670 ret = btrfs_drop_extents(trans, root, inode, start,
2671 start + len, 1);
2672 if (ret)
2673 goto out_free_path;
2674again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002675 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002676 key.type = BTRFS_EXTENT_DATA_KEY;
2677 key.offset = start;
2678
Liu Boa09a0a72013-03-11 09:20:58 +00002679 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002680 if (merge) {
2681 struct btrfs_file_extent_item *fi;
2682 u64 extent_len;
2683 struct btrfs_key found_key;
2684
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002685 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002686 if (ret < 0)
2687 goto out_free_path;
2688
2689 path->slots[0]--;
2690 leaf = path->nodes[0];
2691 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2692
2693 fi = btrfs_item_ptr(leaf, path->slots[0],
2694 struct btrfs_file_extent_item);
2695 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2696
Liu Bo116e0022013-08-02 16:30:40 +08002697 if (extent_len + found_key.offset == start &&
2698 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002699 btrfs_set_file_extent_num_bytes(leaf, fi,
2700 extent_len + len);
2701 btrfs_mark_buffer_dirty(leaf);
2702 inode_add_bytes(inode, len);
2703
2704 ret = 1;
2705 goto out_free_path;
2706 } else {
2707 merge = false;
2708 btrfs_release_path(path);
2709 goto again;
2710 }
2711 }
2712
2713 ret = btrfs_insert_empty_item(trans, root, path, &key,
2714 sizeof(*extent));
2715 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002716 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002717 goto out_free_path;
2718 }
2719
2720 leaf = path->nodes[0];
2721 item = btrfs_item_ptr(leaf, path->slots[0],
2722 struct btrfs_file_extent_item);
2723 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2724 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2725 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2726 btrfs_set_file_extent_num_bytes(leaf, item, len);
2727 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2728 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2729 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2730 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2731 btrfs_set_file_extent_encryption(leaf, item, 0);
2732 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2733
2734 btrfs_mark_buffer_dirty(leaf);
2735 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002736 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002737
Qu Wenruo82fa1132019-04-04 14:45:35 +08002738 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr,
2739 new->disk_len, 0);
2740 btrfs_init_data_ref(&ref, backref->root_id, backref->inum,
2741 new->file_pos); /* start - extent_offset */
2742 ret = btrfs_inc_extent_ref(trans, &ref);
Liu Bo38c227d2013-01-29 03:18:40 +00002743 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002744 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002745 goto out_free_path;
2746 }
2747
2748 ret = 1;
2749out_free_path:
2750 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002751 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002752 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002753out_unlock:
2754 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002755 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002756 iput(inode);
2757 return ret;
2758}
2759
Liu Bo6f519562013-10-29 10:45:05 +08002760static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2761{
2762 struct old_sa_defrag_extent *old, *tmp;
2763
2764 if (!new)
2765 return;
2766
2767 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002768 kfree(old);
2769 }
2770 kfree(new);
2771}
2772
Liu Bo38c227d2013-01-29 03:18:40 +00002773static void relink_file_extents(struct new_sa_defrag_extent *new)
2774{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002775 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002776 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002777 struct sa_defrag_extent_backref *backref;
2778 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002779 struct rb_node *node;
2780 int ret;
2781
Liu Bo38c227d2013-01-29 03:18:40 +00002782 path = btrfs_alloc_path();
2783 if (!path)
2784 return;
2785
2786 if (!record_extent_backrefs(path, new)) {
2787 btrfs_free_path(path);
2788 goto out;
2789 }
2790 btrfs_release_path(path);
2791
2792 while (1) {
2793 node = rb_first(&new->root);
2794 if (!node)
2795 break;
2796 rb_erase(node, &new->root);
2797
2798 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2799
2800 ret = relink_extent_backref(path, prev, backref);
2801 WARN_ON(ret < 0);
2802
2803 kfree(prev);
2804
2805 if (ret == 1)
2806 prev = backref;
2807 else
2808 prev = NULL;
2809 cond_resched();
2810 }
2811 kfree(prev);
2812
2813 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002814out:
Liu Bo6f519562013-10-29 10:45:05 +08002815 free_sa_defrag_extent(new);
2816
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002817 atomic_dec(&fs_info->defrag_running);
2818 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002819}
2820
2821static struct new_sa_defrag_extent *
2822record_old_file_extents(struct inode *inode,
2823 struct btrfs_ordered_extent *ordered)
2824{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002825 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002826 struct btrfs_root *root = BTRFS_I(inode)->root;
2827 struct btrfs_path *path;
2828 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002829 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002830 struct new_sa_defrag_extent *new;
2831 int ret;
2832
2833 new = kmalloc(sizeof(*new), GFP_NOFS);
2834 if (!new)
2835 return NULL;
2836
2837 new->inode = inode;
2838 new->file_pos = ordered->file_offset;
2839 new->len = ordered->len;
2840 new->bytenr = ordered->start;
2841 new->disk_len = ordered->disk_len;
2842 new->compress_type = ordered->compress_type;
2843 new->root = RB_ROOT;
2844 INIT_LIST_HEAD(&new->head);
2845
2846 path = btrfs_alloc_path();
2847 if (!path)
2848 goto out_kfree;
2849
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002850 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002851 key.type = BTRFS_EXTENT_DATA_KEY;
2852 key.offset = new->file_pos;
2853
2854 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2855 if (ret < 0)
2856 goto out_free_path;
2857 if (ret > 0 && path->slots[0] > 0)
2858 path->slots[0]--;
2859
2860 /* find out all the old extents for the file range */
2861 while (1) {
2862 struct btrfs_file_extent_item *extent;
2863 struct extent_buffer *l;
2864 int slot;
2865 u64 num_bytes;
2866 u64 offset;
2867 u64 end;
2868 u64 disk_bytenr;
2869 u64 extent_offset;
2870
2871 l = path->nodes[0];
2872 slot = path->slots[0];
2873
2874 if (slot >= btrfs_header_nritems(l)) {
2875 ret = btrfs_next_leaf(root, path);
2876 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002877 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002878 else if (ret > 0)
2879 break;
2880 continue;
2881 }
2882
2883 btrfs_item_key_to_cpu(l, &key, slot);
2884
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002885 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002886 break;
2887 if (key.type != BTRFS_EXTENT_DATA_KEY)
2888 break;
2889 if (key.offset >= new->file_pos + new->len)
2890 break;
2891
2892 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2893
2894 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2895 if (key.offset + num_bytes < new->file_pos)
2896 goto next;
2897
2898 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2899 if (!disk_bytenr)
2900 goto next;
2901
2902 extent_offset = btrfs_file_extent_offset(l, extent);
2903
2904 old = kmalloc(sizeof(*old), GFP_NOFS);
2905 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002906 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002907
2908 offset = max(new->file_pos, key.offset);
2909 end = min(new->file_pos + new->len, key.offset + num_bytes);
2910
2911 old->bytenr = disk_bytenr;
2912 old->extent_offset = extent_offset;
2913 old->offset = offset - key.offset;
2914 old->len = end - offset;
2915 old->new = new;
2916 old->count = 0;
2917 list_add_tail(&old->list, &new->head);
2918next:
2919 path->slots[0]++;
2920 cond_resched();
2921 }
2922
2923 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002924 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002925
2926 return new;
2927
Liu Bo38c227d2013-01-29 03:18:40 +00002928out_free_path:
2929 btrfs_free_path(path);
2930out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002931 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002932 return NULL;
2933}
2934
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002935static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002936 u64 start, u64 len)
2937{
2938 struct btrfs_block_group_cache *cache;
2939
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002940 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002941 ASSERT(cache);
2942
2943 spin_lock(&cache->lock);
2944 cache->delalloc_bytes -= len;
2945 spin_unlock(&cache->lock);
2946
2947 btrfs_put_block_group(cache);
2948}
2949
Chris Masond352ac62008-09-29 15:18:18 -04002950/* as ordered data IO finishes, this gets called so we can finish
2951 * an ordered extent if the range of bytes in the file it covers are
2952 * fully written.
2953 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002954static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002955{
Josef Bacik5fd02042012-05-02 14:00:54 -04002956 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002957 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002958 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002959 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002960 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002961 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002962 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002963 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002964 int ret = 0;
2965 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002966 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002967 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002968 bool range_locked = false;
2969 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002970 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002971
2972 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2973 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2974 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2975 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002976
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002977 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002978
Josef Bacik5fd02042012-05-02 14:00:54 -04002979 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2980 ret = -EIO;
2981 goto out;
2982 }
2983
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002984 btrfs_free_io_failure_record(BTRFS_I(inode),
2985 ordered_extent->file_offset,
2986 ordered_extent->file_offset +
2987 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002988
Josef Bacik77cef2e2013-08-29 13:57:21 -04002989 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2990 truncated = true;
2991 logical_len = ordered_extent->truncated_len;
2992 /* Truncated the entire extent, don't bother adding */
2993 if (!logical_len)
2994 goto out;
2995 }
2996
Yan, Zhengc2167752009-11-12 09:34:21 +00002997 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002998 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002999
3000 /*
3001 * For mwrite(mmap + memset to write) case, we still reserve
3002 * space for NOCOW range.
3003 * As NOCOW won't cause a new delayed ref, just free the space
3004 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003005 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003006 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003007 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3008 if (nolock)
3009 trans = btrfs_join_transaction_nolock(root);
3010 else
3011 trans = btrfs_join_transaction(root);
3012 if (IS_ERR(trans)) {
3013 ret = PTR_ERR(trans);
3014 trans = NULL;
3015 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003016 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003017 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003018 ret = btrfs_update_inode_fallback(trans, root, inode);
3019 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003020 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003021 goto out;
3022 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003024 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003025 lock_extent_bits(io_tree, ordered_extent->file_offset,
3026 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003027 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003028
Liu Bo38c227d2013-01-29 03:18:40 +00003029 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3030 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003031 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003032 if (ret) {
3033 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003034 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003035 /* the inode is shared */
3036 new = record_old_file_extents(inode, ordered_extent);
3037
3038 clear_extent_bit(io_tree, ordered_extent->file_offset,
3039 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003040 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003041 }
3042
Josef Bacik0cb59c92010-07-02 12:14:14 -04003043 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003044 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003045 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003046 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003047 if (IS_ERR(trans)) {
3048 ret = PTR_ERR(trans);
3049 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003050 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003051 }
Chris Masona79b7d42014-05-22 16:18:52 -07003052
Josef Bacik69fe2d72017-10-19 14:15:57 -04003053 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003054
Chris Masonc8b97812008-10-29 14:49:59 -04003055 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003056 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003057 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003058 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003059 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3060 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003061 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003062 ordered_extent->file_offset,
3063 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003064 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003065 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003066 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003067 ret = insert_reserved_file_extent(trans, inode,
3068 ordered_extent->file_offset,
3069 ordered_extent->start,
3070 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003071 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003072 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003073 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003074 if (!ret) {
3075 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003076 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003077 ordered_extent->start,
3078 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003079 }
Yan Zhengd899e052008-10-30 14:25:28 -04003080 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003081 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3082 ordered_extent->file_offset, ordered_extent->len,
3083 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003084 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003085 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003086 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003087 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003088
Nikolay Borisovac01f262018-01-08 10:59:43 +02003089 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3090 if (ret) {
3091 btrfs_abort_transaction(trans, ret);
3092 goto out;
3093 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003094
Josef Bacik6c760c02012-11-09 10:53:21 -05003095 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3096 ret = btrfs_update_inode_fallback(trans, root, inode);
3097 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003098 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003099 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003100 }
3101 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003102out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003103 if (range_locked || clear_new_delalloc_bytes) {
3104 unsigned int clear_bits = 0;
3105
3106 if (range_locked)
3107 clear_bits |= EXTENT_LOCKED;
3108 if (clear_new_delalloc_bytes)
3109 clear_bits |= EXTENT_DELALLOC_NEW;
3110 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3111 ordered_extent->file_offset,
3112 ordered_extent->file_offset +
3113 ordered_extent->len - 1,
3114 clear_bits,
3115 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003116 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003117 }
3118
Miao Xiea698d0752012-09-20 01:51:59 -06003119 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003120 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003121
Josef Bacik77cef2e2013-08-29 13:57:21 -04003122 if (ret || truncated) {
3123 u64 start, end;
3124
3125 if (truncated)
3126 start = ordered_extent->file_offset + logical_len;
3127 else
3128 start = ordered_extent->file_offset;
3129 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003130 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003131
3132 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003133 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003134
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003135 /*
3136 * If the ordered extent had an IOERR or something else went
3137 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003138 * back to the allocator. We only free the extent in the
3139 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003140 *
3141 * If we made it past insert_reserved_file_extent before we
3142 * errored out then we don't need to do this as the accounting
3143 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003144 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003145 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003146 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003147 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003148 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003149 btrfs_free_reserved_extent(fs_info,
3150 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003151 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003152 }
3153
3154
Josef Bacik5fd02042012-05-02 14:00:54 -04003155 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003156 * This needs to be done to make sure anybody waiting knows we are done
3157 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003158 */
3159 btrfs_remove_ordered_extent(inode, ordered_extent);
3160
Liu Bo38c227d2013-01-29 03:18:40 +00003161 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003162 if (new) {
3163 if (ret) {
3164 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003165 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003166 } else {
3167 relink_file_extents(new);
3168 }
3169 }
Liu Bo38c227d2013-01-29 03:18:40 +00003170
Chris Masone6dcd2d2008-07-17 12:53:50 -04003171 /* once for us */
3172 btrfs_put_ordered_extent(ordered_extent);
3173 /* once for the tree */
3174 btrfs_put_ordered_extent(ordered_extent);
3175
Josef Bacik5fd02042012-05-02 14:00:54 -04003176 return ret;
3177}
3178
3179static void finish_ordered_fn(struct btrfs_work *work)
3180{
3181 struct btrfs_ordered_extent *ordered_extent;
3182 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3183 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003184}
3185
Nikolay Borisovc6297322018-11-08 10:18:08 +02003186void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3187 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003188{
Josef Bacik5fd02042012-05-02 14:00:54 -04003189 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003190 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003191 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003192 struct btrfs_workqueue *wq;
3193 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003194
liubo1abe9b82011-03-24 11:18:59 +00003195 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3196
Chris Mason8b62b722009-09-02 16:53:46 -04003197 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003198 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3199 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003200 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003201
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003202 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003203 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003204 func = btrfs_freespace_write_helper;
3205 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003206 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003207 func = btrfs_endio_write_helper;
3208 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003209
Liu Bo9e0af232014-08-15 23:36:53 +08003210 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3211 NULL);
3212 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003213}
3214
Miao Xiedc380ae2014-09-12 18:43:55 +08003215static int __readpage_endio_check(struct inode *inode,
3216 struct btrfs_io_bio *io_bio,
3217 int icsum, struct page *page,
3218 int pgoff, u64 start, size_t len)
3219{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003220 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3221 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08003222 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003223 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
3224 u8 *csum_expected;
3225 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08003226
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003227 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08003228
3229 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003230 shash->tfm = fs_info->csum_shash;
3231
3232 crypto_shash_init(shash);
3233 crypto_shash_update(shash, kaddr + pgoff, len);
3234 crypto_shash_final(shash, csum);
3235
3236 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08003237 goto zeroit;
3238
3239 kunmap_atomic(kaddr);
3240 return 0;
3241zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02003242 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3243 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003244 memset(kaddr + pgoff, 1, len);
3245 flush_dcache_page(page);
3246 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003247 return -EIO;
3248}
3249
Chris Masond352ac62008-09-29 15:18:18 -04003250/*
Chris Masond352ac62008-09-29 15:18:18 -04003251 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003252 * if there's a match, we allow the bio to finish. If not, the code in
3253 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003254 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003255static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3256 u64 phy_offset, struct page *page,
3257 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003258{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003259 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003260 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003261 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003262 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003263
Chris Masond20f7042008-12-08 16:58:54 -05003264 if (PageChecked(page)) {
3265 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003266 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003267 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003268
3269 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003270 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003271
Yan Zheng17d217f2008-12-12 10:03:38 -05003272 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003273 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003274 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003275 return 0;
3276 }
3277
Miao Xiefacc8a222013-07-25 19:22:34 +08003278 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003279 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3280 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003281}
Chris Masonb888db22007-08-27 16:49:44 -04003282
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003283/*
3284 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3285 *
3286 * @inode: The inode we want to perform iput on
3287 *
3288 * This function uses the generic vfs_inode::i_count to track whether we should
3289 * just decrement it (in case it's > 1) or if this is the last iput then link
3290 * the inode to the delayed iput machinery. Delayed iputs are processed at
3291 * transaction commit time/superblock commit/cleaner kthread.
3292 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003293void btrfs_add_delayed_iput(struct inode *inode)
3294{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003295 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003296 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003297
3298 if (atomic_add_unless(&inode->i_count, -1, 1))
3299 return;
3300
Josef Bacik034f7842018-12-03 11:06:52 -05003301 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003302 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003303 ASSERT(list_empty(&binode->delayed_iput));
3304 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003305 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003306 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3307 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003308}
3309
Josef Bacik63611e72019-06-18 10:59:18 -04003310static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3311 struct btrfs_inode *inode)
3312{
3313 list_del_init(&inode->delayed_iput);
3314 spin_unlock(&fs_info->delayed_iput_lock);
3315 iput(&inode->vfs_inode);
3316 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3317 wake_up(&fs_info->delayed_iputs_wait);
3318 spin_lock(&fs_info->delayed_iput_lock);
3319}
3320
3321static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3322 struct btrfs_inode *inode)
3323{
3324 if (!list_empty(&inode->delayed_iput)) {
3325 spin_lock(&fs_info->delayed_iput_lock);
3326 if (!list_empty(&inode->delayed_iput))
3327 run_delayed_iput_locked(fs_info, inode);
3328 spin_unlock(&fs_info->delayed_iput_lock);
3329 }
3330}
3331
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003332void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003333{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003334
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003335 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003336 while (!list_empty(&fs_info->delayed_iputs)) {
3337 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003338
David Sterba8089fe62015-11-19 14:15:51 +01003339 inode = list_first_entry(&fs_info->delayed_iputs,
3340 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003341 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003342 }
David Sterba8089fe62015-11-19 14:15:51 +01003343 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003344}
3345
Josef Bacik034f7842018-12-03 11:06:52 -05003346/**
3347 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3348 * @fs_info - the fs_info for this fs
3349 * @return - EINTR if we were killed, 0 if nothing's pending
3350 *
3351 * This will wait on any delayed iputs that are currently running with KILLABLE
3352 * set. Once they are all done running we will return, unless we are killed in
3353 * which case we return EINTR. This helps in user operations like fallocate etc
3354 * that might get blocked on the iputs.
3355 */
3356int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3357{
3358 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3359 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3360 if (ret)
3361 return -EINTR;
3362 return 0;
3363}
3364
Yan, Zhengd68fc572010-05-16 10:49:58 -04003365/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003366 * This creates an orphan entry for the given inode in case something goes wrong
3367 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003368 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003369int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003370 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003371{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003373
Omar Sandoval27919062018-05-11 13:13:37 -07003374 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3375 if (ret && ret != -EEXIST) {
3376 btrfs_abort_transaction(trans, ret);
3377 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003378 }
3379
Yan, Zhengd68fc572010-05-16 10:49:58 -04003380 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003381}
3382
3383/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003384 * We have done the delete so we can go ahead and remove the orphan item for
3385 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003386 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003387static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003388 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003389{
Omar Sandoval27919062018-05-11 13:13:37 -07003390 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003391}
3392
3393/*
3394 * this cleans up any orphans that may be left on the list from the last use
3395 * of this root.
3396 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003397int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003398{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003399 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003400 struct btrfs_path *path;
3401 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003402 struct btrfs_key key, found_key;
3403 struct btrfs_trans_handle *trans;
3404 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003405 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003406 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003407
Yan, Zhengd68fc572010-05-16 10:49:58 -04003408 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003409 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003410
3411 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003412 if (!path) {
3413 ret = -ENOMEM;
3414 goto out;
3415 }
David Sterbae4058b52015-11-27 16:31:35 +01003416 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003417
3418 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003419 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003420 key.offset = (u64)-1;
3421
Josef Bacik7b128762008-07-24 12:17:14 -04003422 while (1) {
3423 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003424 if (ret < 0)
3425 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003426
3427 /*
3428 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003429 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003430 * find the key and see if we have stuff that matches
3431 */
3432 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003433 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003434 if (path->slots[0] == 0)
3435 break;
3436 path->slots[0]--;
3437 }
3438
3439 /* pull out the item */
3440 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003441 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3442
3443 /* make sure the item matches what we want */
3444 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3445 break;
David Sterba962a2982014-06-04 18:41:45 +02003446 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003447 break;
3448
3449 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003450 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003451
3452 /*
3453 * this is where we are basically btrfs_lookup, without the
3454 * crossing root thing. we store the inode number in the
3455 * offset of the orphan item.
3456 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003457
3458 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003459 btrfs_err(fs_info,
3460 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003461 ret = -EINVAL;
3462 goto out;
3463 }
3464
3465 last_objectid = found_key.offset;
3466
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003467 found_key.objectid = found_key.offset;
3468 found_key.type = BTRFS_INODE_ITEM_KEY;
3469 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003470 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303471 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003472 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003473 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003474
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003475 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003476 struct btrfs_root *dead_root;
3477 struct btrfs_fs_info *fs_info = root->fs_info;
3478 int is_dead_root = 0;
3479
3480 /*
3481 * this is an orphan in the tree root. Currently these
3482 * could come from 2 sources:
3483 * a) a snapshot deletion in progress
3484 * b) a free space cache inode
3485 * We need to distinguish those two, as the snapshot
3486 * orphan must not get deleted.
3487 * find_dead_roots already ran before us, so if this
3488 * is a snapshot deletion, we should find the root
3489 * in the dead_roots list
3490 */
3491 spin_lock(&fs_info->trans_lock);
3492 list_for_each_entry(dead_root, &fs_info->dead_roots,
3493 root_list) {
3494 if (dead_root->root_key.objectid ==
3495 found_key.objectid) {
3496 is_dead_root = 1;
3497 break;
3498 }
3499 }
3500 spin_unlock(&fs_info->trans_lock);
3501 if (is_dead_root) {
3502 /* prevent this orphan from being found again */
3503 key.offset = found_key.objectid - 1;
3504 continue;
3505 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003506
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003507 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003508
Josef Bacika8c9e572011-09-21 16:55:59 -04003509 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003510 * If we have an inode with links, there are a couple of
3511 * possibilities. Old kernels (before v3.12) used to create an
3512 * orphan item for truncate indicating that there were possibly
3513 * extent items past i_size that needed to be deleted. In v3.12,
3514 * truncate was changed to update i_size in sync with the extent
3515 * items, but the (useless) orphan item was still created. Since
3516 * v4.18, we don't create the orphan item for truncate at all.
3517 *
3518 * So, this item could mean that we need to do a truncate, but
3519 * only if this filesystem was last used on a pre-v3.12 kernel
3520 * and was not cleanly unmounted. The odds of that are quite
3521 * slim, and it's a pain to do the truncate now, so just delete
3522 * the orphan item.
3523 *
3524 * It's also possible that this orphan item was supposed to be
3525 * deleted but wasn't. The inode number may have been reused,
3526 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003527 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003528 if (ret == -ENOENT || inode->i_nlink) {
3529 if (!ret)
3530 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003531 trans = btrfs_start_transaction(root, 1);
3532 if (IS_ERR(trans)) {
3533 ret = PTR_ERR(trans);
3534 goto out;
3535 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003536 btrfs_debug(fs_info, "auto deleting %Lu",
3537 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003538 ret = btrfs_del_orphan_item(trans, root,
3539 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003540 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003541 if (ret)
3542 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003543 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003544 }
Josef Bacik7b128762008-07-24 12:17:14 -04003545
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003546 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003547
3548 /* this will do delete_inode and everything for us */
3549 iput(inode);
3550 }
Miao Xie3254c872011-11-10 20:45:05 -05003551 /* release the path since we're done with it */
3552 btrfs_release_path(path);
3553
Yan, Zhengd68fc572010-05-16 10:49:58 -04003554 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3555
Omar Sandovala575cee2018-05-11 13:13:38 -07003556 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003557 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003558 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003559 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003560 }
Josef Bacik7b128762008-07-24 12:17:14 -04003561
3562 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003563 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003564
3565out:
3566 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003567 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003568 btrfs_free_path(path);
3569 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003570}
3571
Chris Masond352ac62008-09-29 15:18:18 -04003572/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003573 * very simple check to peek ahead in the leaf looking for xattrs. If we
3574 * don't find any xattrs, we know there can't be any acls.
3575 *
3576 * slot is the slot the inode is in, objectid is the objectid of the inode
3577 */
3578static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003579 int slot, u64 objectid,
3580 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003581{
3582 u32 nritems = btrfs_header_nritems(leaf);
3583 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003584 static u64 xattr_access = 0;
3585 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003586 int scanned = 0;
3587
Josef Bacikf23b5a52013-06-19 10:16:26 -04003588 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003589 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3590 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3591 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3592 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003593 }
3594
Chris Mason46a53cc2009-04-27 11:47:50 -04003595 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003596 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003597 while (slot < nritems) {
3598 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3599
3600 /* we found a different objectid, there must not be acls */
3601 if (found_key.objectid != objectid)
3602 return 0;
3603
3604 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003605 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003606 if (*first_xattr_slot == -1)
3607 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003608 if (found_key.offset == xattr_access ||
3609 found_key.offset == xattr_default)
3610 return 1;
3611 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003612
3613 /*
3614 * we found a key greater than an xattr key, there can't
3615 * be any acls later on
3616 */
3617 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3618 return 0;
3619
3620 slot++;
3621 scanned++;
3622
3623 /*
3624 * it goes inode, inode backrefs, xattrs, extents,
3625 * so if there are a ton of hard links to an inode there can
3626 * be a lot of backrefs. Don't waste time searching too hard,
3627 * this is just an optimization
3628 */
3629 if (scanned >= 8)
3630 break;
3631 }
3632 /* we hit the end of the leaf before we found an xattr or
3633 * something larger than an xattr. We have to assume the inode
3634 * has acls
3635 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003636 if (*first_xattr_slot == -1)
3637 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003638 return 1;
3639}
3640
3641/*
Chris Masond352ac62008-09-29 15:18:18 -04003642 * read an inode from the btree into the in-memory inode
3643 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003644static int btrfs_read_locked_inode(struct inode *inode,
3645 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003646{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003647 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003648 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003649 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003650 struct btrfs_inode_item *inode_item;
3651 struct btrfs_root *root = BTRFS_I(inode)->root;
3652 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003653 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003654 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003655 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003656 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003657 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003658 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003659
3660 ret = btrfs_fill_inode(inode, &rdev);
3661 if (!ret)
3662 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003663
Filipe Manana4222ea72018-10-24 10:13:03 +01003664 if (!path) {
3665 path = btrfs_alloc_path();
3666 if (!path)
3667 return -ENOMEM;
3668 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003669
Chris Mason39279cc2007-06-12 06:35:45 -04003670 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003671
Chris Mason39279cc2007-06-12 06:35:45 -04003672 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003673 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003674 if (path != in_path)
3675 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003676 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003677 }
Chris Mason39279cc2007-06-12 06:35:45 -04003678
Chris Mason5f39d392007-10-15 16:14:19 -04003679 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003680
3681 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003682 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003683
Chris Mason5f39d392007-10-15 16:14:19 -04003684 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3685 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003686 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003687 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003688 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3689 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003690 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003691
David Sterbaa937b972014-12-12 17:39:12 +01003692 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3693 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003694
David Sterbaa937b972014-12-12 17:39:12 +01003695 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3696 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003697
David Sterbaa937b972014-12-12 17:39:12 +01003698 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3699 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003700
chandan r9cc97d62012-07-04 12:48:07 +05303701 BTRFS_I(inode)->i_otime.tv_sec =
3702 btrfs_timespec_sec(leaf, &inode_item->otime);
3703 BTRFS_I(inode)->i_otime.tv_nsec =
3704 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003705
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003706 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003707 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003708 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3709
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003710 inode_set_iversion_queried(inode,
3711 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003712 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003713 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003714 rdev = btrfs_inode_rdev(leaf, inode_item);
3715
Josef Bacikaec74772008-07-24 12:12:38 -04003716 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003717 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003718
3719cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003720 /*
3721 * If we were modified in the current generation and evicted from memory
3722 * and then re-read we need to do a full sync since we don't have any
3723 * idea about which extents were modified before we were evicted from
3724 * cache.
3725 *
3726 * This is required for both inode re-read from disk and delayed inode
3727 * in delayed_nodes_tree.
3728 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003729 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003730 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3731 &BTRFS_I(inode)->runtime_flags);
3732
Filipe Mananabde6c242015-07-24 00:00:19 +01003733 /*
3734 * We don't persist the id of the transaction where an unlink operation
3735 * against the inode was last made. So here we assume the inode might
3736 * have been evicted, and therefore the exact value of last_unlink_trans
3737 * lost, and set it to last_trans to avoid metadata inconsistencies
3738 * between the inode and its parent if the inode is fsync'ed and the log
3739 * replayed. For example, in the scenario:
3740 *
3741 * touch mydir/foo
3742 * ln mydir/foo mydir/bar
3743 * sync
3744 * unlink mydir/bar
3745 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3746 * xfs_io -c fsync mydir/foo
3747 * <power failure>
3748 * mount fs, triggers fsync log replay
3749 *
3750 * We must make sure that when we fsync our inode foo we also log its
3751 * parent inode, otherwise after log replay the parent still has the
3752 * dentry with the "bar" name but our inode foo has a link count of 1
3753 * and doesn't have an inode ref with the name "bar" anymore.
3754 *
3755 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003756 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003757 * transaction commits on fsync if our inode is a directory, or if our
3758 * inode is not a directory, logging its parent unnecessarily.
3759 */
3760 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3761
Miao Xie67de1172013-12-26 13:07:06 +08003762 path->slots[0]++;
3763 if (inode->i_nlink != 1 ||
3764 path->slots[0] >= btrfs_header_nritems(leaf))
3765 goto cache_acl;
3766
3767 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003768 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003769 goto cache_acl;
3770
3771 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3772 if (location.type == BTRFS_INODE_REF_KEY) {
3773 struct btrfs_inode_ref *ref;
3774
3775 ref = (struct btrfs_inode_ref *)ptr;
3776 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3777 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3778 struct btrfs_inode_extref *extref;
3779
3780 extref = (struct btrfs_inode_extref *)ptr;
3781 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3782 extref);
3783 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003784cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003785 /*
3786 * try to precache a NULL acl entry for files that don't have
3787 * any xattrs or acls
3788 */
Li Zefan33345d012011-04-20 10:31:50 +08003789 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003790 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003791 if (first_xattr_slot != -1) {
3792 path->slots[0] = first_xattr_slot;
3793 ret = btrfs_load_inode_props(inode, path);
3794 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003795 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003796 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003797 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003798 root->root_key.objectid, ret);
3799 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003800 if (path != in_path)
3801 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003802
Al Viro72c04902009-06-24 16:58:48 -04003803 if (!maybe_acls)
3804 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003805
Chris Mason39279cc2007-06-12 06:35:45 -04003806 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003807 case S_IFREG:
3808 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003809 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003810 inode->i_fop = &btrfs_file_operations;
3811 inode->i_op = &btrfs_file_inode_operations;
3812 break;
3813 case S_IFDIR:
3814 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003815 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003816 break;
3817 case S_IFLNK:
3818 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003819 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003820 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003821 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003822 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003823 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003824 init_special_inode(inode, inode->i_mode, rdev);
3825 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003826 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003827
David Sterba7b6a2212018-03-26 18:40:21 +02003828 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003829 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003830}
3831
Chris Masond352ac62008-09-29 15:18:18 -04003832/*
3833 * given a leaf and an inode, copy the inode fields into the leaf
3834 */
Chris Masone02119d2008-09-05 16:13:11 -04003835static void fill_inode_item(struct btrfs_trans_handle *trans,
3836 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003837 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003838 struct inode *inode)
3839{
Liu Bo51fab692012-12-27 09:01:21 +00003840 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003841
Liu Bo51fab692012-12-27 09:01:21 +00003842 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003843
Liu Bo51fab692012-12-27 09:01:21 +00003844 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3845 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3846 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3847 &token);
3848 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3849 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003850
David Sterbaa937b972014-12-12 17:39:12 +01003851 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003852 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003853 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003854 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003855
David Sterbaa937b972014-12-12 17:39:12 +01003856 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003857 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003858 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003859 inode->i_mtime.tv_nsec, &token);
3860
David Sterbaa937b972014-12-12 17:39:12 +01003861 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003862 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003863 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003864 inode->i_ctime.tv_nsec, &token);
3865
chandan r9cc97d62012-07-04 12:48:07 +05303866 btrfs_set_token_timespec_sec(leaf, &item->otime,
3867 BTRFS_I(inode)->i_otime.tv_sec, &token);
3868 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3869 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3870
Liu Bo51fab692012-12-27 09:01:21 +00003871 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3872 &token);
3873 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3874 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003875 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3876 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003877 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3878 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3879 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3880 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003881}
3882
Chris Masond352ac62008-09-29 15:18:18 -04003883/*
3884 * copy everything in the in-memory inode into the btree.
3885 */
Chris Mason21151332011-11-10 20:39:08 -05003886static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003887 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003888{
3889 struct btrfs_inode_item *inode_item;
3890 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003891 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003892 int ret;
3893
3894 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003895 if (!path)
3896 return -ENOMEM;
3897
Chris Masonb9473432009-03-13 11:00:37 -04003898 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003899 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3900 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003901 if (ret) {
3902 if (ret > 0)
3903 ret = -ENOENT;
3904 goto failed;
3905 }
3906
Chris Mason5f39d392007-10-15 16:14:19 -04003907 leaf = path->nodes[0];
3908 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003909 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003910
Chris Masone02119d2008-09-05 16:13:11 -04003911 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003912 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003913 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003914 ret = 0;
3915failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003916 btrfs_free_path(path);
3917 return ret;
3918}
3919
Chris Masond352ac62008-09-29 15:18:18 -04003920/*
Chris Mason21151332011-11-10 20:39:08 -05003921 * copy everything in the in-memory inode into the btree.
3922 */
3923noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3924 struct btrfs_root *root, struct inode *inode)
3925{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003926 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003927 int ret;
3928
3929 /*
3930 * If the inode is a free space inode, we can deadlock during commit
3931 * if we put it into the delayed code.
3932 *
3933 * The data relocation inode should also be directly updated
3934 * without delay
3935 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003936 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003937 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003938 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003939 btrfs_update_root_times(trans, root);
3940
Chris Mason21151332011-11-10 20:39:08 -05003941 ret = btrfs_delayed_update_inode(trans, root, inode);
3942 if (!ret)
3943 btrfs_set_inode_last_trans(trans, inode);
3944 return ret;
3945 }
3946
3947 return btrfs_update_inode_item(trans, root, inode);
3948}
3949
Josef Bacikbe6aef62012-10-22 15:43:12 -04003950noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3951 struct btrfs_root *root,
3952 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003953{
3954 int ret;
3955
3956 ret = btrfs_update_inode(trans, root, inode);
3957 if (ret == -ENOSPC)
3958 return btrfs_update_inode_item(trans, root, inode);
3959 return ret;
3960}
3961
3962/*
Chris Masond352ac62008-09-29 15:18:18 -04003963 * unlink helper that gets used here in inode.c and in the tree logging
3964 * recovery code. It remove a link in a directory with a given name, and
3965 * also drops the back refs in the inode to the directory
3966 */
Al Viro92986792011-03-04 17:14:37 +00003967static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3968 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003969 struct btrfs_inode *dir,
3970 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003971 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003972{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003973 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003974 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003975 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003976 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003977 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003978 u64 ino = btrfs_ino(inode);
3979 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003980
3981 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003982 if (!path) {
3983 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003984 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003985 }
3986
Chris Masonb9473432009-03-13 11:00:37 -04003987 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003988 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003989 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003990 if (IS_ERR_OR_NULL(di)) {
3991 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003992 goto err;
3993 }
Chris Mason39279cc2007-06-12 06:35:45 -04003994 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003995 if (ret)
3996 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003997 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003998
Miao Xie67de1172013-12-26 13:07:06 +08003999 /*
4000 * If we don't have dir index, we have to get it by looking up
4001 * the inode ref, since we get the inode ref, remove it directly,
4002 * it is unnecessary to do delayed deletion.
4003 *
4004 * But if we have dir index, needn't search inode ref to get it.
4005 * Since the inode ref is close to the inode item, it is better
4006 * that we delay to delete it, and just do this deletion when
4007 * we update the inode item.
4008 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004009 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004010 ret = btrfs_delayed_delete_inode_ref(inode);
4011 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004012 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004013 goto skip_backref;
4014 }
4015 }
4016
Li Zefan33345d012011-04-20 10:31:50 +08004017 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4018 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004019 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004020 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004021 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004022 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004023 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004024 goto err;
4025 }
Miao Xie67de1172013-12-26 13:07:06 +08004026skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08004027 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004028 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004029 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004030 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004031 }
Chris Mason39279cc2007-06-12 06:35:45 -04004032
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004033 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4034 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004035 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004036 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004037 goto err;
4038 }
Chris Masone02119d2008-09-05 16:13:11 -04004039
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004040 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4041 index);
Chris Mason6418c962010-10-30 07:34:24 -04004042 if (ret == -ENOENT)
4043 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004044 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004045 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04004046
4047 /*
4048 * If we have a pending delayed iput we could end up with the final iput
4049 * being run in btrfs-cleaner context. If we have enough of these built
4050 * up we can end up burning a lot of time in btrfs-cleaner without any
4051 * way to throttle the unlinks. Since we're currently holding a ref on
4052 * the inode we can run the delayed iput here without any issues as the
4053 * final iput won't be done until after we drop the ref we're currently
4054 * holding.
4055 */
4056 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004057err:
4058 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004059 if (ret)
4060 goto out;
4061
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004062 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004063 inode_inc_iversion(&inode->vfs_inode);
4064 inode_inc_iversion(&dir->vfs_inode);
4065 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4066 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4067 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004068out:
Chris Mason39279cc2007-06-12 06:35:45 -04004069 return ret;
4070}
4071
Al Viro92986792011-03-04 17:14:37 +00004072int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4073 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004074 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004075 const char *name, int name_len)
4076{
4077 int ret;
4078 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4079 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004080 drop_nlink(&inode->vfs_inode);
4081 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004082 }
4083 return ret;
4084}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004085
4086/*
4087 * helper to start transaction for unlink and rmdir.
4088 *
Josef Bacikd52be812013-05-29 14:54:47 -04004089 * unlink and rmdir are special in btrfs, they do not always free space, so
4090 * if we cannot make our reservations the normal way try and see if there is
4091 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4092 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004093 */
Josef Bacikd52be812013-05-29 14:54:47 -04004094static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004095{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004096 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004097
Josef Bacike70bea52011-10-11 14:18:24 -04004098 /*
4099 * 1 for the possible orphan item
4100 * 1 for the dir item
4101 * 1 for the dir index
4102 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004103 * 1 for the inode
4104 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004105 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004106}
4107
Chris Mason39279cc2007-06-12 06:35:45 -04004108static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4109{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004110 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004111 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004112 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004113 int ret;
4114
Josef Bacikd52be812013-05-29 14:54:47 -04004115 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004116 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004117 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004118
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004119 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4120 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004121
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004122 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4123 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4124 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004125 if (ret)
4126 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004127
Yan, Zhenga22285a2010-05-16 10:48:46 -04004128 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004129 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004130 if (ret)
4131 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004132 }
Josef Bacik7b128762008-07-24 12:17:14 -04004133
Tsutomu Itohb5324022011-07-19 07:27:20 +00004134out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004135 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004136 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004137 return ret;
4138}
4139
Misono Tomohirof60a2362018-04-18 11:34:52 +09004140static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004141 struct inode *dir, u64 objectid,
4142 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004143{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004144 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004145 struct btrfs_path *path;
4146 struct extent_buffer *leaf;
4147 struct btrfs_dir_item *di;
4148 struct btrfs_key key;
4149 u64 index;
4150 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004151 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004152
4153 path = btrfs_alloc_path();
4154 if (!path)
4155 return -ENOMEM;
4156
Li Zefan33345d012011-04-20 10:31:50 +08004157 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004158 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004159 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004160 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004161 goto out;
4162 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004163
4164 leaf = path->nodes[0];
4165 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4166 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4167 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004168 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004169 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004170 goto out;
4171 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004172 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004173
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004174 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4175 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004176 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004177 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004178 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004179 goto out;
4180 }
Li Zefan33345d012011-04-20 10:31:50 +08004181 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004182 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004183 if (IS_ERR_OR_NULL(di)) {
4184 if (!di)
4185 ret = -ENOENT;
4186 else
4187 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004188 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004189 goto out;
4190 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004191
4192 leaf = path->nodes[0];
4193 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194 index = key.offset;
4195 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004196 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004197
Lu Fengqi9add2942018-08-01 11:32:26 +08004198 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004199 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004200 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004201 goto out;
4202 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004203
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004204 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004205 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004206 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004207 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004208 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004209 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004210out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004211 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004212 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004213}
4214
Misono Tomohiroec42f162018-04-18 11:34:13 +09004215/*
4216 * Helper to check if the subvolume references other subvolumes or if it's
4217 * default.
4218 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004219static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004220{
4221 struct btrfs_fs_info *fs_info = root->fs_info;
4222 struct btrfs_path *path;
4223 struct btrfs_dir_item *di;
4224 struct btrfs_key key;
4225 u64 dir_id;
4226 int ret;
4227
4228 path = btrfs_alloc_path();
4229 if (!path)
4230 return -ENOMEM;
4231
4232 /* Make sure this root isn't set as the default subvol */
4233 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4234 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4235 dir_id, "default", 7, 0);
4236 if (di && !IS_ERR(di)) {
4237 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4238 if (key.objectid == root->root_key.objectid) {
4239 ret = -EPERM;
4240 btrfs_err(fs_info,
4241 "deleting default subvolume %llu is not allowed",
4242 key.objectid);
4243 goto out;
4244 }
4245 btrfs_release_path(path);
4246 }
4247
4248 key.objectid = root->root_key.objectid;
4249 key.type = BTRFS_ROOT_REF_KEY;
4250 key.offset = (u64)-1;
4251
4252 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4253 if (ret < 0)
4254 goto out;
4255 BUG_ON(ret == 0);
4256
4257 ret = 0;
4258 if (path->slots[0] > 0) {
4259 path->slots[0]--;
4260 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4261 if (key.objectid == root->root_key.objectid &&
4262 key.type == BTRFS_ROOT_REF_KEY)
4263 ret = -ENOTEMPTY;
4264 }
4265out:
4266 btrfs_free_path(path);
4267 return ret;
4268}
4269
Nikolay Borisov20a68002018-04-27 14:36:24 +03004270/* Delete all dentries for inodes belonging to the root */
4271static void btrfs_prune_dentries(struct btrfs_root *root)
4272{
4273 struct btrfs_fs_info *fs_info = root->fs_info;
4274 struct rb_node *node;
4275 struct rb_node *prev;
4276 struct btrfs_inode *entry;
4277 struct inode *inode;
4278 u64 objectid = 0;
4279
4280 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4281 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4282
4283 spin_lock(&root->inode_lock);
4284again:
4285 node = root->inode_tree.rb_node;
4286 prev = NULL;
4287 while (node) {
4288 prev = node;
4289 entry = rb_entry(node, struct btrfs_inode, rb_node);
4290
David Sterba37508512018-06-29 10:56:40 +02004291 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004292 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004293 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004294 node = node->rb_right;
4295 else
4296 break;
4297 }
4298 if (!node) {
4299 while (prev) {
4300 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004301 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004302 node = prev;
4303 break;
4304 }
4305 prev = rb_next(prev);
4306 }
4307 }
4308 while (node) {
4309 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004310 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004311 inode = igrab(&entry->vfs_inode);
4312 if (inode) {
4313 spin_unlock(&root->inode_lock);
4314 if (atomic_read(&inode->i_count) > 1)
4315 d_prune_aliases(inode);
4316 /*
4317 * btrfs_drop_inode will have it removed from the inode
4318 * cache when its usage count hits zero.
4319 */
4320 iput(inode);
4321 cond_resched();
4322 spin_lock(&root->inode_lock);
4323 goto again;
4324 }
4325
4326 if (cond_resched_lock(&root->inode_lock))
4327 goto again;
4328
4329 node = rb_next(node);
4330 }
4331 spin_unlock(&root->inode_lock);
4332}
4333
Misono Tomohirof60a2362018-04-18 11:34:52 +09004334int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4335{
4336 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4337 struct btrfs_root *root = BTRFS_I(dir)->root;
4338 struct inode *inode = d_inode(dentry);
4339 struct btrfs_root *dest = BTRFS_I(inode)->root;
4340 struct btrfs_trans_handle *trans;
4341 struct btrfs_block_rsv block_rsv;
4342 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004343 int ret;
4344 int err;
4345
4346 /*
4347 * Don't allow to delete a subvolume with send in progress. This is
4348 * inside the inode lock so the error handling that has to drop the bit
4349 * again is not run concurrently.
4350 */
4351 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004352 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004353 spin_unlock(&dest->root_item_lock);
4354 btrfs_warn(fs_info,
4355 "attempt to delete subvolume %llu during send",
4356 dest->root_key.objectid);
4357 return -EPERM;
4358 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004359 root_flags = btrfs_root_flags(&dest->root_item);
4360 btrfs_set_root_flags(&dest->root_item,
4361 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4362 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004363
4364 down_write(&fs_info->subvol_sem);
4365
4366 err = may_destroy_subvol(dest);
4367 if (err)
4368 goto out_up_write;
4369
4370 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4371 /*
4372 * One for dir inode,
4373 * two for dir entries,
4374 * two for root ref/backref.
4375 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004376 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004377 if (err)
4378 goto out_up_write;
4379
4380 trans = btrfs_start_transaction(root, 0);
4381 if (IS_ERR(trans)) {
4382 err = PTR_ERR(trans);
4383 goto out_release;
4384 }
4385 trans->block_rsv = &block_rsv;
4386 trans->bytes_reserved = block_rsv.size;
4387
4388 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4389
Lu Fengqi401b3b12018-08-01 11:32:30 +08004390 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4391 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004392 if (ret) {
4393 err = ret;
4394 btrfs_abort_transaction(trans, ret);
4395 goto out_end_trans;
4396 }
4397
4398 btrfs_record_root_in_trans(trans, dest);
4399
4400 memset(&dest->root_item.drop_progress, 0,
4401 sizeof(dest->root_item.drop_progress));
4402 dest->root_item.drop_level = 0;
4403 btrfs_set_root_refs(&dest->root_item, 0);
4404
4405 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4406 ret = btrfs_insert_orphan_item(trans,
4407 fs_info->tree_root,
4408 dest->root_key.objectid);
4409 if (ret) {
4410 btrfs_abort_transaction(trans, ret);
4411 err = ret;
4412 goto out_end_trans;
4413 }
4414 }
4415
Lu Fengqid1957792018-05-29 15:01:54 +08004416 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004417 BTRFS_UUID_KEY_SUBVOL,
4418 dest->root_key.objectid);
4419 if (ret && ret != -ENOENT) {
4420 btrfs_abort_transaction(trans, ret);
4421 err = ret;
4422 goto out_end_trans;
4423 }
4424 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004425 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004426 dest->root_item.received_uuid,
4427 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4428 dest->root_key.objectid);
4429 if (ret && ret != -ENOENT) {
4430 btrfs_abort_transaction(trans, ret);
4431 err = ret;
4432 goto out_end_trans;
4433 }
4434 }
4435
4436out_end_trans:
4437 trans->block_rsv = NULL;
4438 trans->bytes_reserved = 0;
4439 ret = btrfs_end_transaction(trans);
4440 if (ret && !err)
4441 err = ret;
4442 inode->i_flags |= S_DEAD;
4443out_release:
4444 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4445out_up_write:
4446 up_write(&fs_info->subvol_sem);
4447 if (err) {
4448 spin_lock(&dest->root_item_lock);
4449 root_flags = btrfs_root_flags(&dest->root_item);
4450 btrfs_set_root_flags(&dest->root_item,
4451 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4452 spin_unlock(&dest->root_item_lock);
4453 } else {
4454 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004455 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004456 ASSERT(dest->send_in_progress == 0);
4457
4458 /* the last ref */
4459 if (dest->ino_cache_inode) {
4460 iput(dest->ino_cache_inode);
4461 dest->ino_cache_inode = NULL;
4462 }
4463 }
4464
4465 return err;
4466}
4467
Chris Mason39279cc2007-06-12 06:35:45 -04004468static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4469{
David Howells2b0143b2015-03-17 22:25:59 +00004470 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004471 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004472 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004473 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004474 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004475
David Sterbab3ae2442012-09-13 16:04:34 -06004476 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004477 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004478 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004479 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004480
Josef Bacikd52be812013-05-29 14:54:47 -04004481 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004482 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004483 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004484
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004485 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004486 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004487 BTRFS_I(inode)->location.objectid,
4488 dentry->d_name.name,
4489 dentry->d_name.len);
4490 goto out;
4491 }
4492
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004493 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004494 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004495 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004496
Filipe Manana44f714d2016-06-06 16:11:13 +01004497 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4498
Chris Mason39279cc2007-06-12 06:35:45 -04004499 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004500 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4501 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4502 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004503 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004504 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004505 /*
4506 * Propagate the last_unlink_trans value of the deleted dir to
4507 * its parent directory. This is to prevent an unrecoverable
4508 * log tree in the case we do something like this:
4509 * 1) create dir foo
4510 * 2) create snapshot under dir foo
4511 * 3) delete the snapshot
4512 * 4) rmdir foo
4513 * 5) mkdir foo
4514 * 6) fsync foo or some file inside foo
4515 */
4516 if (last_unlink_trans >= trans->transid)
4517 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4518 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004519out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004520 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004521 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004522
Chris Mason39279cc2007-06-12 06:35:45 -04004523 return err;
4524}
4525
Josef Bacikddfae632017-10-19 14:16:02 -04004526/*
4527 * Return this if we need to call truncate_block for the last bit of the
4528 * truncate.
4529 */
4530#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004531
Chris Mason323ac952008-10-01 19:05:46 -04004532/*
Chris Mason39279cc2007-06-12 06:35:45 -04004533 * this can truncate away extent items, csum items and directory items.
4534 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004535 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004536 *
4537 * csum items that cross the new i_size are truncated to the new size
4538 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004539 *
4540 * min_type is the minimum key type to truncate down to. If set to 0, this
4541 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004542 */
Yan, Zheng80825102009-11-12 09:35:36 +00004543int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4544 struct btrfs_root *root,
4545 struct inode *inode,
4546 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004547{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004548 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004549 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004550 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004551 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004552 struct btrfs_key key;
4553 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004554 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004555 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004556 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004557 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004558 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004559 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004560 int found_extent;
4561 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004562 int pending_del_nr = 0;
4563 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004564 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004565 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004566 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004567 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004568 bool be_nice = false;
4569 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004570
4571 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004572
Chris Mason28ed1342014-12-17 09:41:04 -08004573 /*
4574 * for non-free space inodes and ref cows, we want to back off from
4575 * time to time
4576 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004577 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004578 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004579 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004580
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004581 path = btrfs_alloc_path();
4582 if (!path)
4583 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004584 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004585
Josef Bacik5dc562c2012-08-17 13:14:17 -04004586 /*
4587 * We want to drop from the next block forward in case this new size is
4588 * not block aligned since we will be keeping the last block of the
4589 * extent just the way it is.
4590 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004591 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004592 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004593 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004594 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004595 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004596
Miao Xie16cdcec2011-04-22 18:12:22 +08004597 /*
4598 * This function is also used to drop the items in the log tree before
4599 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004600 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004601 * items.
4602 */
4603 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004604 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004605
Li Zefan33345d012011-04-20 10:31:50 +08004606 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004607 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004608 key.type = (u8)-1;
4609
Chris Mason85e21ba2008-01-29 15:11:36 -05004610search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004611 /*
4612 * with a 16K leaf size and 128MB extents, you can actually queue
4613 * up a huge file in a single leaf. Most of the time that
4614 * bytes_deleted is > 0, it will be huge by the time we get here
4615 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004616 if (be_nice && bytes_deleted > SZ_32M &&
4617 btrfs_should_end_transaction(trans)) {
4618 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004619 goto out;
4620 }
Chris Masond3977122009-01-05 21:25:51 -05004621
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004622 path->leave_spinning = 1;
4623 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4624 if (ret < 0)
4625 goto out;
4626
Chris Mason85e21ba2008-01-29 15:11:36 -05004627 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004628 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004629 /* there are no items in the tree for us to truncate, we're
4630 * done
4631 */
Yan, Zheng80825102009-11-12 09:35:36 +00004632 if (path->slots[0] == 0)
4633 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004634 path->slots[0]--;
4635 }
4636
Chris Masond3977122009-01-05 21:25:51 -05004637 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004638 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004639 leaf = path->nodes[0];
4640 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004641 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004642
Li Zefan33345d012011-04-20 10:31:50 +08004643 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004644 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004645
Chris Mason85e21ba2008-01-29 15:11:36 -05004646 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004647 break;
4648
Chris Mason5f39d392007-10-15 16:14:19 -04004649 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004650 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004651 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004652 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004653 extent_type = btrfs_file_extent_type(leaf, fi);
4654 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004655 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004656 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004657
4658 trace_btrfs_truncate_show_fi_regular(
4659 BTRFS_I(inode), leaf, fi,
4660 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004661 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004662 item_end += btrfs_file_extent_ram_bytes(leaf,
4663 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004664
4665 trace_btrfs_truncate_show_fi_inline(
4666 BTRFS_I(inode), leaf, fi, path->slots[0],
4667 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004668 }
Yan008630c2007-11-07 13:31:09 -05004669 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004670 }
Yan, Zheng80825102009-11-12 09:35:36 +00004671 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004672 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004673 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004674 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004675 break;
4676 if (found_key.offset >= new_size)
4677 del_item = 1;
4678 else
4679 del_item = 0;
4680 }
Chris Mason39279cc2007-06-12 06:35:45 -04004681 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004682 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004683 if (found_type != BTRFS_EXTENT_DATA_KEY)
4684 goto delete;
4685
4686 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004687 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004688 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004689 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004690 u64 orig_num_bytes =
4691 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004692 extent_num_bytes = ALIGN(new_size -
4693 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004694 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004695 btrfs_set_file_extent_num_bytes(leaf, fi,
4696 extent_num_bytes);
4697 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004698 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004699 if (test_bit(BTRFS_ROOT_REF_COWS,
4700 &root->state) &&
4701 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004702 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004703 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004704 } else {
Chris Masondb945352007-10-15 16:15:53 -04004705 extent_num_bytes =
4706 btrfs_file_extent_disk_num_bytes(leaf,
4707 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004708 extent_offset = found_key.offset -
4709 btrfs_file_extent_offset(leaf, fi);
4710
Chris Mason39279cc2007-06-12 06:35:45 -04004711 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004712 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004713 if (extent_start != 0) {
4714 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004715 if (test_bit(BTRFS_ROOT_REF_COWS,
4716 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004717 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004718 }
4719 }
Chris Mason90692182008-02-08 13:49:28 -05004720 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004721 /*
4722 * we can't truncate inline items that have had
4723 * special encodings
4724 */
4725 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004726 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004727 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4728 btrfs_file_extent_compression(leaf, fi) == 0) {
4729 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004730
Josef Bacikddfae632017-10-19 14:16:02 -04004731 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4732 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004733 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004734 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004735 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004736 * We have to bail so the last_size is set to
4737 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004738 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004739 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004740 break;
Chris Mason90692182008-02-08 13:49:28 -05004741 }
Josef Bacikddfae632017-10-19 14:16:02 -04004742
4743 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4744 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004745 }
Chris Mason179e29e2007-11-01 11:28:41 -04004746delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004747 if (del_item)
4748 last_size = found_key.offset;
4749 else
4750 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004751 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004752 if (!pending_del_nr) {
4753 /* no pending yet, add ourselves */
4754 pending_del_slot = path->slots[0];
4755 pending_del_nr = 1;
4756 } else if (pending_del_nr &&
4757 path->slots[0] + 1 == pending_del_slot) {
4758 /* hop on the pending chunk */
4759 pending_del_nr++;
4760 pending_del_slot = path->slots[0];
4761 } else {
Chris Masond3977122009-01-05 21:25:51 -05004762 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004763 }
Chris Mason39279cc2007-06-12 06:35:45 -04004764 } else {
4765 break;
4766 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004767 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004768
Miao Xie27cdeb72014-04-02 19:51:05 +08004769 if (found_extent &&
4770 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004771 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004772 struct btrfs_ref ref = { 0 };
4773
Chris Masonb9473432009-03-13 11:00:37 -04004774 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004775 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004776
4777 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4778 extent_start, extent_num_bytes, 0);
4779 ref.real_root = root->root_key.objectid;
4780 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4781 ino, extent_offset);
4782 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004783 if (ret) {
4784 btrfs_abort_transaction(trans, ret);
4785 break;
4786 }
Chris Mason28f75a02015-02-04 06:59:29 -08004787 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004788 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004789 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004790 }
Chris Mason39279cc2007-06-12 06:35:45 -04004791 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004792
Yan, Zheng80825102009-11-12 09:35:36 +00004793 if (found_type == BTRFS_INODE_ITEM_KEY)
4794 break;
4795
4796 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004797 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004798 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004799 if (pending_del_nr) {
4800 ret = btrfs_del_items(trans, root, path,
4801 pending_del_slot,
4802 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004803 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004804 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004805 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004806 }
Yan, Zheng80825102009-11-12 09:35:36 +00004807 pending_del_nr = 0;
4808 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004809 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004810
Chris Mason28f75a02015-02-04 06:59:29 -08004811 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004812 * We can generate a lot of delayed refs, so we need to
4813 * throttle every once and a while and make sure we're
4814 * adding enough space to keep up with the work we are
4815 * generating. Since we hold a transaction here we
4816 * can't flush, and we don't want to FLUSH_LIMIT because
4817 * we could have generated too many delayed refs to
4818 * actually allocate, so just bail if we're short and
4819 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004820 */
Josef Bacik28bad212018-12-03 10:20:38 -05004821 if (should_throttle) {
4822 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4823 BTRFS_RESERVE_NO_FLUSH);
4824 if (ret) {
4825 ret = -EAGAIN;
4826 break;
4827 }
Chris Mason28f75a02015-02-04 06:59:29 -08004828 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004829 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004830 } else {
4831 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004832 }
Chris Mason39279cc2007-06-12 06:35:45 -04004833 }
Yan, Zheng80825102009-11-12 09:35:36 +00004834out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004835 if (ret >= 0 && pending_del_nr) {
4836 int err;
4837
4838 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004839 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004840 if (err) {
4841 btrfs_abort_transaction(trans, err);
4842 ret = err;
4843 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004844 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004845 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4846 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004847 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004848 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004849 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004850 }
Chris Mason28ed1342014-12-17 09:41:04 -08004851
Chris Mason39279cc2007-06-12 06:35:45 -04004852 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004853 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004854}
4855
Chris Masona52d9a82007-08-27 16:49:44 -04004856/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304857 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004858 * @inode - inode that we're zeroing
4859 * @from - the offset to start zeroing
4860 * @len - the length to zero, 0 to zero the entire range respective to the
4861 * offset
4862 * @front - zero up to the offset instead of from the offset on
4863 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304864 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004865 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004866 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304867int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004868 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004869{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004870 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004871 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004872 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4873 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004874 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004875 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004876 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004877 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004878 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304879 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004880 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004881 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004882 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304883 u64 block_start;
4884 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004885
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004886 if (IS_ALIGNED(offset, blocksize) &&
4887 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004888 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304889
Josef Bacik8b62f872017-10-19 14:15:55 -04004890 block_start = round_down(from, blocksize);
4891 block_end = block_start + blocksize - 1;
4892
Qu Wenruo364ecf32017-02-27 15:10:38 +08004893 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004894 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004895 if (ret)
4896 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004897
Chris Mason211c17f2008-05-15 09:13:45 -04004898again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004899 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004900 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004901 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004902 block_start, blocksize, true);
4903 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004904 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004905 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004906 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004907
Chris Masona52d9a82007-08-27 16:49:44 -04004908 if (!PageUptodate(page)) {
4909 ret = btrfs_readpage(NULL, page);
4910 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004911 if (page->mapping != mapping) {
4912 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004913 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004914 goto again;
4915 }
Chris Masona52d9a82007-08-27 16:49:44 -04004916 if (!PageUptodate(page)) {
4917 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004918 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004919 }
4920 }
Chris Mason211c17f2008-05-15 09:13:45 -04004921 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004922
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304923 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004924 set_page_extent_mapped(page);
4925
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304926 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004927 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304928 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004929 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004930 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004931 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004932 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004933 btrfs_put_ordered_extent(ordered);
4934 goto again;
4935 }
4936
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304937 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004938 EXTENT_DIRTY | EXTENT_DELALLOC |
4939 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004940 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004941
Filipe Mananae3b8a482017-11-04 00:16:59 +00004942 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004943 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004944 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304945 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004946 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004947 goto out_unlock;
4948 }
4949
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304950 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004951 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304952 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004953 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004954 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304955 memset(kaddr + (block_start - page_offset(page)),
4956 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004957 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304958 memset(kaddr + (block_start - page_offset(page)) + offset,
4959 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004960 flush_dcache_page(page);
4961 kunmap(page);
4962 }
Chris Mason247e7432008-07-17 12:53:51 -04004963 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004964 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004965 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004966
Chris Mason89642222008-07-24 09:41:53 -04004967out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004968 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004969 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004970 blocksize, true);
4971 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004972 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004973 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004974out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004975 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004976 return ret;
4977}
4978
Josef Bacik16e75492013-10-22 12:18:51 -04004979static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4980 u64 offset, u64 len)
4981{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004982 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004983 struct btrfs_trans_handle *trans;
4984 int ret;
4985
4986 /*
4987 * Still need to make sure the inode looks like it's been updated so
4988 * that any holes get logged if we fsync.
4989 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004990 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4991 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004992 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4993 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4994 return 0;
4995 }
4996
4997 /*
4998 * 1 - for the one we're dropping
4999 * 1 - for the one we're adding
5000 * 1 - for updating the inode.
5001 */
5002 trans = btrfs_start_transaction(root, 3);
5003 if (IS_ERR(trans))
5004 return PTR_ERR(trans);
5005
5006 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5007 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005008 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005009 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005010 return ret;
5011 }
5012
David Sterbaf85b7372017-01-20 14:54:07 +01005013 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5014 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04005015 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04005016 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04005017 else
5018 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005019 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04005020 return ret;
5021}
5022
Josef Bacik695a0d02011-03-04 15:46:53 -05005023/*
5024 * This function puts in dummy file extents for the area we're creating a hole
5025 * for. So if we are truncating this file to a larger size we need to insert
5026 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5027 * the range between oldsize and size
5028 */
Josef Bacika41ad392011-01-31 15:30:16 -05005029int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04005030{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005031 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04005032 struct btrfs_root *root = BTRFS_I(inode)->root;
5033 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005034 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005035 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005036 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005037 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5038 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005039 u64 last_byte;
5040 u64 cur_offset;
5041 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005042 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04005043
Josef Bacika71754f2013-06-17 17:14:39 -04005044 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305045 * If our size started in the middle of a block we need to zero out the
5046 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04005047 * expose stale data.
5048 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05305049 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04005050 if (err)
5051 return err;
5052
Yan Zheng9036c102008-10-30 14:19:41 -04005053 if (size <= hole_start)
5054 return 0;
5055
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03005056 btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start,
5057 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005058 cur_offset = hole_start;
5059 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005060 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005061 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005062 if (IS_ERR(em)) {
5063 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005064 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005065 break;
5066 }
Yan Zheng9036c102008-10-30 14:19:41 -04005067 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005068 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005069 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005070 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005071 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005072
Josef Bacik16e75492013-10-22 12:18:51 -04005073 err = maybe_insert_hole(root, inode, cur_offset,
5074 hole_size);
5075 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005076 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005077 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005078 cur_offset + hole_size - 1, 0);
5079 hole_em = alloc_extent_map();
5080 if (!hole_em) {
5081 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5082 &BTRFS_I(inode)->runtime_flags);
5083 goto next;
5084 }
5085 hole_em->start = cur_offset;
5086 hole_em->len = hole_size;
5087 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005088
Josef Bacik5dc562c2012-08-17 13:14:17 -04005089 hole_em->block_start = EXTENT_MAP_HOLE;
5090 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005091 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005092 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005093 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005094 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005095 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005096
5097 while (1) {
5098 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005099 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005100 write_unlock(&em_tree->lock);
5101 if (err != -EEXIST)
5102 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005103 btrfs_drop_extent_cache(BTRFS_I(inode),
5104 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005105 cur_offset +
5106 hole_size - 1, 0);
5107 }
5108 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005109 }
Josef Bacik16e75492013-10-22 12:18:51 -04005110next:
Yan Zheng9036c102008-10-30 14:19:41 -04005111 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005112 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005113 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005114 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005115 break;
5116 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005117 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005118 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005119 return err;
5120}
5121
Eric Sandeen3972f262013-01-12 02:57:22 +00005122static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005123{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005124 struct btrfs_root *root = BTRFS_I(inode)->root;
5125 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005126 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005127 loff_t newsize = attr->ia_size;
5128 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005129 int ret;
5130
Eric Sandeen3972f262013-01-12 02:57:22 +00005131 /*
5132 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5133 * special case where we need to update the times despite not having
5134 * these flags set. For all other operations the VFS set these flags
5135 * explicitly if it wants a timestamp update.
5136 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005137 if (newsize != oldsize) {
5138 inode_inc_iversion(inode);
5139 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5140 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005141 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005142 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005143
Josef Bacika41ad392011-01-31 15:30:16 -05005144 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005145 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005146 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005147 * This is to ensure the snapshot captures a fully consistent
5148 * state of this file - if the snapshot captures this expanding
5149 * truncation, it must capture all writes that happened before
5150 * this truncation.
5151 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005152 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005153 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005154 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005155 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005156 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005157 }
Yan, Zheng80825102009-11-12 09:35:36 +00005158
Miao Xief4a2f4c2011-12-14 20:12:01 -05005159 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005160 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005161 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005162 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005163 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005164
5165 i_size_write(inode, newsize);
5166 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305167 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005168 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005169 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005170 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005171 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005172
Josef Bacika41ad392011-01-31 15:30:16 -05005173 /*
5174 * We're truncating a file that used to have good data down to
5175 * zero. Make sure it gets into the ordered flush list so that
5176 * any new writes get down to disk quickly.
5177 */
5178 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005179 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5180 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005181
Josef Bacika41ad392011-01-31 15:30:16 -05005182 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005183
Andrea Gelmini52042d82018-11-28 12:05:13 +01005184 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005185 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005186 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005187 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005188
Filipe Manana213e8c52018-02-06 20:40:31 +00005189 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005190 if (ret && inode->i_nlink) {
5191 int err;
5192
5193 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005194 * Truncate failed, so fix up the in-memory size. We
5195 * adjusted disk_i_size down as we removed extents, so
5196 * wait for disk_i_size to be stable and then update the
5197 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005198 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005199 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005200 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005201 return err;
5202 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005203 }
Yan, Zheng80825102009-11-12 09:35:36 +00005204 }
5205
Josef Bacika41ad392011-01-31 15:30:16 -05005206 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005207}
5208
Chris Mason39279cc2007-06-12 06:35:45 -04005209static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5210{
David Howells2b0143b2015-03-17 22:25:59 +00005211 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005212 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005213 int err;
5214
Li Zefanb83cc962010-12-20 16:04:08 +08005215 if (btrfs_root_readonly(root))
5216 return -EROFS;
5217
Jan Kara31051c82016-05-26 16:55:18 +02005218 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005219 if (err)
5220 return err;
5221
Chris Mason5a3f23d2009-03-31 13:27:11 -04005222 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005223 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005224 if (err)
5225 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005226 }
Yan Zheng9036c102008-10-30 14:19:41 -04005227
Christoph Hellwig10257742010-06-04 11:30:02 +02005228 if (attr->ia_valid) {
5229 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005230 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005231 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005232
Josef Bacik22c44fe2011-11-30 10:45:38 -05005233 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005234 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005235 }
5236
Chris Mason39279cc2007-06-12 06:35:45 -04005237 return err;
5238}
Chris Mason61295eb2008-01-14 16:24:38 -05005239
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005240/*
5241 * While truncating the inode pages during eviction, we get the VFS calling
5242 * btrfs_invalidatepage() against each page of the inode. This is slow because
5243 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5244 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5245 * extent_state structures over and over, wasting lots of time.
5246 *
5247 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5248 * those expensive operations on a per page basis and do only the ordered io
5249 * finishing, while we release here the extent_map and extent_state structures,
5250 * without the excessive merging and splitting.
5251 */
5252static void evict_inode_truncate_pages(struct inode *inode)
5253{
5254 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5255 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5256 struct rb_node *node;
5257
5258 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005259 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005260
5261 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005262 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005263 struct extent_map *em;
5264
Liu Bo07e1ce02018-08-23 03:51:52 +08005265 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005266 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005267 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5268 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005269 remove_extent_mapping(map_tree, em);
5270 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005271 if (need_resched()) {
5272 write_unlock(&map_tree->lock);
5273 cond_resched();
5274 write_lock(&map_tree->lock);
5275 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005276 }
5277 write_unlock(&map_tree->lock);
5278
Filipe Manana6ca07092015-05-26 00:55:42 +01005279 /*
5280 * Keep looping until we have no more ranges in the io tree.
5281 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005282 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5283 * still in progress (unlocked the pages in the bio but did not yet
5284 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005285 * ranges can still be locked and eviction started because before
5286 * submitting those bios, which are executed by a separate task (work
5287 * queue kthread), inode references (inode->i_count) were not taken
5288 * (which would be dropped in the end io callback of each bio).
5289 * Therefore here we effectively end up waiting for those bios and
5290 * anyone else holding locked ranges without having bumped the inode's
5291 * reference count - if we don't do it, when they access the inode's
5292 * io_tree to unlock a range it may be too late, leading to an
5293 * use-after-free issue.
5294 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005295 spin_lock(&io_tree->lock);
5296 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5297 struct extent_state *state;
5298 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005299 u64 start;
5300 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005301 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005302
5303 node = rb_first(&io_tree->state);
5304 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005305 start = state->start;
5306 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005307 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005308 spin_unlock(&io_tree->lock);
5309
David Sterbaff13db42015-12-03 14:30:40 +01005310 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005311
5312 /*
5313 * If still has DELALLOC flag, the extent didn't reach disk,
5314 * and its reserved space won't be freed by delayed_ref.
5315 * So we need to free its reserved space here.
5316 * (Refer to comment in btrfs_invalidatepage, case 2)
5317 *
5318 * Note, end is the bytenr of last byte, so we need + 1 here.
5319 */
Filipe Manana421f0922018-10-12 13:02:48 +01005320 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005321 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005322
Filipe Manana6ca07092015-05-26 00:55:42 +01005323 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005324 EXTENT_LOCKED | EXTENT_DIRTY |
5325 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005326 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005327
Filipe Manana7064dd52014-08-08 02:47:05 +01005328 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005329 spin_lock(&io_tree->lock);
5330 }
5331 spin_unlock(&io_tree->lock);
5332}
5333
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005334static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005335 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005336{
5337 struct btrfs_fs_info *fs_info = root->fs_info;
5338 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005339 struct btrfs_trans_handle *trans;
Josef Bacik260e7702018-11-21 14:03:13 -05005340 u64 delayed_refs_extra = btrfs_calc_trans_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005341 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005342
Josef Bacikd3984c92019-08-01 18:19:37 -04005343 /*
5344 * Eviction should be taking place at some place safe because of our
5345 * delayed iputs. However the normal flushing code will run delayed
5346 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5347 *
5348 * We reserve the delayed_refs_extra here again because we can't use
5349 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5350 * above. We reserve our extra bit here because we generate a ton of
5351 * delayed refs activity by truncating.
5352 *
5353 * If we cannot make our reservation we'll attempt to steal from the
5354 * global reserve, because we really want to be able to free up space.
5355 */
5356 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5357 BTRFS_RESERVE_FLUSH_EVICT);
5358 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005359 /*
5360 * Try to steal from the global reserve if there is space for
5361 * it.
5362 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005363 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5364 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5365 btrfs_warn(fs_info,
5366 "could not allocate space for delete; will truncate on mount");
5367 return ERR_PTR(-ENOSPC);
5368 }
5369 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005370 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005371
5372 trans = btrfs_join_transaction(root);
5373 if (IS_ERR(trans))
5374 return trans;
5375
5376 if (delayed_refs_extra) {
5377 trans->block_rsv = &fs_info->trans_block_rsv;
5378 trans->bytes_reserved = delayed_refs_extra;
5379 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5380 delayed_refs_extra, 1);
5381 }
5382 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005383}
5384
Al Virobd555972010-06-07 11:35:40 -04005385void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005386{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005387 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005388 struct btrfs_trans_handle *trans;
5389 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005390 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005391 int ret;
5392
liubo1abe9b82011-03-24 11:18:59 +00005393 trace_btrfs_inode_evict(inode);
5394
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005395 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005396 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005397 return;
5398 }
5399
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005400 evict_inode_truncate_pages(inode);
5401
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005402 if (inode->i_nlink &&
5403 ((btrfs_root_refs(&root->root_item) != 0 &&
5404 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005405 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005406 goto no_delete;
5407
Omar Sandoval27919062018-05-11 13:13:37 -07005408 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005409 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005410
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005411 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005412
Omar Sandoval7b40b692018-05-11 13:13:33 -07005413 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005414 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005415
Yan, Zheng76dda932009-09-21 16:00:26 -04005416 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005417 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5418 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005419 goto no_delete;
5420 }
5421
Nikolay Borisovaa790212017-01-10 20:35:40 +02005422 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005423 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005424 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005425
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005426 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005427 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005428 goto no_delete;
Josef Bacikad80cf52018-09-28 07:18:19 -04005429 rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005430 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005431
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005432 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005433
Yan, Zheng80825102009-11-12 09:35:36 +00005434 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005435 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005436 if (IS_ERR(trans))
5437 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005438
5439 trans->block_rsv = rsv;
5440
Yan, Zhengd68fc572010-05-16 10:49:58 -04005441 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005442 trans->block_rsv = &fs_info->trans_block_rsv;
5443 btrfs_end_transaction(trans);
5444 btrfs_btree_balance_dirty(fs_info);
5445 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5446 goto free_rsv;
5447 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005448 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005449 }
5450
Josef Bacik4ef31a42013-08-13 14:10:08 -04005451 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005452 * Errors here aren't a big deal, it just means we leave orphan items in
5453 * the tree. They will be cleaned up on the next mount. If the inode
5454 * number gets reused, cleanup deletes the orphan item without doing
5455 * anything, and unlink reuses the existing orphan item.
5456 *
5457 * If it turns out that we are dropping too many of these, we might want
5458 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005459 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005460 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005461 if (!IS_ERR(trans)) {
5462 trans->block_rsv = rsv;
5463 btrfs_orphan_del(trans, BTRFS_I(inode));
5464 trans->block_rsv = &fs_info->trans_block_rsv;
5465 btrfs_end_transaction(trans);
5466 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005467
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005468 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005469 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005470 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005471
Omar Sandoval27919062018-05-11 13:13:37 -07005472free_rsv:
5473 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005474no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005475 /*
5476 * If we didn't successfully delete, the orphan item will still be in
5477 * the tree and we'll retry on the next mount. Again, we might also want
5478 * to retry these periodically in the future.
5479 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005480 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005481 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005482}
5483
5484/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005485 * Return the key found in the dir entry in the location pointer, fill @type
5486 * with BTRFS_FT_*, and return 0.
5487 *
Su Yue005d6712018-03-05 17:13:37 +08005488 * If no dir entries were found, returns -ENOENT.
5489 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005490 */
5491static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005492 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005493{
5494 const char *name = dentry->d_name.name;
5495 int namelen = dentry->d_name.len;
5496 struct btrfs_dir_item *di;
5497 struct btrfs_path *path;
5498 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005499 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005500
5501 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005502 if (!path)
5503 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005504
David Sterbaf85b7372017-01-20 14:54:07 +01005505 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5506 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005507 if (IS_ERR_OR_NULL(di)) {
5508 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005509 goto out;
5510 }
Chris Masond3977122009-01-05 21:25:51 -05005511
Chris Mason5f39d392007-10-15 16:14:19 -04005512 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005513 if (location->type != BTRFS_INODE_ITEM_KEY &&
5514 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005515 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005516 btrfs_warn(root->fs_info,
5517"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5518 __func__, name, btrfs_ino(BTRFS_I(dir)),
5519 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005520 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005521 if (!ret)
5522 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005523out:
Chris Mason39279cc2007-06-12 06:35:45 -04005524 btrfs_free_path(path);
5525 return ret;
5526}
5527
5528/*
5529 * when we hit a tree root in a directory, the btrfs part of the inode
5530 * needs to be changed to reflect the root directory of the tree root. This
5531 * is kind of like crossing a mount point.
5532 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005533static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005534 struct inode *dir,
5535 struct dentry *dentry,
5536 struct btrfs_key *location,
5537 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005538{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005539 struct btrfs_path *path;
5540 struct btrfs_root *new_root;
5541 struct btrfs_root_ref *ref;
5542 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005543 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005544 int ret;
5545 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005546
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005547 path = btrfs_alloc_path();
5548 if (!path) {
5549 err = -ENOMEM;
5550 goto out;
5551 }
Chris Mason39279cc2007-06-12 06:35:45 -04005552
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005553 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005554 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5555 key.type = BTRFS_ROOT_REF_KEY;
5556 key.offset = location->objectid;
5557
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005558 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005559 if (ret) {
5560 if (ret < 0)
5561 err = ret;
5562 goto out;
5563 }
Chris Mason39279cc2007-06-12 06:35:45 -04005564
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005565 leaf = path->nodes[0];
5566 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005567 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005568 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5569 goto out;
5570
5571 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5572 (unsigned long)(ref + 1),
5573 dentry->d_name.len);
5574 if (ret)
5575 goto out;
5576
David Sterbab3b4aa72011-04-21 01:20:15 +02005577 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005578
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005579 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005580 if (IS_ERR(new_root)) {
5581 err = PTR_ERR(new_root);
5582 goto out;
5583 }
5584
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005585 *sub_root = new_root;
5586 location->objectid = btrfs_root_dirid(&new_root->root_item);
5587 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005588 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005589 err = 0;
5590out:
5591 btrfs_free_path(path);
5592 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005593}
5594
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005595static void inode_tree_add(struct inode *inode)
5596{
5597 struct btrfs_root *root = BTRFS_I(inode)->root;
5598 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005599 struct rb_node **p;
5600 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005601 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005602 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603
Al Viro1d3382cb2010-10-23 15:19:20 -04005604 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005605 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005606 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005608 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005609 while (*p) {
5610 parent = *p;
5611 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5612
David Sterba37508512018-06-29 10:56:40 +02005613 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005614 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005615 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005616 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617 else {
5618 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005619 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005620 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005621 RB_CLEAR_NODE(parent);
5622 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005623 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005624 }
5625 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005626 rb_link_node(new, parent, p);
5627 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005628 spin_unlock(&root->inode_lock);
5629}
5630
5631static void inode_tree_del(struct inode *inode)
5632{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005633 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005634 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005635 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005636
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005637 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005638 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005639 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005640 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005641 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005642 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005643 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005644
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005645 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005646 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005647 spin_lock(&root->inode_lock);
5648 empty = RB_EMPTY_ROOT(&root->inode_tree);
5649 spin_unlock(&root->inode_lock);
5650 if (empty)
5651 btrfs_add_dead_root(root);
5652 }
5653}
5654
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005655
Chris Masone02119d2008-09-05 16:13:11 -04005656static int btrfs_init_locked_inode(struct inode *inode, void *p)
5657{
5658 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005659 inode->i_ino = args->location->objectid;
5660 memcpy(&BTRFS_I(inode)->location, args->location,
5661 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005662 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005663 return 0;
5664}
5665
5666static int btrfs_find_actor(struct inode *inode, void *opaque)
5667{
5668 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005669 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005670 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005671}
5672
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005673static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005674 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005675 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005676{
5677 struct inode *inode;
5678 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005679 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005680
Chris Mason90d3e592014-01-09 17:28:00 -08005681 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005682 args.root = root;
5683
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005684 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005685 btrfs_init_locked_inode,
5686 (void *)&args);
5687 return inode;
5688}
5689
Balaji Rao1a54ef82008-07-21 02:01:04 +05305690/* Get an inode object given its location and corresponding root.
5691 * Returns in *is_new if the inode was read from disk
5692 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005693struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5694 struct btrfs_root *root, int *new,
5695 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305696{
5697 struct inode *inode;
5698
Chris Mason90d3e592014-01-09 17:28:00 -08005699 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305700 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005701 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305702
5703 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005704 int ret;
5705
Filipe Manana4222ea72018-10-24 10:13:03 +01005706 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005707 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005708 inode_tree_add(inode);
5709 unlock_new_inode(inode);
5710 if (new)
5711 *new = 1;
5712 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005713 iget_failed(inode);
5714 /*
5715 * ret > 0 can come from btrfs_search_slot called by
5716 * btrfs_read_locked_inode, this means the inode item
5717 * was not found.
5718 */
5719 if (ret > 0)
5720 ret = -ENOENT;
5721 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005722 }
5723 }
5724
Balaji Rao1a54ef82008-07-21 02:01:04 +05305725 return inode;
5726}
5727
Filipe Manana4222ea72018-10-24 10:13:03 +01005728struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5729 struct btrfs_root *root, int *new)
5730{
5731 return btrfs_iget_path(s, location, root, new, NULL);
5732}
5733
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005734static struct inode *new_simple_dir(struct super_block *s,
5735 struct btrfs_key *key,
5736 struct btrfs_root *root)
5737{
5738 struct inode *inode = new_inode(s);
5739
5740 if (!inode)
5741 return ERR_PTR(-ENOMEM);
5742
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005743 BTRFS_I(inode)->root = root;
5744 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005745 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005746
5747 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005748 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005749 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005750 inode->i_fop = &simple_dir_operations;
5751 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005752 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305753 inode->i_atime = inode->i_mtime;
5754 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005755 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005756
5757 return inode;
5758}
5759
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005760static inline u8 btrfs_inode_type(struct inode *inode)
5761{
5762 /*
5763 * Compile-time asserts that generic FT_* types still match
5764 * BTRFS_FT_* types
5765 */
5766 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5767 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5768 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5769 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5770 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5771 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5772 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5773 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5774
5775 return fs_umode_to_ftype(inode->i_mode);
5776}
5777
Chris Mason3de45862008-11-17 21:02:50 -05005778struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005779{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005780 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005781 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005782 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005783 struct btrfs_root *sub_root = root;
5784 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005785 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005786 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005787 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005788
5789 if (dentry->d_name.len > BTRFS_NAME_LEN)
5790 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005791
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005792 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005793 if (ret < 0)
5794 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005795
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005796 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005797 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005798 if (IS_ERR(inode))
5799 return inode;
5800
5801 /* Do extra check against inode mode with di_type */
5802 if (btrfs_inode_type(inode) != di_type) {
5803 btrfs_crit(fs_info,
5804"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5805 inode->i_mode, btrfs_inode_type(inode),
5806 di_type);
5807 iput(inode);
5808 return ERR_PTR(-EUCLEAN);
5809 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005810 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005811 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005812
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005813 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005814 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005815 &location, &sub_root);
5816 if (ret < 0) {
5817 if (ret != -ENOENT)
5818 inode = ERR_PTR(ret);
5819 else
5820 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5821 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005822 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005823 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005824 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005825
Julia Lawall34d19ba2011-01-24 19:55:19 +00005826 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005827 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005828 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005829 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005830 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005831 if (ret) {
5832 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005833 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005834 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005835 }
5836
Chris Mason3de45862008-11-17 21:02:50 -05005837 return inode;
5838}
5839
Nick Pigginfe15ce42011-01-07 17:49:23 +11005840static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005841{
5842 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005843 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005844
Li Zefan848cce02012-02-21 17:04:28 +08005845 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005846 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005847
Li Zefan848cce02012-02-21 17:04:28 +08005848 if (inode) {
5849 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005850 if (btrfs_root_refs(&root->root_item) == 0)
5851 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005852
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005853 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005854 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005855 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005856 return 0;
5857}
5858
Chris Mason3de45862008-11-17 21:02:50 -05005859static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005860 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005861{
Al Viro3837d202018-10-10 16:38:27 -04005862 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005863
Al Viro3837d202018-10-10 16:38:27 -04005864 if (inode == ERR_PTR(-ENOENT))
5865 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005866 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005867}
5868
Josef Bacik23b5ec72017-07-24 15:14:25 -04005869/*
5870 * All this infrastructure exists because dir_emit can fault, and we are holding
5871 * the tree lock when doing readdir. For now just allocate a buffer and copy
5872 * our information into that, and then dir_emit from the buffer. This is
5873 * similar to what NFS does, only we don't keep the buffer around in pagecache
5874 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5875 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5876 * tree lock.
5877 */
5878static int btrfs_opendir(struct inode *inode, struct file *file)
5879{
5880 struct btrfs_file_private *private;
5881
5882 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5883 if (!private)
5884 return -ENOMEM;
5885 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5886 if (!private->filldir_buf) {
5887 kfree(private);
5888 return -ENOMEM;
5889 }
5890 file->private_data = private;
5891 return 0;
5892}
5893
5894struct dir_entry {
5895 u64 ino;
5896 u64 offset;
5897 unsigned type;
5898 int name_len;
5899};
5900
5901static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5902{
5903 while (entries--) {
5904 struct dir_entry *entry = addr;
5905 char *name = (char *)(entry + 1);
5906
David Sterba92d32172018-04-16 21:10:14 +02005907 ctx->pos = get_unaligned(&entry->offset);
5908 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5909 get_unaligned(&entry->ino),
5910 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005911 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005912 addr += sizeof(struct dir_entry) +
5913 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005914 ctx->pos++;
5915 }
5916 return 0;
5917}
5918
Al Viro9cdda8d2013-05-22 16:48:09 -04005919static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005920{
Al Viro9cdda8d2013-05-22 16:48:09 -04005921 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005922 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005923 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005924 struct btrfs_dir_item *di;
5925 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005926 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005927 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005928 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005929 struct list_head ins_list;
5930 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005931 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005932 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005933 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005934 char *name_ptr;
5935 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005936 int entries = 0;
5937 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005938 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005939 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005940
Al Viro9cdda8d2013-05-22 16:48:09 -04005941 if (!dir_emit_dots(file, ctx))
5942 return 0;
5943
David Woodhouse49593bf2008-08-17 17:08:36 +01005944 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005945 if (!path)
5946 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005947
Josef Bacik23b5ec72017-07-24 15:14:25 -04005948 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005949 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005950
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005951 INIT_LIST_HEAD(&ins_list);
5952 INIT_LIST_HEAD(&del_list);
5953 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005954
Josef Bacik23b5ec72017-07-24 15:14:25 -04005955again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005956 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005957 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005958 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005959
Chris Mason39279cc2007-06-12 06:35:45 -04005960 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5961 if (ret < 0)
5962 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005963
5964 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005965 struct dir_entry *entry;
5966
Chris Mason5f39d392007-10-15 16:14:19 -04005967 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005968 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005969 if (slot >= btrfs_header_nritems(leaf)) {
5970 ret = btrfs_next_leaf(root, path);
5971 if (ret < 0)
5972 goto err;
5973 else if (ret > 0)
5974 break;
5975 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005976 }
Chris Mason3de45862008-11-17 21:02:50 -05005977
Chris Mason5f39d392007-10-15 16:14:19 -04005978 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5979
5980 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005981 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005982 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005983 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005984 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005985 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005986 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005987 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005988 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005989 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005990 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5991 PAGE_SIZE) {
5992 btrfs_release_path(path);
5993 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5994 if (ret)
5995 goto nopos;
5996 addr = private->filldir_buf;
5997 entries = 0;
5998 total_len = 0;
5999 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006000 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006001
6002 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02006003 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006004 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006005 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6006 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00006007 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02006008 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006009 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02006010 put_unaligned(location.objectid, &entry->ino);
6011 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006012 entries++;
6013 addr += sizeof(struct dir_entry) + name_len;
6014 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006015next:
6016 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006017 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006018 btrfs_release_path(path);
6019
6020 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6021 if (ret)
6022 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006023
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006024 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006025 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006026 goto nopos;
6027
Zach Browndb62efb2013-07-11 16:19:42 -07006028 /*
6029 * Stop new entries from being returned after we return the last
6030 * entry.
6031 *
6032 * New directory entries are assigned a strictly increasing
6033 * offset. This means that new entries created during readdir
6034 * are *guaranteed* to be seen in the future by that readdir.
6035 * This has broken buggy programs which operate on names as
6036 * they're returned by readdir. Until we re-use freed offsets
6037 * we have this hack to stop new entries from being returned
6038 * under the assumption that they'll never reach this huge
6039 * offset.
6040 *
6041 * This is being careful not to overflow 32bit loff_t unless the
6042 * last entry requires it because doing so has broken 32bit apps
6043 * in the past.
6044 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006045 if (ctx->pos >= INT_MAX)
6046 ctx->pos = LLONG_MAX;
6047 else
6048 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006049nopos:
6050 ret = 0;
6051err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006052 if (put)
6053 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006054 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006055 return ret;
6056}
6057
Chris Mason39279cc2007-06-12 06:35:45 -04006058/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006059 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006060 * inode changes. But, it is most likely to find the inode in cache.
6061 * FIXME, needs more benchmarking...there are no reasons other than performance
6062 * to keep or drop this code.
6063 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006064static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006065{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006066 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006067 struct btrfs_root *root = BTRFS_I(inode)->root;
6068 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006069 int ret;
6070
Josef Bacik72ac3c02012-05-23 14:13:11 -04006071 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006072 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006073
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006074 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006075 if (IS_ERR(trans))
6076 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006077
6078 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006079 if (ret && ret == -ENOSPC) {
6080 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006081 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006082 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006083 if (IS_ERR(trans))
6084 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006085
Chris Mason94b60442010-05-26 11:02:00 -04006086 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006087 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006088 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006089 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006090 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006091
6092 return ret;
6093}
6094
6095/*
6096 * This is a copy of file_update_time. We need this so we can return error on
6097 * ENOSPC for updating the inode in the case of file write and mmap writes.
6098 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006099static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006100 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006101{
Alexander Block2bc5565282012-06-15 09:49:33 +02006102 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006103 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006104
6105 if (btrfs_root_readonly(root))
6106 return -EROFS;
6107
Josef Bacike41f9412012-03-26 09:46:47 -04006108 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006109 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006110 if (flags & S_CTIME)
6111 inode->i_ctime = *now;
6112 if (flags & S_MTIME)
6113 inode->i_mtime = *now;
6114 if (flags & S_ATIME)
6115 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006116 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006117}
6118
Chris Masond352ac62008-09-29 15:18:18 -04006119/*
6120 * find the highest existing sequence number in a directory
6121 * and then set the in-memory index_cnt variable to reflect
6122 * free sequence numbers
6123 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006124static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006125{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006126 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006127 struct btrfs_key key, found_key;
6128 struct btrfs_path *path;
6129 struct extent_buffer *leaf;
6130 int ret;
6131
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006132 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006133 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006134 key.offset = (u64)-1;
6135
6136 path = btrfs_alloc_path();
6137 if (!path)
6138 return -ENOMEM;
6139
6140 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6141 if (ret < 0)
6142 goto out;
6143 /* FIXME: we should be able to handle this */
6144 if (ret == 0)
6145 goto out;
6146 ret = 0;
6147
6148 /*
6149 * MAGIC NUMBER EXPLANATION:
6150 * since we search a directory based on f_pos we have to start at 2
6151 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6152 * else has to start at 2
6153 */
6154 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006155 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006156 goto out;
6157 }
6158
6159 path->slots[0]--;
6160
6161 leaf = path->nodes[0];
6162 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6163
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006164 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006165 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006166 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006167 goto out;
6168 }
6169
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006170 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006171out:
6172 btrfs_free_path(path);
6173 return ret;
6174}
6175
Chris Masond352ac62008-09-29 15:18:18 -04006176/*
6177 * helper to find a free sequence number in a given directory. This current
6178 * code is very simple, later versions will do smarter things in the btree
6179 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006180int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006181{
6182 int ret = 0;
6183
Nikolay Borisov877574e2017-02-20 13:50:33 +02006184 if (dir->index_cnt == (u64)-1) {
6185 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006186 if (ret) {
6187 ret = btrfs_set_inode_index_count(dir);
6188 if (ret)
6189 return ret;
6190 }
Josef Bacikaec74772008-07-24 12:12:38 -04006191 }
6192
Nikolay Borisov877574e2017-02-20 13:50:33 +02006193 *index = dir->index_cnt;
6194 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006195
6196 return ret;
6197}
6198
Chris Masonb0d5d102014-09-08 13:08:51 -07006199static int btrfs_insert_inode_locked(struct inode *inode)
6200{
6201 struct btrfs_iget_args args;
6202 args.location = &BTRFS_I(inode)->location;
6203 args.root = BTRFS_I(inode)->root;
6204
6205 return insert_inode_locked4(inode,
6206 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6207 btrfs_find_actor, &args);
6208}
6209
Anand Jain19aee8d2017-07-18 17:37:05 +08006210/*
6211 * Inherit flags from the parent inode.
6212 *
6213 * Currently only the compression flags and the cow flags are inherited.
6214 */
6215static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6216{
6217 unsigned int flags;
6218
6219 if (!dir)
6220 return;
6221
6222 flags = BTRFS_I(dir)->flags;
6223
6224 if (flags & BTRFS_INODE_NOCOMPRESS) {
6225 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6226 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6227 } else if (flags & BTRFS_INODE_COMPRESS) {
6228 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6229 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6230 }
6231
6232 if (flags & BTRFS_INODE_NODATACOW) {
6233 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6234 if (S_ISREG(inode->i_mode))
6235 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6236 }
6237
David Sterba7b6a2212018-03-26 18:40:21 +02006238 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006239}
6240
Chris Mason39279cc2007-06-12 06:35:45 -04006241static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6242 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006243 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006244 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006245 u64 ref_objectid, u64 objectid,
6246 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006248 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006249 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006250 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006251 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006252 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006253 struct btrfs_inode_ref *ref;
6254 struct btrfs_key key[2];
6255 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006256 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006257 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006258 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006259
Chris Mason5f39d392007-10-15 16:14:19 -04006260 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006261 if (!path)
6262 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006263
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006264 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006265 if (!inode) {
6266 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006267 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006268 }
Chris Mason39279cc2007-06-12 06:35:45 -04006269
Li Zefan581bb052011-04-20 10:06:11 +08006270 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006271 * O_TMPFILE, set link count to 0, so that after this point,
6272 * we fill in an inode item with the correct link count.
6273 */
6274 if (!name)
6275 set_nlink(inode, 0);
6276
6277 /*
Li Zefan581bb052011-04-20 10:06:11 +08006278 * we have to initialize this early, so we can reclaim the inode
6279 * number if we fail afterwards in this function.
6280 */
6281 inode->i_ino = objectid;
6282
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006283 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006284 trace_btrfs_inode_request(dir);
6285
Nikolay Borisov877574e2017-02-20 13:50:33 +02006286 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006287 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006288 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006289 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006290 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006291 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006292 } else if (dir) {
6293 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006294 }
6295 /*
6296 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006297 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006298 * number
6299 */
6300 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006301 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006302 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006303 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006304 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006305
Josef Bacik5dc562c2012-08-17 13:14:17 -04006306 /*
6307 * We could have gotten an inode number from somebody who was fsynced
6308 * and then removed in this same transaction, so let's just set full
6309 * sync since it will be a full sync anyway and this will blow away the
6310 * old info in the log.
6311 */
6312 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6313
Chris Mason9c583092008-01-29 15:15:18 -05006314 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006315 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006316 key[0].offset = 0;
6317
Chris Mason9c583092008-01-29 15:15:18 -05006318 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006319
6320 if (name) {
6321 /*
6322 * Start new inodes with an inode_ref. This is slightly more
6323 * efficient for small numbers of hard links since they will
6324 * be packed into one item. Extended refs will kick in if we
6325 * add more hard links than can fit in the ref item.
6326 */
6327 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006328 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006329 key[1].offset = ref_objectid;
6330
6331 sizes[1] = name_len + sizeof(*ref);
6332 }
Chris Mason9c583092008-01-29 15:15:18 -05006333
Chris Masonb0d5d102014-09-08 13:08:51 -07006334 location = &BTRFS_I(inode)->location;
6335 location->objectid = objectid;
6336 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006337 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006338
6339 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006340 if (ret < 0) {
6341 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006342 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006343 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006344
Chris Masonb9473432009-03-13 11:00:37 -04006345 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006346 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006347 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006348 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006349
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006350 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006351 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306352
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006353 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306354 inode->i_atime = inode->i_mtime;
6355 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006356 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306357
Chris Mason5f39d392007-10-15 16:14:19 -04006358 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6359 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006360 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006361 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006362 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006363
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006364 if (name) {
6365 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6366 struct btrfs_inode_ref);
6367 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6368 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6369 ptr = (unsigned long)(ref + 1);
6370 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6371 }
Chris Mason9c583092008-01-29 15:15:18 -05006372
Chris Mason5f39d392007-10-15 16:14:19 -04006373 btrfs_mark_buffer_dirty(path->nodes[0]);
6374 btrfs_free_path(path);
6375
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006376 btrfs_inherit_iflags(inode, dir);
6377
Al Viro569254b2011-07-24 17:08:40 -04006378 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006379 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006380 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006381 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006382 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6383 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006384 }
6385
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006386 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006387
6388 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006389 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006390
Alexander Block8ea05e32012-07-25 17:35:53 +02006391 btrfs_update_root_times(trans, root);
6392
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006393 ret = btrfs_inode_inherit_props(trans, inode, dir);
6394 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006395 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006396 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006397 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006398
Chris Mason39279cc2007-06-12 06:35:45 -04006399 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006400
6401fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006402 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006403fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006404 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006405 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006406 btrfs_free_path(path);
6407 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006408}
6409
Chris Masond352ac62008-09-29 15:18:18 -04006410/*
6411 * utility function to add 'inode' into 'parent_inode' with
6412 * a give name and a given sequence number.
6413 * if 'add_backref' is true, also insert a backref from the
6414 * inode to the parent directory.
6415 */
Chris Masone02119d2008-09-05 16:13:11 -04006416int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006417 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006418 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006419{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006420 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006421 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006422 struct btrfs_root *root = parent_inode->root;
6423 u64 ino = btrfs_ino(inode);
6424 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006425
Li Zefan33345d012011-04-20 10:31:50 +08006426 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006427 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006428 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006429 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006430 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006431 key.offset = 0;
6432 }
Chris Mason39279cc2007-06-12 06:35:45 -04006433
Li Zefan33345d012011-04-20 10:31:50 +08006434 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006435 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006436 root->root_key.objectid, parent_ino,
6437 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006438 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006439 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6440 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006441 }
6442
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006443 /* Nothing to clean up yet */
6444 if (ret)
6445 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006446
Lu Fengqi684572d2018-08-04 21:10:57 +08006447 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006448 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006449 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006450 goto fail_dir_item;
6451 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006452 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006453 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006454 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006455
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006456 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006457 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006458 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006459 /*
6460 * If we are replaying a log tree, we do not want to update the mtime
6461 * and ctime of the parent directory with the current time, since the
6462 * log replay procedure is responsible for setting them to their correct
6463 * values (the ones it had when the fsync was done).
6464 */
6465 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6466 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6467
6468 parent_inode->vfs_inode.i_mtime = now;
6469 parent_inode->vfs_inode.i_ctime = now;
6470 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006471 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006472 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006473 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006474 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006475
6476fail_dir_item:
6477 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6478 u64 local_index;
6479 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006480 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006481 root->root_key.objectid, parent_ino,
6482 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006483 if (err)
6484 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006485 } else if (add_backref) {
6486 u64 local_index;
6487 int err;
6488
6489 err = btrfs_del_inode_ref(trans, root, name, name_len,
6490 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006491 if (err)
6492 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006493 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006494
6495 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006496 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006497}
6498
6499static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006500 struct btrfs_inode *dir, struct dentry *dentry,
6501 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006502{
Josef Bacika1b075d2010-11-19 20:36:11 +00006503 int err = btrfs_add_link(trans, dir, inode,
6504 dentry->d_name.name, dentry->d_name.len,
6505 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006506 if (err > 0)
6507 err = -EEXIST;
6508 return err;
6509}
6510
Josef Bacik618e21d2007-07-11 10:18:17 -04006511static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006512 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006513{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006514 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006515 struct btrfs_trans_handle *trans;
6516 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006517 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006518 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006519 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006520 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006521
Josef Bacik9ed74f22009-09-11 16:12:44 -04006522 /*
6523 * 2 for inode item and ref
6524 * 2 for dir items
6525 * 1 for xattr if selinux is on
6526 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006527 trans = btrfs_start_transaction(root, 5);
6528 if (IS_ERR(trans))
6529 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006530
Li Zefan581bb052011-04-20 10:06:11 +08006531 err = btrfs_find_free_ino(root, &objectid);
6532 if (err)
6533 goto out_unlock;
6534
Josef Bacikaec74772008-07-24 12:12:38 -04006535 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006536 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6537 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006538 if (IS_ERR(inode)) {
6539 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006540 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006541 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006542 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006543
Casey Schauflerad19db72011-12-15 10:09:07 -05006544 /*
6545 * If the active LSM wants to access the inode during
6546 * d_instantiate it needs these. Smack checks to see
6547 * if the filesystem supports xattrs by looking at the
6548 * ops vector.
6549 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006550 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006551 init_special_inode(inode, inode->i_mode, rdev);
6552
6553 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006554 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006555 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006556
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006557 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6558 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006559 if (err)
6560 goto out_unlock;
6561
6562 btrfs_update_inode(trans, root, inode);
6563 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006564
Josef Bacik618e21d2007-07-11 10:18:17 -04006565out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006566 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006567 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006568 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006569 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006570 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006571 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006572 return err;
6573}
6574
Chris Mason39279cc2007-06-12 06:35:45 -04006575static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006576 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006577{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006578 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006579 struct btrfs_trans_handle *trans;
6580 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006581 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006582 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006583 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006584 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006585
Josef Bacik9ed74f22009-09-11 16:12:44 -04006586 /*
6587 * 2 for inode item and ref
6588 * 2 for dir items
6589 * 1 for xattr if selinux is on
6590 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006591 trans = btrfs_start_transaction(root, 5);
6592 if (IS_ERR(trans))
6593 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006594
Li Zefan581bb052011-04-20 10:06:11 +08006595 err = btrfs_find_free_ino(root, &objectid);
6596 if (err)
6597 goto out_unlock;
6598
Josef Bacikaec74772008-07-24 12:12:38 -04006599 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006600 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6601 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006602 if (IS_ERR(inode)) {
6603 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006604 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006605 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006606 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006607 /*
6608 * If the active LSM wants to access the inode during
6609 * d_instantiate it needs these. Smack checks to see
6610 * if the filesystem supports xattrs by looking at the
6611 * ops vector.
6612 */
6613 inode->i_fop = &btrfs_file_operations;
6614 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006615 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006616
6617 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6618 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006619 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006620
6621 err = btrfs_update_inode(trans, root, inode);
6622 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006623 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006624
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006625 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6626 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006627 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006628 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006629
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006630 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006631 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006632
Chris Mason39279cc2007-06-12 06:35:45 -04006633out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006634 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006635 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006636 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006637 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006638 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006639 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006640 return err;
6641}
6642
6643static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6644 struct dentry *dentry)
6645{
Filipe Manana271dba452016-01-05 16:24:05 +00006646 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006647 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006648 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006649 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006650 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006651 int err;
6652 int drop_inode = 0;
6653
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006654 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006655 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006656 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006657
Mark Fashehf1863732012-08-08 11:32:27 -07006658 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006659 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006660
Nikolay Borisov877574e2017-02-20 13:50:33 +02006661 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006662 if (err)
6663 goto fail;
6664
Yan, Zhenga22285a2010-05-16 10:48:46 -04006665 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006666 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006667 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006668 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006669 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006670 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006671 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006672 if (IS_ERR(trans)) {
6673 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006674 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006675 goto fail;
6676 }
Chris Mason5f39d392007-10-15 16:14:19 -04006677
Miao Xie67de1172013-12-26 13:07:06 +08006678 /* There are several dir indexes for this inode, clear the cache. */
6679 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006680 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006681 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006682 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006683 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006684 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006685
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006686 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6687 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006688
Yan, Zhenga5719522009-09-24 09:17:31 -04006689 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006690 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006691 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006692 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006693 int ret;
6694
Yan, Zhenga5719522009-09-24 09:17:31 -04006695 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006696 if (err)
6697 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006698 if (inode->i_nlink == 1) {
6699 /*
6700 * If new hard link count is 1, it's a file created
6701 * with open(2) O_TMPFILE flag.
6702 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006703 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006704 if (err)
6705 goto fail;
6706 }
Al Viro08c422c2011-12-23 07:58:13 -05006707 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006708 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6709 true, NULL);
6710 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6711 err = btrfs_commit_transaction(trans);
6712 trans = NULL;
6713 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006714 }
Chris Mason39279cc2007-06-12 06:35:45 -04006715
Chris Mason1832a6d2007-12-21 16:27:21 -05006716fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006717 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006718 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006719 if (drop_inode) {
6720 inode_dec_link_count(inode);
6721 iput(inode);
6722 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006723 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006724 return err;
6725}
6726
Al Viro18bb1db2011-07-26 01:41:39 -04006727static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006728{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006729 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006730 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006731 struct btrfs_trans_handle *trans;
6732 struct btrfs_root *root = BTRFS_I(dir)->root;
6733 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006734 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006735 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006736
Josef Bacik9ed74f22009-09-11 16:12:44 -04006737 /*
6738 * 2 items for inode and ref
6739 * 2 items for dir items
6740 * 1 for xattr if selinux is on
6741 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006742 trans = btrfs_start_transaction(root, 5);
6743 if (IS_ERR(trans))
6744 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006745
Li Zefan581bb052011-04-20 10:06:11 +08006746 err = btrfs_find_free_ino(root, &objectid);
6747 if (err)
6748 goto out_fail;
6749
Josef Bacikaec74772008-07-24 12:12:38 -04006750 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006751 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6752 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006753 if (IS_ERR(inode)) {
6754 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006755 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006756 goto out_fail;
6757 }
Chris Mason5f39d392007-10-15 16:14:19 -04006758
Chris Masonb0d5d102014-09-08 13:08:51 -07006759 /* these must be set before we unlock the inode */
6760 inode->i_op = &btrfs_dir_inode_operations;
6761 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006762
Eric Paris2a7dba32011-02-01 11:05:39 -05006763 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006764 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006765 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006766
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006767 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006768 err = btrfs_update_inode(trans, root, inode);
6769 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006770 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006771
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006772 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6773 dentry->d_name.name,
6774 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006775 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006776 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006777
Al Viro1e2e5472018-05-04 08:23:01 -04006778 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006779
6780out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006781 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006782 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006783 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006784 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006785 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006786 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006787 return err;
6788}
6789
Chris Masonc8b97812008-10-29 14:49:59 -04006790static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006791 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006792 size_t pg_offset, u64 extent_offset,
6793 struct btrfs_file_extent_item *item)
6794{
6795 int ret;
6796 struct extent_buffer *leaf = path->nodes[0];
6797 char *tmp;
6798 size_t max_size;
6799 unsigned long inline_size;
6800 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006801 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006802
6803 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006804 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006805 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6806 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006807 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006808 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006809 if (!tmp)
6810 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006811 ptr = btrfs_file_extent_inline_start(item);
6812
6813 read_extent_buffer(leaf, tmp, ptr, inline_size);
6814
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006815 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006816 ret = btrfs_decompress(compress_type, tmp, page,
6817 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006818
6819 /*
6820 * decompression code contains a memset to fill in any space between the end
6821 * of the uncompressed data and the end of max_size in case the decompressed
6822 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6823 * the end of an inline extent and the beginning of the next block, so we
6824 * cover that region here.
6825 */
6826
6827 if (max_size + pg_offset < PAGE_SIZE) {
6828 char *map = kmap(page);
6829 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6830 kunmap(page);
6831 }
Chris Masonc8b97812008-10-29 14:49:59 -04006832 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006833 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006834}
6835
Chris Masond352ac62008-09-29 15:18:18 -04006836/*
6837 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006838 * the ugly parts come from merging extents from the disk with the in-ram
6839 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006840 * where the in-ram extents might be locked pending data=ordered completion.
6841 *
6842 * This also copies inline extents directly into the page.
6843 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006844struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006845 struct page *page,
6846 size_t pg_offset, u64 start, u64 len,
6847 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006848{
David Sterba3ffbd682018-06-29 10:56:42 +02006849 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006850 int ret;
6851 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006852 u64 extent_start = 0;
6853 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006854 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006855 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006856 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006857 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006858 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006859 struct extent_buffer *leaf;
6860 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006861 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006862 struct extent_map_tree *em_tree = &inode->extent_tree;
6863 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006864 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006865
Chris Mason890871b2009-09-02 16:24:52 -04006866 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006867 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006868 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006869 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006870 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006871
Chris Masona52d9a82007-08-27 16:49:44 -04006872 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006873 if (em->start > start || em->start + em->len <= start)
6874 free_extent_map(em);
6875 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006876 free_extent_map(em);
6877 else
6878 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006879 }
David Sterba172ddd62011-04-21 00:48:27 +02006880 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006881 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006882 err = -ENOMEM;
6883 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006884 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006885 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006886 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006887 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006888 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006889 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006890
Liu Bobee6ec82018-08-17 05:05:28 +08006891 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006892 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006893 err = -ENOMEM;
6894 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006895 }
6896
Liu Bobee6ec82018-08-17 05:05:28 +08006897 /* Chances are we'll be called again, so go ahead and do readahead */
6898 path->reada = READA_FORWARD;
6899
Liu Boe49aabd2018-08-25 13:47:09 +08006900 /*
6901 * Unless we're going to uncompress the inline extent, no sleep would
6902 * happen.
6903 */
6904 path->leave_spinning = 1;
6905
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006906 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006907 if (ret < 0) {
6908 err = ret;
6909 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006910 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006911 if (path->slots[0] == 0)
6912 goto not_found;
6913 path->slots[0]--;
6914 }
6915
Chris Mason5f39d392007-10-15 16:14:19 -04006916 leaf = path->nodes[0];
6917 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006918 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006919 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006920 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006921 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006922 /*
6923 * If we backup past the first extent we want to move forward
6924 * and see if there is an extent in front of us, otherwise we'll
6925 * say there is a hole for our whole search range which can
6926 * cause problems.
6927 */
6928 extent_end = start;
6929 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006930 }
6931
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006932 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006933 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006934 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6935 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006936 /* Only regular file could have regular/prealloc extent */
6937 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6938 ret = -EUCLEAN;
6939 btrfs_crit(fs_info,
6940 "regular/prealloc extent found for non-regular inode %llu",
6941 btrfs_ino(inode));
6942 goto out;
6943 }
Chris Masona52d9a82007-08-27 16:49:44 -04006944 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006945 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006946
6947 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6948 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006949 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04006950 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006951
6952 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006953 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006954 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006955
6956 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6957 path->slots[0],
6958 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006959 }
Josef Bacik25a50342013-10-14 12:08:38 -04006960next:
Yan Zheng9036c102008-10-30 14:19:41 -04006961 if (start >= extent_end) {
6962 path->slots[0]++;
6963 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6964 ret = btrfs_next_leaf(root, path);
6965 if (ret < 0) {
6966 err = ret;
6967 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006968 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006969 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006970 }
Yan Zheng9036c102008-10-30 14:19:41 -04006971 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006972 }
Yan Zheng9036c102008-10-30 14:19:41 -04006973 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6974 if (found_key.objectid != objectid ||
6975 found_key.type != BTRFS_EXTENT_DATA_KEY)
6976 goto not_found;
6977 if (start + len <= found_key.offset)
6978 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006979 if (start > found_key.offset)
6980 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006981
6982 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006983 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006984 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006985 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006986 em->block_start = EXTENT_MAP_HOLE;
6987 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006988 }
6989
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006990 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006991 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006992
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006993 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6994 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006995 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006996 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006997 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006998 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006999 size_t size;
7000 size_t extent_offset;
7001 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007002
Filipe Manana7ffbb592014-06-09 03:48:05 +01007003 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007004 goto out;
Yan689f9342007-10-29 11:41:07 -04007005
Qu Wenruoe41ca582018-06-06 15:41:49 +08007006 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04007007 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007008 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7009 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007010 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007011 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007012 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007013 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007014 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08007015
7016 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007017 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007018 if (btrfs_file_extent_compression(leaf, item) !=
7019 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007020 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007021 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007022 if (ret) {
7023 err = ret;
7024 goto out;
7025 }
Chris Masonc8b97812008-10-29 14:49:59 -04007026 } else {
7027 map = kmap(page);
7028 read_extent_buffer(leaf, map + pg_offset, ptr,
7029 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007030 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007031 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007032 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007033 copy_size);
7034 }
Chris Masonc8b97812008-10-29 14:49:59 -04007035 kunmap(page);
7036 }
Chris Mason179e29e2007-11-01 11:28:41 -04007037 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007038 }
Chris Masond1310b22008-01-24 16:13:08 -05007039 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007040 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007041 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007042 }
7043not_found:
7044 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007045 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007046 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04007047 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007048insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007049 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007050 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007051 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007052 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7053 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007054 err = -EIO;
7055 goto out;
7056 }
Chris Masond1310b22008-01-24 16:13:08 -05007057
7058 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007059 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02007060 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007061 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007062out:
Liu Boc6414282018-08-23 07:36:17 +08007063 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00007064
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007065 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007066
Chris Masona52d9a82007-08-27 16:49:44 -04007067 if (err) {
7068 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007069 return ERR_PTR(err);
7070 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007071 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007072 return em;
7073}
7074
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007075struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007076 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007077{
7078 struct extent_map *em;
7079 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007080 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007081 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007082 u64 delalloc_len;
7083 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007084 int err = 0;
7085
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007086 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05007087 if (IS_ERR(em))
7088 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007089 /*
7090 * If our em maps to:
7091 * - a hole or
7092 * - a pre-alloc extent,
7093 * there might actually be delalloc bytes behind it.
7094 */
7095 if (em->block_start != EXTENT_MAP_HOLE &&
7096 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7097 return em;
7098 else
7099 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007100
7101 /* check to see if we've wrapped (len == -1 or similar) */
7102 end = start + len;
7103 if (end < start)
7104 end = (u64)-1;
7105 else
7106 end -= 1;
7107
7108 em = NULL;
7109
7110 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007111 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007112 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007113 delalloc_end = delalloc_start + delalloc_len;
7114 if (delalloc_end < delalloc_start)
7115 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007116
7117 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007118 * We didn't find anything useful, return the original results from
7119 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007120 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007121 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007122 em = hole_em;
7123 hole_em = NULL;
7124 goto out;
7125 }
7126
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007127 /*
7128 * Adjust the delalloc_start to make sure it doesn't go backwards from
7129 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007130 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007131 delalloc_start = max(start, delalloc_start);
7132 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007133
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007134 if (delalloc_len > 0) {
7135 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007136 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007137 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007138
David Sterba172ddd62011-04-21 00:48:27 +02007139 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007140 if (!em) {
7141 err = -ENOMEM;
7142 goto out;
7143 }
Chris Masonec29ed52011-02-23 16:23:20 -05007144 em->bdev = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007145
7146 ASSERT(hole_em);
7147 /*
7148 * When btrfs_get_extent can't find anything it returns one
7149 * huge hole
7150 *
7151 * Make sure what it found really fits our range, and adjust to
7152 * make sure it is based on the start from the caller
7153 */
7154 if (hole_end <= start || hole_em->start > end) {
7155 free_extent_map(hole_em);
7156 hole_em = NULL;
7157 } else {
7158 hole_start = max(hole_em->start, start);
7159 hole_len = hole_end - hole_start;
7160 }
7161
7162 if (hole_em && delalloc_start > hole_start) {
7163 /*
7164 * Our hole starts before our delalloc, so we have to
7165 * return just the parts of the hole that go until the
7166 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007167 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007168 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007169 em->start = hole_start;
7170 em->orig_start = hole_start;
7171 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007172 * Don't adjust block start at all, it is fixed at
7173 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007174 */
7175 em->block_start = hole_em->block_start;
7176 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007177 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7178 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007179 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007180 /*
7181 * Hole is out of passed range or it starts after
7182 * delalloc range
7183 */
7184 em->start = delalloc_start;
7185 em->len = delalloc_len;
7186 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007187 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007188 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007189 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007190 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007191 return hole_em;
7192 }
7193out:
7194
7195 free_extent_map(hole_em);
7196 if (err) {
7197 free_extent_map(em);
7198 return ERR_PTR(err);
7199 }
7200 return em;
7201}
7202
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007203static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7204 const u64 start,
7205 const u64 len,
7206 const u64 orig_start,
7207 const u64 block_start,
7208 const u64 block_len,
7209 const u64 orig_block_len,
7210 const u64 ram_bytes,
7211 const int type)
7212{
7213 struct extent_map *em = NULL;
7214 int ret;
7215
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007216 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007217 em = create_io_em(inode, start, len, orig_start,
7218 block_start, block_len, orig_block_len,
7219 ram_bytes,
7220 BTRFS_COMPRESS_NONE, /* compress_type */
7221 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007222 if (IS_ERR(em))
7223 goto out;
7224 }
7225 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7226 len, block_len, type);
7227 if (ret) {
7228 if (em) {
7229 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007230 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007231 start + len - 1, 0);
7232 }
7233 em = ERR_PTR(ret);
7234 }
7235 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007236
7237 return em;
7238}
7239
Josef Bacik4b46fce2010-05-23 11:00:55 -04007240static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7241 u64 start, u64 len)
7242{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007243 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007244 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007245 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007246 struct btrfs_key ins;
7247 u64 alloc_hint;
7248 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249
Josef Bacik4b46fce2010-05-23 11:00:55 -04007250 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007251 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007252 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007253 if (ret)
7254 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007255
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007256 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7257 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007258 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007259 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007260 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007261 btrfs_free_reserved_extent(fs_info, ins.objectid,
7262 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007263
Josef Bacik4b46fce2010-05-23 11:00:55 -04007264 return em;
7265}
7266
Chris Mason46bfbb52010-05-26 11:04:10 -04007267/*
7268 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7269 * block must be cow'd
7270 */
Josef Bacik00361582013-08-14 14:02:47 -04007271noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007272 u64 *orig_start, u64 *orig_block_len,
7273 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007274{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007275 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007276 struct btrfs_path *path;
7277 int ret;
7278 struct extent_buffer *leaf;
7279 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007280 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007281 struct btrfs_file_extent_item *fi;
7282 struct btrfs_key key;
7283 u64 disk_bytenr;
7284 u64 backref_offset;
7285 u64 extent_end;
7286 u64 num_bytes;
7287 int slot;
7288 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007289 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007290
Chris Mason46bfbb52010-05-26 11:04:10 -04007291 path = btrfs_alloc_path();
7292 if (!path)
7293 return -ENOMEM;
7294
David Sterbaf85b7372017-01-20 14:54:07 +01007295 ret = btrfs_lookup_file_extent(NULL, root, path,
7296 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007297 if (ret < 0)
7298 goto out;
7299
7300 slot = path->slots[0];
7301 if (ret == 1) {
7302 if (slot == 0) {
7303 /* can't find the item, must cow */
7304 ret = 0;
7305 goto out;
7306 }
7307 slot--;
7308 }
7309 ret = 0;
7310 leaf = path->nodes[0];
7311 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007312 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007313 key.type != BTRFS_EXTENT_DATA_KEY) {
7314 /* not our file or wrong item type, must cow */
7315 goto out;
7316 }
7317
7318 if (key.offset > offset) {
7319 /* Wrong offset, must cow */
7320 goto out;
7321 }
7322
7323 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7324 found_type = btrfs_file_extent_type(leaf, fi);
7325 if (found_type != BTRFS_FILE_EXTENT_REG &&
7326 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7327 /* not a regular extent, must cow */
7328 goto out;
7329 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007330
7331 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7332 goto out;
7333
Miao Xiee77751a2013-12-27 21:11:50 +08007334 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7335 if (extent_end <= offset)
7336 goto out;
7337
Chris Mason46bfbb52010-05-26 11:04:10 -04007338 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007339 if (disk_bytenr == 0)
7340 goto out;
7341
7342 if (btrfs_file_extent_compression(leaf, fi) ||
7343 btrfs_file_extent_encryption(leaf, fi) ||
7344 btrfs_file_extent_other_encoding(leaf, fi))
7345 goto out;
7346
Ethan Lien78d42952018-05-17 14:58:29 +08007347 /*
7348 * Do the same check as in btrfs_cross_ref_exist but without the
7349 * unnecessary search.
7350 */
7351 if (btrfs_file_extent_generation(leaf, fi) <=
7352 btrfs_root_last_snapshot(&root->root_item))
7353 goto out;
7354
Chris Mason46bfbb52010-05-26 11:04:10 -04007355 backref_offset = btrfs_file_extent_offset(leaf, fi);
7356
Josef Bacik7ee9e442013-06-21 16:37:03 -04007357 if (orig_start) {
7358 *orig_start = key.offset - backref_offset;
7359 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7360 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7361 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007362
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007363 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007364 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007365
7366 num_bytes = min(offset + *len, extent_end) - offset;
7367 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7368 u64 range_end;
7369
Jeff Mahoneyda170662016-06-15 09:22:56 -04007370 range_end = round_up(offset + num_bytes,
7371 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007372 ret = test_range_bit(io_tree, offset, range_end,
7373 EXTENT_DELALLOC, 0, NULL);
7374 if (ret) {
7375 ret = -EAGAIN;
7376 goto out;
7377 }
7378 }
7379
Josef Bacik1bda19e2013-10-18 12:10:36 -04007380 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007381
7382 /*
7383 * look for other files referencing this extent, if we
7384 * find any we must cow
7385 */
Josef Bacik00361582013-08-14 14:02:47 -04007386
Liu Boe4c3b2d2017-01-30 12:25:28 -08007387 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007388 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007389 if (ret) {
7390 ret = 0;
7391 goto out;
7392 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007393
7394 /*
7395 * adjust disk_bytenr and num_bytes to cover just the bytes
7396 * in this extent we are about to write. If there
7397 * are any csums in that range we have to cow in order
7398 * to keep the csums correct
7399 */
7400 disk_bytenr += backref_offset;
7401 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007402 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7403 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007404 /*
7405 * all of the above have passed, it is safe to overwrite this extent
7406 * without cow
7407 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007408 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007409 ret = 1;
7410out:
7411 btrfs_free_path(path);
7412 return ret;
7413}
7414
Josef Bacikeb838e72012-07-31 16:28:48 -04007415static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7416 struct extent_state **cached_state, int writing)
7417{
7418 struct btrfs_ordered_extent *ordered;
7419 int ret = 0;
7420
7421 while (1) {
7422 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007423 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007424 /*
7425 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007426 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007427 * extents in this range.
7428 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007429 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007430 lockend - lockstart + 1);
7431
7432 /*
7433 * We need to make sure there are no buffered pages in this
7434 * range either, we could have raced between the invalidate in
7435 * generic_file_direct_write and locking the extent. The
7436 * invalidate needs to happen so that reads after a write do not
7437 * get stale data.
7438 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007439 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007440 (!writing || !filemap_range_has_page(inode->i_mapping,
7441 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007442 break;
7443
7444 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007445 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007446
7447 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007448 /*
7449 * If we are doing a DIO read and the ordered extent we
7450 * found is for a buffered write, we can not wait for it
7451 * to complete and retry, because if we do so we can
7452 * deadlock with concurrent buffered writes on page
7453 * locks. This happens only if our DIO read covers more
7454 * than one extent map, if at this point has already
7455 * created an ordered extent for a previous extent map
7456 * and locked its range in the inode's io tree, and a
7457 * concurrent write against that previous extent map's
7458 * range and this range started (we unlock the ranges
7459 * in the io tree only when the bios complete and
7460 * buffered writes always lock pages before attempting
7461 * to lock range in the io tree).
7462 */
7463 if (writing ||
7464 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7465 btrfs_start_ordered_extent(inode, ordered, 1);
7466 else
7467 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007468 btrfs_put_ordered_extent(ordered);
7469 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007470 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007471 * We could trigger writeback for this range (and wait
7472 * for it to complete) and then invalidate the pages for
7473 * this range (through invalidate_inode_pages2_range()),
7474 * but that can lead us to a deadlock with a concurrent
7475 * call to readpages() (a buffered read or a defrag call
7476 * triggered a readahead) on a page lock due to an
7477 * ordered dio extent we created before but did not have
7478 * yet a corresponding bio submitted (whence it can not
7479 * complete), which makes readpages() wait for that
7480 * ordered extent to complete while holding a lock on
7481 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007482 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007483 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007484 }
7485
Filipe Mananaade77022016-02-18 14:28:55 +00007486 if (ret)
7487 break;
7488
Josef Bacikeb838e72012-07-31 16:28:48 -04007489 cond_resched();
7490 }
7491
7492 return ret;
7493}
7494
Liu Bo6f9994d2017-01-31 07:50:22 -08007495/* The callers of this must take lock_extent() */
7496static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7497 u64 orig_start, u64 block_start,
7498 u64 block_len, u64 orig_block_len,
7499 u64 ram_bytes, int compress_type,
7500 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007501{
7502 struct extent_map_tree *em_tree;
7503 struct extent_map *em;
7504 struct btrfs_root *root = BTRFS_I(inode)->root;
7505 int ret;
7506
Liu Bo6f9994d2017-01-31 07:50:22 -08007507 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7508 type == BTRFS_ORDERED_COMPRESSED ||
7509 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007510 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007511
Josef Bacik69ffb542012-09-11 15:40:07 -04007512 em_tree = &BTRFS_I(inode)->extent_tree;
7513 em = alloc_extent_map();
7514 if (!em)
7515 return ERR_PTR(-ENOMEM);
7516
7517 em->start = start;
7518 em->orig_start = orig_start;
7519 em->len = len;
7520 em->block_len = block_len;
7521 em->block_start = block_start;
7522 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007523 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007524 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007525 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007526 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007527 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007528 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007529 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007530 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7531 em->compress_type = compress_type;
7532 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007533
7534 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007535 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007536 em->start + em->len - 1, 0);
7537 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007538 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007539 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007540 /*
7541 * The caller has taken lock_extent(), who could race with us
7542 * to add em?
7543 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007544 } while (ret == -EEXIST);
7545
7546 if (ret) {
7547 free_extent_map(em);
7548 return ERR_PTR(ret);
7549 }
7550
Liu Bo6f9994d2017-01-31 07:50:22 -08007551 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007552 return em;
7553}
7554
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007555
7556static int btrfs_get_blocks_direct_read(struct extent_map *em,
7557 struct buffer_head *bh_result,
7558 struct inode *inode,
7559 u64 start, u64 len)
7560{
7561 if (em->block_start == EXTENT_MAP_HOLE ||
7562 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7563 return -ENOENT;
7564
7565 len = min(len, em->len - (start - em->start));
7566
7567 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7568 inode->i_blkbits;
7569 bh_result->b_size = len;
7570 bh_result->b_bdev = em->bdev;
7571 set_buffer_mapped(bh_result);
7572
7573 return 0;
7574}
7575
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007576static int btrfs_get_blocks_direct_write(struct extent_map **map,
7577 struct buffer_head *bh_result,
7578 struct inode *inode,
7579 struct btrfs_dio_data *dio_data,
7580 u64 start, u64 len)
7581{
7582 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7583 struct extent_map *em = *map;
7584 int ret = 0;
7585
7586 /*
7587 * We don't allocate a new extent in the following cases
7588 *
7589 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7590 * existing extent.
7591 * 2) The extent is marked as PREALLOC. We're good to go here and can
7592 * just use the extent.
7593 *
7594 */
7595 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7596 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7597 em->block_start != EXTENT_MAP_HOLE)) {
7598 int type;
7599 u64 block_start, orig_start, orig_block_len, ram_bytes;
7600
7601 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7602 type = BTRFS_ORDERED_PREALLOC;
7603 else
7604 type = BTRFS_ORDERED_NOCOW;
7605 len = min(len, em->len - (start - em->start));
7606 block_start = em->block_start + (start - em->start);
7607
7608 if (can_nocow_extent(inode, start, &len, &orig_start,
7609 &orig_block_len, &ram_bytes) == 1 &&
7610 btrfs_inc_nocow_writers(fs_info, block_start)) {
7611 struct extent_map *em2;
7612
7613 em2 = btrfs_create_dio_extent(inode, start, len,
7614 orig_start, block_start,
7615 len, orig_block_len,
7616 ram_bytes, type);
7617 btrfs_dec_nocow_writers(fs_info, block_start);
7618 if (type == BTRFS_ORDERED_PREALLOC) {
7619 free_extent_map(em);
7620 *map = em = em2;
7621 }
7622
7623 if (em2 && IS_ERR(em2)) {
7624 ret = PTR_ERR(em2);
7625 goto out;
7626 }
7627 /*
7628 * For inode marked NODATACOW or extent marked PREALLOC,
7629 * use the existing or preallocated extent, so does not
7630 * need to adjust btrfs_space_info's bytes_may_use.
7631 */
7632 btrfs_free_reserved_data_space_noquota(inode, start,
7633 len);
7634 goto skip_cow;
7635 }
7636 }
7637
7638 /* this will cow the extent */
7639 len = bh_result->b_size;
7640 free_extent_map(em);
7641 *map = em = btrfs_new_extent_direct(inode, start, len);
7642 if (IS_ERR(em)) {
7643 ret = PTR_ERR(em);
7644 goto out;
7645 }
7646
7647 len = min(len, em->len - (start - em->start));
7648
7649skip_cow:
7650 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7651 inode->i_blkbits;
7652 bh_result->b_size = len;
7653 bh_result->b_bdev = em->bdev;
7654 set_buffer_mapped(bh_result);
7655
7656 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7657 set_buffer_new(bh_result);
7658
7659 /*
7660 * Need to update the i_size under the extent lock so buffered
7661 * readers will get the updated i_size when we unlock.
7662 */
7663 if (!dio_data->overwrite && start + len > i_size_read(inode))
7664 i_size_write(inode, start + len);
7665
7666 WARN_ON(dio_data->reserve < len);
7667 dio_data->reserve -= len;
7668 dio_data->unsubmitted_oe_range_end = start + len;
7669 current->journal_info = dio_data;
7670out:
7671 return ret;
7672}
7673
Josef Bacik4b46fce2010-05-23 11:00:55 -04007674static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7675 struct buffer_head *bh_result, int create)
7676{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007677 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007678 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007679 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307680 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007682 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007683 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007684 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007685 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007686
Miao Xie172a5042013-02-21 02:48:22 -07007687 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007688 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007689 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007690 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007691
Josef Bacikc3298612012-08-03 16:49:19 -04007692 lockstart = start;
7693 lockend = start + len - 1;
7694
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007695 if (current->journal_info) {
7696 /*
7697 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007698 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007699 * confused.
7700 */
chandan50745b02015-08-28 21:10:13 +05307701 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007702 current->journal_info = NULL;
7703 }
7704
Josef Bacikeb838e72012-07-31 16:28:48 -04007705 /*
7706 * If this errors out it's because we couldn't invalidate pagecache for
7707 * this range and we need to fallback to buffered.
7708 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007709 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7710 create)) {
7711 ret = -ENOTBLK;
7712 goto err;
7713 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007714
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007715 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007716 if (IS_ERR(em)) {
7717 ret = PTR_ERR(em);
7718 goto unlock_err;
7719 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007720
7721 /*
7722 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7723 * io. INLINE is special, and we could probably kludge it in here, but
7724 * it's still buffered so for safety lets just fall back to the generic
7725 * buffered path.
7726 *
7727 * For COMPRESSED we _have_ to read the entire extent in so we can
7728 * decompress it, so there will be buffering required no matter what we
7729 * do, so go ahead and fallback to buffered.
7730 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007731 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007732 * to buffered IO. Don't blame me, this is the price we pay for using
7733 * the generic code.
7734 */
7735 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7736 em->block_start == EXTENT_MAP_INLINE) {
7737 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007738 ret = -ENOTBLK;
7739 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007740 }
7741
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007742 if (create) {
7743 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7744 dio_data, start, len);
7745 if (ret < 0)
7746 goto unlock_err;
7747
7748 /* clear and unlock the entire range */
7749 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7750 unlock_bits, 1, 0, &cached_state);
7751 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007752 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7753 start, len);
7754 /* Can be negative only if we read from a hole */
7755 if (ret < 0) {
7756 ret = 0;
7757 free_extent_map(em);
7758 goto unlock_err;
7759 }
7760 /*
7761 * We need to unlock only the end area that we aren't using.
7762 * The rest is going to be unlocked by the endio routine.
7763 */
7764 lockstart = start + bh_result->b_size;
7765 if (lockstart < lockend) {
7766 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7767 lockend, unlock_bits, 1, 0,
7768 &cached_state);
7769 } else {
7770 free_extent_state(cached_state);
7771 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007772 }
7773
Josef Bacik4b46fce2010-05-23 11:00:55 -04007774 free_extent_map(em);
7775
7776 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007777
7778unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007779 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007780 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007781err:
chandan50745b02015-08-28 21:10:13 +05307782 if (dio_data)
7783 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007784 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007785}
7786
Omar Sandoval58efbc92017-08-22 23:45:59 -07007787static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7788 struct bio *bio,
7789 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007790{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007791 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007792 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007793
Mike Christie37226b22016-06-05 14:31:52 -05007794 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007795
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007796 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007797 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007798 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007799
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007800 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007801
Miao Xie8b110e32014-09-12 18:44:03 +08007802 return ret;
7803}
7804
7805static int btrfs_check_dio_repairable(struct inode *inode,
7806 struct bio *failed_bio,
7807 struct io_failure_record *failrec,
7808 int failed_mirror)
7809{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007810 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007811 int num_copies;
7812
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007813 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007814 if (num_copies == 1) {
7815 /*
7816 * we only have a single copy of the data, so don't bother with
7817 * all the retry and error correction code that follows. no
7818 * matter what the error is, it is very likely to persist.
7819 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007820 btrfs_debug(fs_info,
7821 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7822 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007823 return 0;
7824 }
7825
7826 failrec->failed_mirror = failed_mirror;
7827 failrec->this_mirror++;
7828 if (failrec->this_mirror == failed_mirror)
7829 failrec->this_mirror++;
7830
7831 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007832 btrfs_debug(fs_info,
7833 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7834 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007835 return 0;
7836 }
7837
7838 return 1;
7839}
7840
Omar Sandoval58efbc92017-08-22 23:45:59 -07007841static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7842 struct page *page, unsigned int pgoff,
7843 u64 start, u64 end, int failed_mirror,
7844 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007845{
7846 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007847 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7848 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007849 struct bio *bio;
7850 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007851 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007852 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007853 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007854 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007855 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007856
Mike Christie37226b22016-06-05 14:31:52 -05007857 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007858
7859 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7860 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007861 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007862
7863 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7864 failed_mirror);
7865 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007866 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007867 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007868 }
7869
Liu Bo17347ce2017-05-15 15:33:27 -07007870 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007871 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007872 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007873 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007874 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007875
7876 isector = start - btrfs_io_bio(failed_bio)->logical;
7877 isector >>= inode->i_sb->s_blocksize_bits;
7878 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307879 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007880 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007881
7882 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007883 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007884 read_mode, failrec->this_mirror, failrec->in_validation);
7885
Omar Sandoval58efbc92017-08-22 23:45:59 -07007886 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7887 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007888 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007889 bio_put(bio);
7890 }
7891
Omar Sandoval58efbc92017-08-22 23:45:59 -07007892 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007893}
7894
7895struct btrfs_retry_complete {
7896 struct completion done;
7897 struct inode *inode;
7898 u64 start;
7899 int uptodate;
7900};
7901
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007902static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007903{
7904 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007905 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007906 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007907 struct extent_io_tree *io_tree, *failure_tree;
Ming Lei6dc4f102019-02-15 19:13:19 +08007908 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007909
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007910 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007911 goto end;
7912
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307913 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007914 io_tree = &BTRFS_I(inode)->io_tree;
7915 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007916 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307917
Miao Xie8b110e32014-09-12 18:44:03 +08007918 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007919 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007920 bio_for_each_segment_all(bvec, bio, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007921 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7922 io_tree, done->start, bvec->bv_page,
7923 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007924end:
7925 complete(&done->done);
7926 bio_put(bio);
7927}
7928
Omar Sandoval58efbc92017-08-22 23:45:59 -07007929static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7930 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007931{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307932 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007933 struct bio_vec bvec;
7934 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007935 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007936 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307937 unsigned int pgoff;
7938 u32 sectorsize;
7939 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007940 blk_status_t ret;
7941 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007942
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307943 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007944 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307945
Miao Xiec1dc0892014-09-12 18:43:56 +08007946 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007947 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007948 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007949
Liu Bo17347ce2017-05-15 15:33:27 -07007950 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7951 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7952 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307953
7954next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007955 done.uptodate = 0;
7956 done.start = start;
7957 init_completion(&done.done);
7958
Liu Bo17347ce2017-05-15 15:33:27 -07007959 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307960 pgoff, start, start + sectorsize - 1,
7961 io_bio->mirror_num,
7962 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007963 if (ret) {
7964 err = ret;
7965 goto next;
7966 }
Miao Xie8b110e32014-09-12 18:44:03 +08007967
David Sterba9c17f6c2017-07-19 19:26:45 +02007968 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007969
7970 if (!done.uptodate) {
7971 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307972 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007973 }
7974
Liu Bo629ebf42017-05-15 17:20:07 -07007975next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307976 start += sectorsize;
7977
Liu Bo97bf5a52017-04-07 13:11:10 -07007978 nr_sectors--;
7979 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307980 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007981 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307982 goto next_block_or_try_again;
7983 }
Miao Xie8b110e32014-09-12 18:44:03 +08007984 }
7985
Liu Bo629ebf42017-05-15 17:20:07 -07007986 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007987}
7988
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007989static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007990{
7991 struct btrfs_retry_complete *done = bio->bi_private;
7992 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007993 struct extent_io_tree *io_tree, *failure_tree;
7994 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007995 struct bio_vec *bvec;
7996 int uptodate;
7997 int ret;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007998 int i = 0;
Ming Lei6dc4f102019-02-15 19:13:19 +08007999 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08008000
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008001 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008002 goto end;
8003
8004 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308005
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308006 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08008007 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308008
Josef Bacik7870d082017-05-05 11:57:15 -04008009 io_tree = &BTRFS_I(inode)->io_tree;
8010 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8011
David Sterbac09abff2017-07-13 18:10:07 +02008012 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008013 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04008014 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8015 bvec->bv_offset, done->start,
8016 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008017 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008018 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8019 failure_tree, io_tree, done->start,
8020 bvec->bv_page,
8021 btrfs_ino(BTRFS_I(inode)),
8022 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008023 else
8024 uptodate = 0;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02008025 i++;
Miao Xie8b110e32014-09-12 18:44:03 +08008026 }
8027
8028 done->uptodate = uptodate;
8029end:
8030 complete(&done->done);
8031 bio_put(bio);
8032}
8033
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008034static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8035 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008036{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308037 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008038 struct bio_vec bvec;
8039 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008040 struct btrfs_retry_complete done;
8041 u64 start;
8042 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308043 u32 sectorsize;
8044 int nr_sectors;
8045 unsigned int pgoff;
8046 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008047 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008048 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008049 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008050
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308051 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008052 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308053
Omar Sandoval58efbc92017-08-22 23:45:59 -07008054 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008055 start = io_bio->logical;
8056 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008057 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008058
Liu Bo17347ce2017-05-15 15:33:27 -07008059 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8060 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308061
Liu Bo17347ce2017-05-15 15:33:27 -07008062 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308063next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008064 if (uptodate) {
8065 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8066 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8067 bvec.bv_page, pgoff, start, sectorsize);
8068 if (likely(!ret))
8069 goto next;
8070 }
Miao Xie8b110e32014-09-12 18:44:03 +08008071try_again:
8072 done.uptodate = 0;
8073 done.start = start;
8074 init_completion(&done.done);
8075
Omar Sandoval58efbc92017-08-22 23:45:59 -07008076 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8077 pgoff, start, start + sectorsize - 1,
8078 io_bio->mirror_num, btrfs_retry_endio,
8079 &done);
8080 if (status) {
8081 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008082 goto next;
8083 }
8084
David Sterba9c17f6c2017-07-19 19:26:45 +02008085 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008086
8087 if (!done.uptodate) {
8088 /* We might have another mirror, so try again */
8089 goto try_again;
8090 }
8091next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308092 offset += sectorsize;
8093 start += sectorsize;
8094
8095 ASSERT(nr_sectors);
8096
Liu Bo97bf5a52017-04-07 13:11:10 -07008097 nr_sectors--;
8098 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308099 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008100 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308101 goto next_block;
8102 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008103 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008104
8105 return err;
8106}
8107
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008108static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8109 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008110{
8111 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8112
8113 if (skip_csum) {
8114 if (unlikely(err))
8115 return __btrfs_correct_data_nocsum(inode, io_bio);
8116 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008117 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008118 } else {
8119 return __btrfs_subio_endio_read(inode, io_bio, err);
8120 }
8121}
8122
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008123static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008124{
8125 struct btrfs_dio_private *dip = bio->bi_private;
8126 struct inode *inode = dip->inode;
8127 struct bio *dio_bio;
8128 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008129 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008130
Liu Bo99c4e3b92017-09-15 15:06:51 -06008131 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008132 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008133
Josef Bacik4b46fce2010-05-23 11:00:55 -04008134 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008135 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008136 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008137
Josef Bacik4b46fce2010-05-23 11:00:55 -04008138 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008139
Liu Bo99c4e3b92017-09-15 15:06:51 -06008140 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008141 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008142 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008143 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008144}
8145
Qu Wenruo524272602017-03-08 10:25:52 +08008146static void __endio_write_update_ordered(struct inode *inode,
8147 const u64 offset, const u64 bytes,
8148 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008149{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008150 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008151 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008152 struct btrfs_workqueue *wq;
8153 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008154 u64 ordered_offset = offset;
8155 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008156 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008157
Qu Wenruo524272602017-03-08 10:25:52 +08008158 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8159 wq = fs_info->endio_freespace_worker;
8160 func = btrfs_freespace_write_helper;
8161 } else {
8162 wq = fs_info->endio_write_workers;
8163 func = btrfs_endio_write_helper;
8164 }
8165
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008166 while (ordered_offset < offset + bytes) {
8167 last_offset = ordered_offset;
8168 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8169 &ordered_offset,
8170 ordered_bytes,
8171 uptodate)) {
8172 btrfs_init_work(&ordered->work, func,
8173 finish_ordered_fn,
8174 NULL, NULL);
8175 btrfs_queue_work(wq, &ordered->work);
8176 }
8177 /*
8178 * If btrfs_dec_test_ordered_pending does not find any ordered
8179 * extent in the range, we can exit.
8180 */
8181 if (ordered_offset == last_offset)
8182 return;
8183 /*
8184 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008185 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008186 */
8187 if (ordered_offset < offset + bytes) {
8188 ordered_bytes = offset + bytes - ordered_offset;
8189 ordered = NULL;
8190 }
Chris Mason163cf092010-11-28 19:56:33 -05008191 }
Filipe Manana14543772015-11-24 16:23:54 +00008192}
8193
8194static void btrfs_endio_direct_write(struct bio *bio)
8195{
8196 struct btrfs_dio_private *dip = bio->bi_private;
8197 struct bio *dio_bio = dip->dio_bio;
8198
Qu Wenruo524272602017-03-08 10:25:52 +08008199 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008200 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008201
Josef Bacik4b46fce2010-05-23 11:00:55 -04008202 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008203
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008204 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008205 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008206 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008207}
8208
David Sterbad0ee3932018-03-08 14:35:48 +01008209static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008210 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008211{
Josef Bacikc6100a42017-05-05 11:57:13 -04008212 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008213 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008214 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008215 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008216 return 0;
8217}
8218
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008219static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008220{
8221 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008222 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008223
Miao Xie8b110e32014-09-12 18:44:03 +08008224 if (err)
8225 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008226 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008227 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8228 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008229 (unsigned long long)bio->bi_iter.bi_sector,
8230 bio->bi_iter.bi_size, err);
8231
8232 if (dip->subio_endio)
8233 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008234
8235 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008236 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008237 * We want to perceive the errors flag being set before
8238 * decrementing the reference count. We don't need a barrier
8239 * since atomic operations with a return value are fully
8240 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008241 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008242 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008243 }
8244
8245 /* if there are more bios still pending for this dio, just exit */
8246 if (!atomic_dec_and_test(&dip->pending_bios))
8247 goto out;
8248
Chris Mason9be33952013-05-17 18:30:14 -04008249 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008250 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008251 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008252 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008253 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008254 }
8255out:
8256 bio_put(bio);
8257}
8258
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008259static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008260 struct btrfs_dio_private *dip,
8261 struct bio *bio,
8262 u64 file_offset)
8263{
8264 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8265 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008266 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008267
8268 /*
8269 * We load all the csum data we need when we submit
8270 * the first bio to reduce the csum tree search and
8271 * contention.
8272 */
8273 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008274 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008275 file_offset);
8276 if (ret)
8277 return ret;
8278 }
8279
8280 if (bio == dip->orig_bio)
8281 return 0;
8282
8283 file_offset -= dip->logical_offset;
8284 file_offset >>= inode->i_sb->s_blocksize_bits;
8285 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8286
8287 return 0;
8288}
8289
David Sterbad0ee3932018-03-08 14:35:48 +01008290static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8291 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008292{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008293 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008294 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008295 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008296 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008297
Liu Bo4c274bc2017-11-01 17:19:27 -06008298 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008299 if (async_submit)
8300 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8301
Josef Bacik5fd02042012-05-02 14:00:54 -04008302 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008303 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008304 if (ret)
8305 goto err;
8306 }
Miao Xiee65e1532010-11-22 03:04:43 +00008307
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008308 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008309 goto map;
8310
8311 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008312 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8313 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008314 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008315 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008316 } else if (write) {
8317 /*
8318 * If we aren't doing async submit, calculate the csum of the
8319 * bio now.
8320 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008321 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008322 if (ret)
8323 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008324 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008325 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008326 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008327 if (ret)
8328 goto err;
8329 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008330map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008331 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008332err:
Miao Xiee65e1532010-11-22 03:04:43 +00008333 return ret;
8334}
8335
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008336static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008337{
8338 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008339 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008340 struct bio *bio;
8341 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008342 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008343 u64 file_offset = dip->logical_offset;
Josef Bacik1ae39932011-04-06 14:41:34 -04008344 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008345 u64 submit_len;
8346 int clone_offset = 0;
8347 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308348 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008349 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008350 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00008351
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008352 submit_len = orig_bio->bi_iter.bi_size;
8353 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8354 start_sector << 9, submit_len, &geom);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008355 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008356 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008357
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008358 if (geom.len >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008359 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008360 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008361 goto submit;
8362 }
8363
David Woodhouse53b381b2013-01-29 18:40:14 -05008364 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008365 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008366 async_submit = 0;
8367 else
8368 async_submit = 1;
8369
Liu Bo725130b2017-05-16 09:51:39 -07008370 /* bio split */
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008371 ASSERT(geom.len <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008372 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008373 do {
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008374 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008375
Liu Bo725130b2017-05-16 09:51:39 -07008376 /*
8377 * This will never fail as it's passing GPF_NOFS and
8378 * the allocation is backed by btrfs_bioset.
8379 */
Liu Boe4770942017-05-16 10:57:14 -07008380 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008381 clone_len);
8382 bio->bi_private = dip;
8383 bio->bi_end_io = btrfs_end_dio_bio;
8384 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008385
Liu Bo725130b2017-05-16 09:51:39 -07008386 ASSERT(submit_len >= clone_len);
8387 submit_len -= clone_len;
8388 if (submit_len == 0)
8389 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008390
Liu Bo725130b2017-05-16 09:51:39 -07008391 /*
8392 * Increase the count before we submit the bio so we know
8393 * the end IO handler won't happen before we increase the
8394 * count. Otherwise, the dip might get freed before we're
8395 * done setting it up.
8396 */
8397 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008398
David Sterbad0ee3932018-03-08 14:35:48 +01008399 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008400 async_submit);
8401 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008402 bio_put(bio);
8403 atomic_dec(&dip->pending_bios);
8404 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008405 }
Liu Bo725130b2017-05-16 09:51:39 -07008406
8407 clone_offset += clone_len;
8408 start_sector += clone_len >> 9;
8409 file_offset += clone_len;
8410
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008411 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8412 start_sector << 9, submit_len, &geom);
Liu Bo725130b2017-05-16 09:51:39 -07008413 if (ret)
8414 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008415 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008416
Josef Bacik02f57c72011-04-06 14:25:44 -04008417submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008418 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008419 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008420 return 0;
8421
8422 bio_put(bio);
8423out_err:
8424 dip->errors = 1;
8425 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008426 * Before atomic variable goto zero, we must make sure dip->errors is
8427 * perceived to be set. This ordering is ensured by the fact that an
8428 * atomic operations with a return value are fully ordered as per
8429 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008430 */
Miao Xiee65e1532010-11-22 03:04:43 +00008431 if (atomic_dec_and_test(&dip->pending_bios))
8432 bio_io_error(dip->orig_bio);
8433
8434 /* bio_end_io() will handle error, so we needn't return it */
8435 return 0;
8436}
8437
Mike Christie8a4c1e42016-06-05 14:31:50 -05008438static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8439 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008440{
Filipe Manana61de7182015-07-01 12:13:10 +01008441 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008442 struct bio *bio = NULL;
8443 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008444 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008445 int ret = 0;
8446
David Sterba8b6c1d52017-06-02 17:48:13 +02008447 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008448
Miao Xiec1dc0892014-09-12 18:43:56 +08008449 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008450 if (!dip) {
8451 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008452 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008453 }
8454
8455 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008456 dip->inode = inode;
8457 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008458 dip->bytes = dio_bio->bi_iter.bi_size;
8459 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008460 bio->bi_private = dip;
8461 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008462 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008463 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008464 io_bio = btrfs_io_bio(bio);
8465 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008466
Miao Xiec1dc0892014-09-12 18:43:56 +08008467 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008468 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008469 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008470 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008471 dip->subio_endio = btrfs_subio_endio_read;
8472 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008473
Filipe Mananaf28a4922015-12-08 19:23:20 +00008474 /*
8475 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8476 * even if we fail to submit a bio, because in such case we do the
8477 * corresponding error handling below and it must not be done a second
8478 * time by btrfs_direct_IO().
8479 */
8480 if (write) {
8481 struct btrfs_dio_data *dio_data = current->journal_info;
8482
8483 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8484 dip->bytes;
8485 dio_data->unsubmitted_oe_range_start =
8486 dio_data->unsubmitted_oe_range_end;
8487 }
8488
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008489 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008490 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008491 return;
Chris Mason9be33952013-05-17 18:30:14 -04008492
David Sterbab3a0dd52018-11-22 17:16:49 +01008493 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008494
Josef Bacik4b46fce2010-05-23 11:00:55 -04008495free_ordered:
8496 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008497 * If we arrived here it means either we failed to submit the dip
8498 * or we either failed to clone the dio_bio or failed to allocate the
8499 * dip. If we cloned the dio_bio and allocated the dip, we can just
8500 * call bio_endio against our io_bio so that we get proper resource
8501 * cleanup if we fail to submit the dip, otherwise, we must do the
8502 * same as btrfs_endio_direct_[write|read] because we can't call these
8503 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008504 */
Liu Bo3892ac92017-04-17 15:00:28 -07008505 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008506 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008507 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008508 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008509 * and all the cleanup and final put for dio_bio (through
8510 * dio_end_io()).
8511 */
8512 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008513 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008514 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008515 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008516 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008517 file_offset,
8518 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008519 false);
Filipe Manana14543772015-11-24 16:23:54 +00008520 else
Filipe Manana61de7182015-07-01 12:13:10 +01008521 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8522 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008523
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008524 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008525 /*
8526 * Releases and cleans up our dio_bio, no need to bio_put()
8527 * nor bio_endio()/bio_io_error() against dio_bio.
8528 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008529 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008530 }
Liu Bo3892ac92017-04-17 15:00:28 -07008531 if (bio)
8532 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008533 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008534}
8535
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008536static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008537 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008538{
8539 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008540 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008541 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008542 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008543
8544 if (offset & blocksize_mask)
8545 goto out;
8546
Al Viro28060d52014-03-22 05:15:17 -04008547 if (iov_iter_alignment(iter) & blocksize_mask)
8548 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008549
Al Viro28060d52014-03-22 05:15:17 -04008550 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008551 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008552 return 0;
8553 /*
8554 * Check to make sure we don't have duplicate iov_base's in this
8555 * iovec, if so return EINVAL, otherwise we'll get csum errors
8556 * when reading back.
8557 */
8558 for (seg = 0; seg < iter->nr_segs; seg++) {
8559 for (i = seg + 1; i < iter->nr_segs; i++) {
8560 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008561 goto out;
8562 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008563 }
8564 retval = 0;
8565out:
8566 return retval;
8567}
Josef Bacikeb838e72012-07-31 16:28:48 -04008568
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008569static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008570{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008571 struct file *file = iocb->ki_filp;
8572 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008573 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308574 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008575 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008576 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008577 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008578 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008579 bool wakeup = true;
8580 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008581 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008582
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008583 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008584 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008585
Jens Axboefe0f07d2015-04-15 17:05:48 -06008586 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008587
Josef Bacik0e267c42013-07-02 10:38:02 -04008588 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008589 * The generic stuff only does filemap_write_and_wait_range, which
8590 * isn't enough if we've written compressed pages to this area, so
8591 * we need to flush the dirty pages again to make absolutely sure
8592 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008593 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008594 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008595 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8596 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008597 filemap_fdatawrite_range(inode->i_mapping, offset,
8598 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008599
Omar Sandoval6f673762015-03-16 04:33:52 -07008600 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008601 /*
8602 * If the write DIO is beyond the EOF, we need update
8603 * the isize, but it is protected by i_mutex. So we can
8604 * not unlock the i_mutex at this case.
8605 */
8606 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008607 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008608 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008609 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008610 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8611 ret = -EAGAIN;
8612 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008613 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008614 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8615 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008616 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008617 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008618
8619 /*
8620 * We need to know how many extents we reserved so that we can
8621 * do the accounting properly if we go over the number we
8622 * originally calculated. Abuse current->journal_info for this.
8623 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008624 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008625 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008626 dio_data.unsubmitted_oe_range_start = (u64)offset;
8627 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308628 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308629 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008630 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8631 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008632 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008633 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8634 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008635 }
8636
Omar Sandoval17f8c842015-03-16 04:33:50 -07008637 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008638 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008639 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008640 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008641 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308642 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008643 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008644 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308645 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008646 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008647 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008648 /*
8649 * On error we might have left some ordered extents
8650 * without submitting corresponding bios for them, so
8651 * cleanup them up to avoid other tasks getting them
8652 * and waiting for them to complete forever.
8653 */
8654 if (dio_data.unsubmitted_oe_range_start <
8655 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008656 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008657 dio_data.unsubmitted_oe_range_start,
8658 dio_data.unsubmitted_oe_range_end -
8659 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008660 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008661 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008662 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008663 offset, count - (size_t)ret, true);
8664 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008665 }
Miao Xie38851cc2013-02-08 07:04:11 +00008666out:
Miao Xie2e60a512013-02-08 07:01:08 +00008667 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008668 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008669 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008670 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008671
Qu Wenruo364ecf32017-02-27 15:10:38 +08008672 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008673 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008674}
8675
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008676#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8677
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008678static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8679 __u64 start, __u64 len)
8680{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008681 int ret;
8682
8683 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8684 if (ret)
8685 return ret;
8686
David Sterba2135fb92017-06-23 04:09:57 +02008687 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008688}
8689
Chris Mason9ebefb182007-06-15 13:50:00 -04008690int btrfs_readpage(struct file *file, struct page *page)
8691{
Chris Masond1310b22008-01-24 16:13:08 -05008692 struct extent_io_tree *tree;
8693 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008694 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008695}
Chris Mason1832a6d2007-12-21 16:27:21 -05008696
Chris Mason39279cc2007-06-12 06:35:45 -04008697static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8698{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008699 struct inode *inode = page->mapping->host;
8700 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008701
8702 if (current->flags & PF_MEMALLOC) {
8703 redirty_page_for_writepage(wbc, page);
8704 unlock_page(page);
8705 return 0;
8706 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008707
8708 /*
8709 * If we are under memory pressure we will call this directly from the
8710 * VM, we need to make sure we have the inode referenced for the ordered
8711 * extent. If not just return like we didn't do anything.
8712 */
8713 if (!igrab(inode)) {
8714 redirty_page_for_writepage(wbc, page);
8715 return AOP_WRITEPAGE_ACTIVATE;
8716 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008717 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008718 btrfs_add_delayed_iput(inode);
8719 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008720}
Chris Mason39279cc2007-06-12 06:35:45 -04008721
Eric Sandeen48a3b632013-04-25 20:41:01 +00008722static int btrfs_writepages(struct address_space *mapping,
8723 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008724{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008725 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008726}
8727
Chris Mason3ab2fb52007-11-08 10:59:22 -05008728static int
8729btrfs_readpages(struct file *file, struct address_space *mapping,
8730 struct list_head *pages, unsigned nr_pages)
8731{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008732 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008733}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008734
Chris Masone6dcd2d2008-07-17 12:53:50 -04008735static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008736{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008737 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008738 if (ret == 1) {
8739 ClearPagePrivate(page);
8740 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008741 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008742 }
8743 return ret;
8744}
Chris Mason39279cc2007-06-12 06:35:45 -04008745
Chris Masone6dcd2d2008-07-17 12:53:50 -04008746static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8747{
Chris Mason98509cf2008-09-11 15:51:43 -04008748 if (PageWriteback(page) || PageDirty(page))
8749 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008750 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008751}
8752
Lukas Czernerd47992f2013-05-21 23:17:23 -04008753static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8754 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008755{
Josef Bacik5fd02042012-05-02 14:00:54 -04008756 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008757 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008758 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008759 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008760 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008761 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308762 u64 start;
8763 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008764 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008765
Chris Mason8b62b722009-09-02 16:53:46 -04008766 /*
8767 * we have the page locked, so new writeback can't start,
8768 * and the dirty bit won't be cleared while we are here.
8769 *
8770 * Wait for IO on this page so that we can safely clear
8771 * the PagePrivate2 bit and do ordered accounting
8772 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008773 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008774
Josef Bacik5fd02042012-05-02 14:00:54 -04008775 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008776 if (offset) {
8777 btrfs_releasepage(page, GFP_NOFS);
8778 return;
8779 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008780
8781 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008782 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308783again:
8784 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008785 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308786 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008787 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308788 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008789 /*
8790 * IO on this page will never be started, so we need
8791 * to account for any ordered extents now
8792 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008793 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308794 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008795 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008796 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008797 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008798 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008799 /*
8800 * whoever cleared the private bit is responsible
8801 * for the finish_ordered_io
8802 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008803 if (TestClearPagePrivate2(page)) {
8804 struct btrfs_ordered_inode_tree *tree;
8805 u64 new_len;
8806
8807 tree = &BTRFS_I(inode)->ordered_tree;
8808
8809 spin_lock_irq(&tree->lock);
8810 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308811 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008812 if (new_len < ordered->truncated_len)
8813 ordered->truncated_len = new_len;
8814 spin_unlock_irq(&tree->lock);
8815
8816 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308817 start,
8818 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008819 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008820 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008821 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008822 if (!inode_evicting) {
8823 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308824 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008825 &cached_state);
8826 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308827
8828 start = end + 1;
8829 if (start < page_end)
8830 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008831 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008832
Qu Wenruob9d0b382015-09-29 10:35:16 +08008833 /*
8834 * Qgroup reserved space handler
8835 * Page here will be either
8836 * 1) Already written to disk
8837 * In this case, its reserved space is released from data rsv map
8838 * and will be freed by delayed_ref handler finally.
8839 * So even we call qgroup_free_data(), it won't decrease reserved
8840 * space.
8841 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008842 * This means the reserved space should be freed here. However,
8843 * if a truncate invalidates the page (by clearing PageDirty)
8844 * and the page is accounted for while allocating extent
8845 * in btrfs_check_data_free_space() we let delayed_ref to
8846 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008847 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008848 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008849 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008850 if (!inode_evicting) {
8851 clear_extent_bit(tree, page_start, page_end,
8852 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008853 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8854 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008855 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008856
8857 __btrfs_releasepage(page, GFP_NOFS);
8858 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008859
Chris Mason4a096752008-07-21 10:29:44 -04008860 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008861 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008862 ClearPagePrivate(page);
8863 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008864 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008865 }
Chris Mason39279cc2007-06-12 06:35:45 -04008866}
8867
Chris Mason9ebefb182007-06-15 13:50:00 -04008868/*
8869 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8870 * called from a page fault handler when a page is first dirtied. Hence we must
8871 * be careful to check for EOF conditions here. We set the page up correctly
8872 * for a written page which means we get ENOSPC checking when writing into
8873 * holes and correct delalloc and unwritten extent mapping on filesystems that
8874 * support these features.
8875 *
8876 * We are not allowed to take the i_mutex here so we have to play games to
8877 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008878 * truncate_setsize() writes the inode size before removing pages, once we have
8879 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008880 * beyond EOF, then the page is guaranteed safe against truncation until we
8881 * unlock the page.
8882 */
Souptick Joardera528a242018-06-06 19:54:44 +05308883vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008884{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008885 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008886 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008887 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008888 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8889 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008890 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008891 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008892 char *kaddr;
8893 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008894 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308895 vm_fault_t ret;
8896 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008897 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308898 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008899 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008900 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308901 u64 end;
8902
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008903 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008904
Jan Karab2b5ef52012-06-12 16:20:45 +02008905 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008906 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008907 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308908 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008909
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308910 /*
8911 * Reserving delalloc space after obtaining the page lock can lead to
8912 * deadlock. For example, if a dirty page is locked by this function
8913 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8914 * dirty page write out, then the btrfs_writepage() function could
8915 * end up waiting indefinitely to get a lock on the page currently
8916 * being processed by btrfs_page_mkwrite() function.
8917 */
Souptick Joardera528a242018-06-06 19:54:44 +05308918 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308919 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308920 if (!ret2) {
8921 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008922 reserved = 1;
8923 }
Souptick Joardera528a242018-06-06 19:54:44 +05308924 if (ret2) {
8925 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008926 if (reserved)
8927 goto out;
8928 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008929 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008930
Nick Piggin56a76f82009-03-31 15:23:23 -07008931 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008932again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008933 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008934 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008935
Chris Mason9ebefb182007-06-15 13:50:00 -04008936 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008937 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008938 /* page got truncated out from underneath us */
8939 goto out_unlock;
8940 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008941 wait_on_page_writeback(page);
8942
David Sterbaff13db42015-12-03 14:30:40 +01008943 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008944 set_page_extent_mapped(page);
8945
Chris Masoneb84ae02008-07-17 13:53:27 -04008946 /*
8947 * we can't set the delalloc bits if there are pending ordered
8948 * extents. Drop our locks and wait for them to finish
8949 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008950 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8951 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008952 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008953 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008954 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008955 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008956 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008957 btrfs_put_ordered_extent(ordered);
8958 goto again;
8959 }
8960
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008961 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008962 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008963 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008964 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308965 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008966 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008967 page_start, PAGE_SIZE - reserved_space,
8968 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308969 }
8970 }
8971
Josef Bacikfbf19082009-10-01 17:10:23 -04008972 /*
Liu Bo54160342016-12-13 12:15:19 -08008973 * page_mkwrite gets called when the page is firstly dirtied after it's
8974 * faulted in, but write(2) could also dirty a page and set delalloc
8975 * bits, thus in this case for space account reason, we still need to
8976 * clear any delalloc bits within this page range since we have to
8977 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008978 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308979 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008980 EXTENT_DIRTY | EXTENT_DELALLOC |
8981 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008982 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008983
Souptick Joardera528a242018-06-06 19:54:44 +05308984 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008985 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308986 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008987 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008988 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008989 ret = VM_FAULT_SIGBUS;
8990 goto out_unlock;
8991 }
Souptick Joardera528a242018-06-06 19:54:44 +05308992 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008993
8994 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008995 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008996 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008997 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008998 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008999
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009000 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009001 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009002 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009003 flush_dcache_page(page);
9004 kunmap(page);
9005 }
Chris Mason247e7432008-07-17 12:53:51 -04009006 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009007 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009008 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009009
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009010 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009011 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009012 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009013
David Sterbae43bbe52017-12-12 21:43:52 +01009014 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009015
Souptick Joardera528a242018-06-06 19:54:44 +05309016 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08009017 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02009018 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009019 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009020 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009021 }
Chris Mason717beb92018-06-25 10:03:41 -07009022
9023out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04009024 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009025out:
Qu Wenruo43b18592017-12-12 15:34:32 +08009026 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08009027 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009028 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009029out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009030 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009031 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009032 return ret;
9033}
9034
Filipe Manana213e8c52018-02-06 20:40:31 +00009035static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009036{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009037 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009038 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009039 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009040 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009041 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009042 u64 mask = fs_info->sectorsize - 1;
9043 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009044
Filipe Manana213e8c52018-02-06 20:40:31 +00009045 if (!skip_writeback) {
9046 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9047 (u64)-1);
9048 if (ret)
9049 return ret;
9050 }
Chris Mason39279cc2007-06-12 06:35:45 -04009051
Josef Bacikfcb80c22011-05-03 10:40:22 -04009052 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009053 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9054 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04009055 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009056 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009057 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009058 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04009059 * be free'd up by the truncate operation, but also have some slack
9060 * space reserved in case it uses space during the truncate (thank you
9061 * very much snapshotting).
9062 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009063 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009064 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009065 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009066 * doesn't end up using space reserved for updating the inode. We also
9067 * need to be able to stop the transaction and start a new one, which
9068 * means we need to be able to update the inode several times, and we
9069 * have no idea of knowing how many times that will be, so we can't just
9070 * reserve 1 item for the entirety of the operation, so that has to be
9071 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009072 *
9073 * So that leaves us with
9074 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009075 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009076 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009077 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009078 * updating the inode.
9079 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009080 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009081 if (!rsv)
9082 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009083 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009084 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009085
Josef Bacik907cbce2011-08-08 13:46:15 -04009086 /*
Josef Bacik07127182011-08-19 10:29:59 -04009087 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009088 * 1 for updating the inode.
9089 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009090 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009091 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009092 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009093 goto out;
9094 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009095
Josef Bacik907cbce2011-08-08 13:46:15 -04009096 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009097 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009098 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009099 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009100
Chris Mason5a3f23d2009-03-31 13:27:11 -04009101 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009102 * So if we truncate and then write and fsync we normally would just
9103 * write the extents that changed, which is a problem if we need to
9104 * first truncate that entire inode. So set this flag so we write out
9105 * all of the extents in the inode to the sync log so we're completely
9106 * safe.
9107 */
9108 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009109 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009110
Yan, Zheng80825102009-11-12 09:35:36 +00009111 while (1) {
9112 ret = btrfs_truncate_inode_items(trans, root, inode,
9113 inode->i_size,
9114 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009115 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009116 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009117 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009118
Yan, Zheng80825102009-11-12 09:35:36 +00009119 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009120 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009121 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009122
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009123 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009124 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009125
9126 trans = btrfs_start_transaction(root, 2);
9127 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009128 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009129 trans = NULL;
9130 break;
9131 }
9132
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009133 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009134 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009135 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009136 BUG_ON(ret); /* shouldn't happen */
9137 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009138 }
9139
Josef Bacikddfae632017-10-19 14:16:02 -04009140 /*
9141 * We can't call btrfs_truncate_block inside a trans handle as we could
9142 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9143 * we've truncated everything except the last little bit, and can do
9144 * btrfs_truncate_block and then update the disk_i_size.
9145 */
9146 if (ret == NEED_TRUNCATE_BLOCK) {
9147 btrfs_end_transaction(trans);
9148 btrfs_btree_balance_dirty(fs_info);
9149
9150 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9151 if (ret)
9152 goto out;
9153 trans = btrfs_start_transaction(root, 1);
9154 if (IS_ERR(trans)) {
9155 ret = PTR_ERR(trans);
9156 goto out;
9157 }
9158 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9159 }
9160
Chris Mason917c16b2011-11-08 14:49:59 -05009161 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009162 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009163
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009164 trans->block_rsv = &fs_info->trans_block_rsv;
9165 ret2 = btrfs_update_inode(trans, root, inode);
9166 if (ret2 && !ret)
9167 ret = ret2;
9168
9169 ret2 = btrfs_end_transaction(trans);
9170 if (ret2 && !ret)
9171 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009172 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009173 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009174out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009175 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009176
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009177 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009178}
9179
Sven Wegener3b963622008-06-09 21:57:42 -04009180/*
Chris Masond352ac62008-09-29 15:18:18 -04009181 * create a new subvolume directory/inode (helper for the ioctl).
9182 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009183int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009184 struct btrfs_root *new_root,
9185 struct btrfs_root *parent_root,
9186 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009187{
Chris Mason39279cc2007-06-12 06:35:45 -04009188 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009189 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009190 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009191
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009192 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9193 new_dirid, new_dirid,
9194 S_IFDIR | (~current_umask() & S_IRWXUGO),
9195 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009196 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009197 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009198 inode->i_op = &btrfs_dir_inode_operations;
9199 inode->i_fop = &btrfs_dir_file_operations;
9200
Miklos Szeredibfe86842011-10-28 14:13:29 +02009201 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009202 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009203 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009204
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009205 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9206 if (err)
9207 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009208 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009209 new_root->root_key.objectid, err);
9210
Yan, Zheng76dda932009-09-21 16:00:26 -04009211 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009212
Yan, Zheng76dda932009-09-21 16:00:26 -04009213 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009214 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009215}
9216
Chris Mason39279cc2007-06-12 06:35:45 -04009217struct inode *btrfs_alloc_inode(struct super_block *sb)
9218{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009219 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009220 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009221 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009222
David Sterba712e36c2017-10-31 17:08:27 +01009223 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009224 if (!ei)
9225 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009226
9227 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009228 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009229 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009230 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009231 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009232 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009233 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009234 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009235 ei->disk_i_size = 0;
9236 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009237 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009238 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009239 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009240 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009241 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009242
Josef Bacik9e0baf62011-07-15 15:16:44 +00009243 spin_lock_init(&ei->lock);
9244 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009245 if (sb->s_magic != BTRFS_TEST_MAGIC)
9246 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9247 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009248 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009249 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009250 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009251
Miao Xie16cdcec2011-04-22 18:12:22 +08009252 ei->delayed_node = NULL;
9253
chandan r9cc97d62012-07-04 12:48:07 +05309254 ei->i_otime.tv_sec = 0;
9255 ei->i_otime.tv_nsec = 0;
9256
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009257 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009258 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08009259 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9260 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9261 IO_TREE_INODE_IO_FAILURE, inode);
David Sterba7b439732019-03-11 15:58:30 +01009262 ei->io_tree.track_uptodate = true;
9263 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05009264 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009265 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009266 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009267 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009268 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009269 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009270 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009271 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009272
9273 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009274}
9275
Josef Bacikaaedb552013-10-11 14:44:09 -04009276#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9277void btrfs_test_destroy_inode(struct inode *inode)
9278{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009279 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009280 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9281}
9282#endif
9283
Al Viro26602ca2019-04-10 15:14:41 -04009284void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009285{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009286 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9287}
9288
Chris Mason39279cc2007-06-12 06:35:45 -04009289void btrfs_destroy_inode(struct inode *inode)
9290{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009291 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009292 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009293 struct btrfs_root *root = BTRFS_I(inode)->root;
9294
Al Virob3d9b7a2012-06-09 13:51:19 -04009295 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009296 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009297 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9298 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009299 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009300 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009301 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009302 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009303 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009304
Chris Mason5a3f23d2009-03-31 13:27:11 -04009305 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009306 * This can happen where we create an inode, but somebody else also
9307 * created the same inode and we need to destroy the one we already
9308 * created.
9309 */
9310 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04009311 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05009312
Chris Masond3977122009-01-05 21:25:51 -05009313 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009314 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9315 if (!ordered)
9316 break;
9317 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009318 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009319 "found ordered extent %llu %llu on inode cleanup",
9320 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009321 btrfs_remove_ordered_extent(inode, ordered);
9322 btrfs_put_ordered_extent(ordered);
9323 btrfs_put_ordered_extent(ordered);
9324 }
9325 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009326 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009327 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009328 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04009329}
9330
Al Viro45321ac2010-06-07 13:43:19 -04009331int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009332{
9333 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009334
Naohiro Aota6379ef92013-06-06 09:56:34 +00009335 if (root == NULL)
9336 return 1;
9337
Liu Bofa6ac872013-02-20 14:10:23 +00009338 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009339 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009340 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009341 else
Al Viro45321ac2010-06-07 13:43:19 -04009342 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009343}
9344
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009345static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009346{
9347 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9348
9349 inode_init_once(&ei->vfs_inode);
9350}
9351
David Sterbae67c7182018-02-19 17:24:18 +01009352void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009353{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009354 /*
9355 * Make sure all delayed rcu free inodes are flushed before we
9356 * destroy cache.
9357 */
9358 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009359 kmem_cache_destroy(btrfs_inode_cachep);
9360 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009361 kmem_cache_destroy(btrfs_path_cachep);
9362 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009363}
9364
Liu Bof5c29bd2017-11-02 17:21:50 -06009365int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009366{
David Sterba837e1972012-09-07 03:00:48 -06009367 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009368 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009369 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9370 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009371 if (!btrfs_inode_cachep)
9372 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009373
David Sterba837e1972012-09-07 03:00:48 -06009374 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009375 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009376 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009377 if (!btrfs_trans_handle_cachep)
9378 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009379
David Sterba837e1972012-09-07 03:00:48 -06009380 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009381 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009382 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009383 if (!btrfs_path_cachep)
9384 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009385
David Sterba837e1972012-09-07 03:00:48 -06009386 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009387 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009388 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009389 if (!btrfs_free_space_cachep)
9390 goto fail;
9391
Chris Mason39279cc2007-06-12 06:35:45 -04009392 return 0;
9393fail:
9394 btrfs_destroy_cachep();
9395 return -ENOMEM;
9396}
9397
David Howellsa528d352017-01-31 16:46:22 +00009398static int btrfs_getattr(const struct path *path, struct kstat *stat,
9399 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009400{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009401 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009402 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009403 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009404 u32 bi_flags = BTRFS_I(inode)->flags;
9405
9406 stat->result_mask |= STATX_BTIME;
9407 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9408 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9409 if (bi_flags & BTRFS_INODE_APPEND)
9410 stat->attributes |= STATX_ATTR_APPEND;
9411 if (bi_flags & BTRFS_INODE_COMPRESS)
9412 stat->attributes |= STATX_ATTR_COMPRESSED;
9413 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9414 stat->attributes |= STATX_ATTR_IMMUTABLE;
9415 if (bi_flags & BTRFS_INODE_NODUMP)
9416 stat->attributes |= STATX_ATTR_NODUMP;
9417
9418 stat->attributes_mask |= (STATX_ATTR_APPEND |
9419 STATX_ATTR_COMPRESSED |
9420 STATX_ATTR_IMMUTABLE |
9421 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009422
Chris Mason39279cc2007-06-12 06:35:45 -04009423 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009424 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009425
9426 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009427 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009428 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009429 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009430 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009431 return 0;
9432}
9433
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009434static int btrfs_rename_exchange(struct inode *old_dir,
9435 struct dentry *old_dentry,
9436 struct inode *new_dir,
9437 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009438{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009439 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009440 struct btrfs_trans_handle *trans;
9441 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009442 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009443 struct inode *new_inode = new_dentry->d_inode;
9444 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009445 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009446 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009447 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9448 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009449 u64 old_idx = 0;
9450 u64 new_idx = 0;
9451 u64 root_objectid;
9452 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009453 bool root_log_pinned = false;
9454 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009455 struct btrfs_log_ctx ctx_root;
9456 struct btrfs_log_ctx ctx_dest;
9457 bool sync_log_root = false;
9458 bool sync_log_dest = false;
9459 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009460
9461 /* we only allow rename subvolume link between subvolumes */
9462 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9463 return -EXDEV;
9464
Filipe Mananad4682ba2018-06-11 19:24:28 +01009465 btrfs_init_log_ctx(&ctx_root, old_inode);
9466 btrfs_init_log_ctx(&ctx_dest, new_inode);
9467
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009468 /* close the race window with snapshot create/destroy ioctl */
9469 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009470 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009471 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009472 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009473
9474 /*
9475 * We want to reserve the absolute worst case amount of items. So if
9476 * both inodes are subvols and we need to unlink them then that would
9477 * require 4 item modifications, but if they are both normal inodes it
9478 * would require 5 item modifications, so we'll assume their normal
9479 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9480 * should cover the worst case number of items we'll modify.
9481 */
9482 trans = btrfs_start_transaction(root, 12);
9483 if (IS_ERR(trans)) {
9484 ret = PTR_ERR(trans);
9485 goto out_notrans;
9486 }
9487
9488 /*
9489 * We need to find a free sequence number both in the source and
9490 * in the destination directory for the exchange.
9491 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009492 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009493 if (ret)
9494 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009495 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009496 if (ret)
9497 goto out_fail;
9498
9499 BTRFS_I(old_inode)->dir_index = 0ULL;
9500 BTRFS_I(new_inode)->dir_index = 0ULL;
9501
9502 /* Reference for the source. */
9503 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9504 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009505 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009506 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009507 btrfs_pin_log_trans(root);
9508 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009509 ret = btrfs_insert_inode_ref(trans, dest,
9510 new_dentry->d_name.name,
9511 new_dentry->d_name.len,
9512 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009513 btrfs_ino(BTRFS_I(new_dir)),
9514 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009515 if (ret)
9516 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009517 }
9518
9519 /* And now for the dest. */
9520 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9521 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009522 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009523 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009524 btrfs_pin_log_trans(dest);
9525 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009526 ret = btrfs_insert_inode_ref(trans, root,
9527 old_dentry->d_name.name,
9528 old_dentry->d_name.len,
9529 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009530 btrfs_ino(BTRFS_I(old_dir)),
9531 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009532 if (ret)
9533 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009534 }
9535
9536 /* Update inode version and ctime/mtime. */
9537 inode_inc_iversion(old_dir);
9538 inode_inc_iversion(new_dir);
9539 inode_inc_iversion(old_inode);
9540 inode_inc_iversion(new_inode);
9541 old_dir->i_ctime = old_dir->i_mtime = ctime;
9542 new_dir->i_ctime = new_dir->i_mtime = ctime;
9543 old_inode->i_ctime = ctime;
9544 new_inode->i_ctime = ctime;
9545
9546 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009547 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9548 BTRFS_I(old_inode), 1);
9549 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9550 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009551 }
9552
9553 /* src is a subvolume */
9554 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9555 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009556 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009557 old_dentry->d_name.name,
9558 old_dentry->d_name.len);
9559 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009560 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9561 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009562 old_dentry->d_name.name,
9563 old_dentry->d_name.len);
9564 if (!ret)
9565 ret = btrfs_update_inode(trans, root, old_inode);
9566 }
9567 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009568 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 goto out_fail;
9570 }
9571
9572 /* dest is a subvolume */
9573 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9574 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009575 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009576 new_dentry->d_name.name,
9577 new_dentry->d_name.len);
9578 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009579 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9580 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009581 new_dentry->d_name.name,
9582 new_dentry->d_name.len);
9583 if (!ret)
9584 ret = btrfs_update_inode(trans, dest, new_inode);
9585 }
9586 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009587 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009588 goto out_fail;
9589 }
9590
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009591 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009592 new_dentry->d_name.name,
9593 new_dentry->d_name.len, 0, old_idx);
9594 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009595 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009596 goto out_fail;
9597 }
9598
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009599 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009600 old_dentry->d_name.name,
9601 old_dentry->d_name.len, 0, new_idx);
9602 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009603 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009604 goto out_fail;
9605 }
9606
9607 if (old_inode->i_nlink == 1)
9608 BTRFS_I(old_inode)->dir_index = old_idx;
9609 if (new_inode->i_nlink == 1)
9610 BTRFS_I(new_inode)->dir_index = new_idx;
9611
Filipe Manana86e8aa02016-05-05 02:02:27 +01009612 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009614 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9615 BTRFS_I(old_dir), parent,
9616 false, &ctx_root);
9617 if (ret == BTRFS_NEED_LOG_SYNC)
9618 sync_log_root = true;
9619 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9620 commit_transaction = true;
9621 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009622 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009623 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009624 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009625 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009626 if (!commit_transaction) {
9627 parent = old_dentry->d_parent;
9628 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9629 BTRFS_I(new_dir), parent,
9630 false, &ctx_dest);
9631 if (ret == BTRFS_NEED_LOG_SYNC)
9632 sync_log_dest = true;
9633 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9634 commit_transaction = true;
9635 ret = 0;
9636 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009637 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009638 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009639 }
9640out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009641 /*
9642 * If we have pinned a log and an error happened, we unpin tasks
9643 * trying to sync the log and force them to fallback to a transaction
9644 * commit if the log currently contains any of the inodes involved in
9645 * this rename operation (to ensure we do not persist a log with an
9646 * inconsistent state for any of these inodes or leading to any
9647 * inconsistencies when replayed). If the transaction was aborted, the
9648 * abortion reason is propagated to userspace when attempting to commit
9649 * the transaction. If the log does not contain any of these inodes, we
9650 * allow the tasks to sync it.
9651 */
9652 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009653 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9654 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9655 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009656 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009657 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009658 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009659
9660 if (root_log_pinned) {
9661 btrfs_end_log_trans(root);
9662 root_log_pinned = false;
9663 }
9664 if (dest_log_pinned) {
9665 btrfs_end_log_trans(dest);
9666 dest_log_pinned = false;
9667 }
9668 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009669 if (!ret && sync_log_root && !commit_transaction) {
9670 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9671 &ctx_root);
9672 if (ret)
9673 commit_transaction = true;
9674 }
9675 if (!ret && sync_log_dest && !commit_transaction) {
9676 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9677 &ctx_dest);
9678 if (ret)
9679 commit_transaction = true;
9680 }
9681 if (commit_transaction) {
9682 ret = btrfs_commit_transaction(trans);
9683 } else {
9684 int ret2;
9685
9686 ret2 = btrfs_end_transaction(trans);
9687 ret = ret ? ret : ret2;
9688 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009689out_notrans:
9690 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009691 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009693 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694
9695 return ret;
9696}
9697
9698static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9699 struct btrfs_root *root,
9700 struct inode *dir,
9701 struct dentry *dentry)
9702{
9703 int ret;
9704 struct inode *inode;
9705 u64 objectid;
9706 u64 index;
9707
9708 ret = btrfs_find_free_ino(root, &objectid);
9709 if (ret)
9710 return ret;
9711
9712 inode = btrfs_new_inode(trans, root, dir,
9713 dentry->d_name.name,
9714 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009715 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 objectid,
9717 S_IFCHR | WHITEOUT_MODE,
9718 &index);
9719
9720 if (IS_ERR(inode)) {
9721 ret = PTR_ERR(inode);
9722 return ret;
9723 }
9724
9725 inode->i_op = &btrfs_special_inode_operations;
9726 init_special_inode(inode, inode->i_mode,
9727 WHITEOUT_DEV);
9728
9729 ret = btrfs_init_inode_security(trans, inode, dir,
9730 &dentry->d_name);
9731 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009732 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009733
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009734 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9735 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009736 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009737 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009738
9739 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009740out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009741 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009742 if (ret)
9743 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009744 iput(inode);
9745
Filipe Mananac9901612016-05-05 01:41:57 +01009746 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009747}
9748
Chris Mason39279cc2007-06-12 06:35:45 -04009749static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009750 struct inode *new_dir, struct dentry *new_dentry,
9751 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009752{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009753 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009754 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009755 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009756 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9757 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009758 struct inode *new_inode = d_inode(new_dentry);
9759 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009760 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009761 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009762 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009763 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009764 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009765 struct btrfs_log_ctx ctx;
9766 bool sync_log = false;
9767 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009768
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009769 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009770 return -EPERM;
9771
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009772 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009773 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009774 return -EXDEV;
9775
Li Zefan33345d012011-04-20 10:31:50 +08009776 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009777 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009778 return -ENOTEMPTY;
9779
Chris Mason39279cc2007-06-12 06:35:45 -04009780 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009781 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009782 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009783
9784
9785 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009786 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009787 new_dentry->d_name.name,
9788 new_dentry->d_name.len);
9789
9790 if (ret) {
9791 if (ret == -EEXIST) {
9792 /* we shouldn't get
9793 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309794 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009795 return ret;
9796 }
9797 } else {
9798 /* maybe -EOVERFLOW */
9799 return ret;
9800 }
9801 }
9802 ret = 0;
9803
Chris Mason5a3f23d2009-03-31 13:27:11 -04009804 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009805 * we're using rename to replace one file with another. Start IO on it
9806 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009807 */
Chris Mason8d875f92014-08-12 10:47:42 -07009808 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009809 filemap_flush(old_inode->i_mapping);
9810
Yan, Zheng76dda932009-09-21 16:00:26 -04009811 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009812 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009813 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009814 /*
9815 * We want to reserve the absolute worst case amount of items. So if
9816 * both inodes are subvols and we need to unlink them then that would
9817 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009818 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009819 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9820 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009821 * If our rename has the whiteout flag, we need more 5 units for the
9822 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9823 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009824 */
Filipe Manana5062af32016-05-05 10:26:26 +01009825 trans_num_items = 11;
9826 if (flags & RENAME_WHITEOUT)
9827 trans_num_items += 5;
9828 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009829 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009830 ret = PTR_ERR(trans);
9831 goto out_notrans;
9832 }
Chris Mason5f39d392007-10-15 16:14:19 -04009833
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009834 if (dest != root)
9835 btrfs_record_root_in_trans(trans, dest);
9836
Nikolay Borisov877574e2017-02-20 13:50:33 +02009837 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009838 if (ret)
9839 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009840
Miao Xie67de1172013-12-26 13:07:06 +08009841 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009842 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009843 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009844 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009845 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009846 btrfs_pin_log_trans(root);
9847 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009848 ret = btrfs_insert_inode_ref(trans, dest,
9849 new_dentry->d_name.name,
9850 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009851 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009852 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009853 if (ret)
9854 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009855 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009856
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009857 inode_inc_iversion(old_dir);
9858 inode_inc_iversion(new_dir);
9859 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009860 old_dir->i_ctime = old_dir->i_mtime =
9861 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009862 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009863
Chris Mason12fcfd22009-03-24 10:24:20 -04009864 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009865 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9866 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009867
Li Zefan33345d012011-04-20 10:31:50 +08009868 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009869 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009870 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009871 old_dentry->d_name.name,
9872 old_dentry->d_name.len);
9873 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009874 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9875 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009876 old_dentry->d_name.name,
9877 old_dentry->d_name.len);
9878 if (!ret)
9879 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009880 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009881 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009882 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009883 goto out_fail;
9884 }
Chris Mason39279cc2007-06-12 06:35:45 -04009885
9886 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009887 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009888 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009889 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009890 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9891 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009892 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009893 new_dentry->d_name.name,
9894 new_dentry->d_name.len);
9895 BUG_ON(new_inode->i_nlink == 0);
9896 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009897 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9898 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009899 new_dentry->d_name.name,
9900 new_dentry->d_name.len);
9901 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009902 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009903 ret = btrfs_orphan_add(trans,
9904 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009905 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009906 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009907 goto out_fail;
9908 }
Chris Mason39279cc2007-06-12 06:35:45 -04009909 }
Josef Bacikaec74772008-07-24 12:12:38 -04009910
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009911 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009912 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009913 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009914 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009915 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009916 goto out_fail;
9917 }
Chris Mason39279cc2007-06-12 06:35:45 -04009918
Miao Xie67de1172013-12-26 13:07:06 +08009919 if (old_inode->i_nlink == 1)
9920 BTRFS_I(old_inode)->dir_index = index;
9921
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009922 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009923 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009924
Filipe Mananad4682ba2018-06-11 19:24:28 +01009925 btrfs_init_log_ctx(&ctx, old_inode);
9926 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9927 BTRFS_I(old_dir), parent,
9928 false, &ctx);
9929 if (ret == BTRFS_NEED_LOG_SYNC)
9930 sync_log = true;
9931 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9932 commit_transaction = true;
9933 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009934 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009935 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009936 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009937
9938 if (flags & RENAME_WHITEOUT) {
9939 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9940 old_dentry);
9941
9942 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009943 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009944 goto out_fail;
9945 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009946 }
Chris Mason39279cc2007-06-12 06:35:45 -04009947out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009948 /*
9949 * If we have pinned the log and an error happened, we unpin tasks
9950 * trying to sync the log and force them to fallback to a transaction
9951 * commit if the log currently contains any of the inodes involved in
9952 * this rename operation (to ensure we do not persist a log with an
9953 * inconsistent state for any of these inodes or leading to any
9954 * inconsistencies when replayed). If the transaction was aborted, the
9955 * abortion reason is propagated to userspace when attempting to commit
9956 * the transaction. If the log does not contain any of these inodes, we
9957 * allow the tasks to sync it.
9958 */
9959 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009960 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9961 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9962 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009963 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009964 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009965 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009966
9967 btrfs_end_log_trans(root);
9968 log_pinned = false;
9969 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009970 if (!ret && sync_log) {
9971 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9972 if (ret)
9973 commit_transaction = true;
9974 }
9975 if (commit_transaction) {
9976 ret = btrfs_commit_transaction(trans);
9977 } else {
9978 int ret2;
9979
9980 ret2 = btrfs_end_transaction(trans);
9981 ret = ret ? ret : ret2;
9982 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009983out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009984 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009985 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009986
Chris Mason39279cc2007-06-12 06:35:45 -04009987 return ret;
9988}
9989
Miklos Szeredi80ace852014-07-23 15:15:32 +02009990static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9991 struct inode *new_dir, struct dentry *new_dentry,
9992 unsigned int flags)
9993{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009994 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009995 return -EINVAL;
9996
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009997 if (flags & RENAME_EXCHANGE)
9998 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9999 new_dentry);
10000
10001 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010002}
10003
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010004struct btrfs_delalloc_work {
10005 struct inode *inode;
10006 struct completion completion;
10007 struct list_head list;
10008 struct btrfs_work work;
10009};
10010
Miao Xie8ccf6f192012-10-25 09:28:04 +000010011static void btrfs_run_delalloc_work(struct btrfs_work *work)
10012{
10013 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010014 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010015
10016 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10017 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010018 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010019 filemap_flush(inode->i_mapping);
10020 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10021 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010022 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010023
Nikolay Borisov076da912018-04-23 10:54:16 +030010024 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010025 complete(&delalloc_work->completion);
10026}
10027
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +030010028static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010029{
10030 struct btrfs_delalloc_work *work;
10031
David Sterba100d5702015-12-08 14:39:32 +010010032 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010033 if (!work)
10034 return NULL;
10035
10036 init_completion(&work->completion);
10037 INIT_LIST_HEAD(&work->list);
10038 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +080010039 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10040 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010041
10042 return work;
10043}
10044
Chris Masond352ac62008-09-29 15:18:18 -040010045/*
10046 * some fairly slow code that needs optimization. This walks the list
10047 * of all the inodes with pending delalloc and forces them to disk.
10048 */
Ethan Lien3cd24c62018-11-01 14:49:03 +080010049static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -040010050{
Chris Masonea8c2812008-08-04 23:17:27 -040010051 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010052 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010053 struct btrfs_delalloc_work *work, *next;
10054 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010055 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010056 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010057
Miao Xie8ccf6f192012-10-25 09:28:04 +000010058 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010059 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010060
Miao Xie573bfb72014-03-06 13:55:03 +080010061 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010062 spin_lock(&root->delalloc_lock);
10063 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010064 while (!list_empty(&splice)) {
10065 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010066 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010067
Miao Xieeb73c1b2013-05-15 07:48:22 +000010068 list_move_tail(&binode->delalloc_inodes,
10069 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010070 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010071 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010072 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010073 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010074 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010075 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010076
Ethan Lien3cd24c62018-11-01 14:49:03 +080010077 if (snapshot)
10078 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10079 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +030010080 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010081 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010082 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010083 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010084 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010085 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010086 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010087 btrfs_queue_work(root->fs_info->flush_workers,
10088 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010089 ret++;
10090 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010091 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010092 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010093 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010094 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010095 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010096
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010097out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010098 list_for_each_entry_safe(work, next, &works, list) {
10099 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010100 wait_for_completion(&work->completion);
10101 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010102 }
10103
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010104 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010105 spin_lock(&root->delalloc_lock);
10106 list_splice_tail(&splice, &root->delalloc_inodes);
10107 spin_unlock(&root->delalloc_lock);
10108 }
Miao Xie573bfb72014-03-06 13:55:03 +080010109 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110 return ret;
10111}
10112
Ethan Lien3cd24c62018-11-01 14:49:03 +080010113int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010114{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010115 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010116 int ret;
10117
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010118 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010119 return -EROFS;
10120
Ethan Lien3cd24c62018-11-01 14:49:03 +080010121 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010122 if (ret > 0)
10123 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010124 return ret;
10125}
10126
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010127int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010128{
10129 struct btrfs_root *root;
10130 struct list_head splice;
10131 int ret;
10132
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010133 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010134 return -EROFS;
10135
10136 INIT_LIST_HEAD(&splice);
10137
Miao Xie573bfb72014-03-06 13:55:03 +080010138 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010139 spin_lock(&fs_info->delalloc_root_lock);
10140 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010141 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010142 root = list_first_entry(&splice, struct btrfs_root,
10143 delalloc_root);
10144 root = btrfs_grab_fs_root(root);
10145 BUG_ON(!root);
10146 list_move_tail(&root->delalloc_root,
10147 &fs_info->delalloc_roots);
10148 spin_unlock(&fs_info->delalloc_root_lock);
10149
Ethan Lien3cd24c62018-11-01 14:49:03 +080010150 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010151 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010152 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010153 goto out;
10154
Miao Xie6c255e62014-03-06 13:55:01 +080010155 if (nr != -1) {
10156 nr -= ret;
10157 WARN_ON(nr < 0);
10158 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010159 spin_lock(&fs_info->delalloc_root_lock);
10160 }
10161 spin_unlock(&fs_info->delalloc_root_lock);
10162
Miao Xie6c255e62014-03-06 13:55:01 +080010163 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010164out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010165 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010166 spin_lock(&fs_info->delalloc_root_lock);
10167 list_splice_tail(&splice, &fs_info->delalloc_roots);
10168 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010169 }
Miao Xie573bfb72014-03-06 13:55:03 +080010170 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010171 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010172}
10173
Chris Mason39279cc2007-06-12 06:35:45 -040010174static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10175 const char *symname)
10176{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010177 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010178 struct btrfs_trans_handle *trans;
10179 struct btrfs_root *root = BTRFS_I(dir)->root;
10180 struct btrfs_path *path;
10181 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010182 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010183 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010184 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010185 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010186 int name_len;
10187 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010188 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010189 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010190 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010191
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010192 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010193 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010194 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010195
Josef Bacik9ed74f22009-09-11 16:12:44 -040010196 /*
10197 * 2 items for inode item and ref
10198 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010199 * 1 item for updating parent inode item
10200 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010201 * 1 item for xattr if selinux is on
10202 */
Filipe Manana9269d122015-12-31 18:16:29 +000010203 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010204 if (IS_ERR(trans))
10205 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010206
Li Zefan581bb052011-04-20 10:06:11 +080010207 err = btrfs_find_free_ino(root, &objectid);
10208 if (err)
10209 goto out_unlock;
10210
Josef Bacikaec74772008-07-24 12:12:38 -040010211 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010212 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10213 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010214 if (IS_ERR(inode)) {
10215 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010216 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010217 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010218 }
Chris Mason39279cc2007-06-12 06:35:45 -040010219
Casey Schauflerad19db72011-12-15 10:09:07 -050010220 /*
10221 * If the active LSM wants to access the inode during
10222 * d_instantiate it needs these. Smack checks to see
10223 * if the filesystem supports xattrs by looking at the
10224 * ops vector.
10225 */
10226 inode->i_fop = &btrfs_file_operations;
10227 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010228 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010229 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10230
10231 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10232 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010233 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010234
Chris Mason39279cc2007-06-12 06:35:45 -040010235 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010236 if (!path) {
10237 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010238 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010239 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010240 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010241 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010242 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010243 datasize = btrfs_file_extent_calc_inline_size(name_len);
10244 err = btrfs_insert_empty_item(trans, root, path, &key,
10245 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010246 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010247 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010248 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010249 }
Chris Mason5f39d392007-10-15 16:14:19 -040010250 leaf = path->nodes[0];
10251 ei = btrfs_item_ptr(leaf, path->slots[0],
10252 struct btrfs_file_extent_item);
10253 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10254 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010255 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010256 btrfs_set_file_extent_encryption(leaf, ei, 0);
10257 btrfs_set_file_extent_compression(leaf, ei, 0);
10258 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10259 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10260
Chris Mason39279cc2007-06-12 06:35:45 -040010261 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010262 write_extent_buffer(leaf, symname, ptr, name_len);
10263 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010264 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010265
Chris Mason39279cc2007-06-12 06:35:45 -040010266 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010267 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -040010268 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010269 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010270 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010271 /*
10272 * Last step, add directory indexes for our symlink inode. This is the
10273 * last step to avoid extra cleanup of these indexes if an error happens
10274 * elsewhere above.
10275 */
10276 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010277 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10278 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010279 if (err)
10280 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010281
Al Viro1e2e5472018-05-04 08:23:01 -040010282 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010283
10284out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010285 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010286 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010287 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010288 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010289 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010290 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010291 return err;
10292}
Chris Mason16432982008-04-10 10:23:21 -040010293
Josef Bacik0af3d002010-06-21 14:48:16 -040010294static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10295 u64 start, u64 num_bytes, u64 min_size,
10296 loff_t actual_len, u64 *alloc_hint,
10297 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010298{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010299 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010300 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10301 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010302 struct btrfs_root *root = BTRFS_I(inode)->root;
10303 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010304 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010305 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010306 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010307 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010308 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010309 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010310 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010311
Josef Bacik0af3d002010-06-21 14:48:16 -040010312 if (trans)
10313 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010314 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010315 if (own_trans) {
10316 trans = btrfs_start_transaction(root, 3);
10317 if (IS_ERR(trans)) {
10318 ret = PTR_ERR(trans);
10319 break;
10320 }
Yan Zhengd899e052008-10-30 14:25:28 -040010321 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010322
Byongho Leeee221842015-12-15 01:42:10 +090010323 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010324 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010325 /*
10326 * If we are severely fragmented we could end up with really
10327 * small allocations, so if the allocator is returning small
10328 * chunks lets make its job easier by only searching for those
10329 * sized chunks.
10330 */
10331 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010332 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10333 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010334 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010335 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010336 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010337 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010338 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010339 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010340
Josef Bacik0b670dc2015-09-23 17:11:16 -040010341 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010342 ret = insert_reserved_file_extent(trans, inode,
10343 cur_offset, ins.objectid,
10344 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010345 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010346 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010347 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010348 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010349 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010350 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010351 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010352 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010353 break;
10354 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010355
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010356 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010357 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010358
Josef Bacik5dc562c2012-08-17 13:14:17 -040010359 em = alloc_extent_map();
10360 if (!em) {
10361 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10362 &BTRFS_I(inode)->runtime_flags);
10363 goto next;
10364 }
10365
10366 em->start = cur_offset;
10367 em->orig_start = cur_offset;
10368 em->len = ins.offset;
10369 em->block_start = ins.objectid;
10370 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010371 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010372 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010373 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010374 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10375 em->generation = trans->transid;
10376
10377 while (1) {
10378 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010379 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010380 write_unlock(&em_tree->lock);
10381 if (ret != -EEXIST)
10382 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010383 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010384 cur_offset + ins.offset - 1,
10385 0);
10386 }
10387 free_extent_map(em);
10388next:
Yan Zhengd899e052008-10-30 14:25:28 -040010389 num_bytes -= ins.offset;
10390 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010391 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010392
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010393 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010394 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010395 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010396 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010397 (actual_len > inode->i_size) &&
10398 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010399 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010400 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010401 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010402 i_size = cur_offset;
10403 i_size_write(inode, i_size);
10404 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010405 }
10406
Yan Zhengd899e052008-10-30 14:25:28 -040010407 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010408
10409 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010410 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010411 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010412 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010413 break;
10414 }
Yan Zhengd899e052008-10-30 14:25:28 -040010415
Josef Bacik0af3d002010-06-21 14:48:16 -040010416 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010417 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010418 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010419 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010420 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010421 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010422 return ret;
10423}
10424
Josef Bacik0af3d002010-06-21 14:48:16 -040010425int btrfs_prealloc_file_range(struct inode *inode, int mode,
10426 u64 start, u64 num_bytes, u64 min_size,
10427 loff_t actual_len, u64 *alloc_hint)
10428{
10429 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10430 min_size, actual_len, alloc_hint,
10431 NULL);
10432}
10433
10434int btrfs_prealloc_file_range_trans(struct inode *inode,
10435 struct btrfs_trans_handle *trans, int mode,
10436 u64 start, u64 num_bytes, u64 min_size,
10437 loff_t actual_len, u64 *alloc_hint)
10438{
10439 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10440 min_size, actual_len, alloc_hint, trans);
10441}
10442
Chris Masone6dcd2d2008-07-17 12:53:50 -040010443static int btrfs_set_page_dirty(struct page *page)
10444{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010445 return __set_page_dirty_nobuffers(page);
10446}
10447
Al Viro10556cb22011-06-20 19:28:19 -040010448static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010449{
Li Zefanb83cc962010-12-20 16:04:08 +080010450 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010451 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010452
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010453 if (mask & MAY_WRITE &&
10454 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10455 if (btrfs_root_readonly(root))
10456 return -EROFS;
10457 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10458 return -EACCES;
10459 }
Al Viro2830ba72011-06-20 19:16:29 -040010460 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010461}
Chris Mason39279cc2007-06-12 06:35:45 -040010462
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010463static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10464{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010465 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010466 struct btrfs_trans_handle *trans;
10467 struct btrfs_root *root = BTRFS_I(dir)->root;
10468 struct inode *inode = NULL;
10469 u64 objectid;
10470 u64 index;
10471 int ret = 0;
10472
10473 /*
10474 * 5 units required for adding orphan entry
10475 */
10476 trans = btrfs_start_transaction(root, 5);
10477 if (IS_ERR(trans))
10478 return PTR_ERR(trans);
10479
10480 ret = btrfs_find_free_ino(root, &objectid);
10481 if (ret)
10482 goto out;
10483
10484 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010485 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010486 if (IS_ERR(inode)) {
10487 ret = PTR_ERR(inode);
10488 inode = NULL;
10489 goto out;
10490 }
10491
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010492 inode->i_fop = &btrfs_file_operations;
10493 inode->i_op = &btrfs_file_inode_operations;
10494
10495 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010496 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10497
Chris Masonb0d5d102014-09-08 13:08:51 -070010498 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10499 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010500 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010501
10502 ret = btrfs_update_inode(trans, root, inode);
10503 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010504 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010505 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010506 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010507 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010508
Filipe Manana5762b5c2014-08-01 00:10:32 +010010509 /*
10510 * We set number of links to 0 in btrfs_new_inode(), and here we set
10511 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10512 * through:
10513 *
10514 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10515 */
10516 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010517 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010518 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010519 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010520out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010521 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010522 if (ret && inode)
10523 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010524 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010525 return ret;
10526}
10527
David Sterba5cdc84b2018-07-18 20:32:52 +020010528void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010529{
David Sterba5cdc84b2018-07-18 20:32:52 +020010530 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010531 unsigned long index = start >> PAGE_SHIFT;
10532 unsigned long end_index = end >> PAGE_SHIFT;
10533 struct page *page;
10534
10535 while (index <= end_index) {
10536 page = find_get_page(inode->i_mapping, index);
10537 ASSERT(page); /* Pages should be in the extent_io_tree */
10538 set_page_writeback(page);
10539 put_page(page);
10540 index++;
10541 }
10542}
10543
Omar Sandovaled46ff32016-11-03 10:28:14 -070010544#ifdef CONFIG_SWAP
10545/*
10546 * Add an entry indicating a block group or device which is pinned by a
10547 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10548 * negative errno on failure.
10549 */
10550static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10551 bool is_block_group)
10552{
10553 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10554 struct btrfs_swapfile_pin *sp, *entry;
10555 struct rb_node **p;
10556 struct rb_node *parent = NULL;
10557
10558 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10559 if (!sp)
10560 return -ENOMEM;
10561 sp->ptr = ptr;
10562 sp->inode = inode;
10563 sp->is_block_group = is_block_group;
10564
10565 spin_lock(&fs_info->swapfile_pins_lock);
10566 p = &fs_info->swapfile_pins.rb_node;
10567 while (*p) {
10568 parent = *p;
10569 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10570 if (sp->ptr < entry->ptr ||
10571 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10572 p = &(*p)->rb_left;
10573 } else if (sp->ptr > entry->ptr ||
10574 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10575 p = &(*p)->rb_right;
10576 } else {
10577 spin_unlock(&fs_info->swapfile_pins_lock);
10578 kfree(sp);
10579 return 1;
10580 }
10581 }
10582 rb_link_node(&sp->node, parent, p);
10583 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10584 spin_unlock(&fs_info->swapfile_pins_lock);
10585 return 0;
10586}
10587
10588/* Free all of the entries pinned by this swapfile. */
10589static void btrfs_free_swapfile_pins(struct inode *inode)
10590{
10591 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10592 struct btrfs_swapfile_pin *sp;
10593 struct rb_node *node, *next;
10594
10595 spin_lock(&fs_info->swapfile_pins_lock);
10596 node = rb_first(&fs_info->swapfile_pins);
10597 while (node) {
10598 next = rb_next(node);
10599 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10600 if (sp->inode == inode) {
10601 rb_erase(&sp->node, &fs_info->swapfile_pins);
10602 if (sp->is_block_group)
10603 btrfs_put_block_group(sp->ptr);
10604 kfree(sp);
10605 }
10606 node = next;
10607 }
10608 spin_unlock(&fs_info->swapfile_pins_lock);
10609}
10610
10611struct btrfs_swap_info {
10612 u64 start;
10613 u64 block_start;
10614 u64 block_len;
10615 u64 lowest_ppage;
10616 u64 highest_ppage;
10617 unsigned long nr_pages;
10618 int nr_extents;
10619};
10620
10621static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10622 struct btrfs_swap_info *bsi)
10623{
10624 unsigned long nr_pages;
10625 u64 first_ppage, first_ppage_reported, next_ppage;
10626 int ret;
10627
10628 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10629 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10630 PAGE_SIZE) >> PAGE_SHIFT;
10631
10632 if (first_ppage >= next_ppage)
10633 return 0;
10634 nr_pages = next_ppage - first_ppage;
10635
10636 first_ppage_reported = first_ppage;
10637 if (bsi->start == 0)
10638 first_ppage_reported++;
10639 if (bsi->lowest_ppage > first_ppage_reported)
10640 bsi->lowest_ppage = first_ppage_reported;
10641 if (bsi->highest_ppage < (next_ppage - 1))
10642 bsi->highest_ppage = next_ppage - 1;
10643
10644 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10645 if (ret < 0)
10646 return ret;
10647 bsi->nr_extents += ret;
10648 bsi->nr_pages += nr_pages;
10649 return 0;
10650}
10651
10652static void btrfs_swap_deactivate(struct file *file)
10653{
10654 struct inode *inode = file_inode(file);
10655
10656 btrfs_free_swapfile_pins(inode);
10657 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10658}
10659
10660static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10661 sector_t *span)
10662{
10663 struct inode *inode = file_inode(file);
10664 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10665 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10666 struct extent_state *cached_state = NULL;
10667 struct extent_map *em = NULL;
10668 struct btrfs_device *device = NULL;
10669 struct btrfs_swap_info bsi = {
10670 .lowest_ppage = (sector_t)-1ULL,
10671 };
10672 int ret = 0;
10673 u64 isize;
10674 u64 start;
10675
10676 /*
10677 * If the swap file was just created, make sure delalloc is done. If the
10678 * file changes again after this, the user is doing something stupid and
10679 * we don't really care.
10680 */
10681 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10682 if (ret)
10683 return ret;
10684
10685 /*
10686 * The inode is locked, so these flags won't change after we check them.
10687 */
10688 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10689 btrfs_warn(fs_info, "swapfile must not be compressed");
10690 return -EINVAL;
10691 }
10692 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10693 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10694 return -EINVAL;
10695 }
10696 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10697 btrfs_warn(fs_info, "swapfile must not be checksummed");
10698 return -EINVAL;
10699 }
10700
10701 /*
10702 * Balance or device remove/replace/resize can move stuff around from
10703 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10704 * concurrently while we are mapping the swap extents, and
10705 * fs_info->swapfile_pins prevents them from running while the swap file
10706 * is active and moving the extents. Note that this also prevents a
10707 * concurrent device add which isn't actually necessary, but it's not
10708 * really worth the trouble to allow it.
10709 */
10710 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10711 btrfs_warn(fs_info,
10712 "cannot activate swapfile while exclusive operation is running");
10713 return -EBUSY;
10714 }
10715 /*
10716 * Snapshots can create extents which require COW even if NODATACOW is
10717 * set. We use this counter to prevent snapshots. We must increment it
10718 * before walking the extents because we don't want a concurrent
10719 * snapshot to run after we've already checked the extents.
10720 */
10721 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10722
10723 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10724
10725 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10726 start = 0;
10727 while (start < isize) {
10728 u64 logical_block_start, physical_block_start;
10729 struct btrfs_block_group_cache *bg;
10730 u64 len = isize - start;
10731
10732 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10733 if (IS_ERR(em)) {
10734 ret = PTR_ERR(em);
10735 goto out;
10736 }
10737
10738 if (em->block_start == EXTENT_MAP_HOLE) {
10739 btrfs_warn(fs_info, "swapfile must not have holes");
10740 ret = -EINVAL;
10741 goto out;
10742 }
10743 if (em->block_start == EXTENT_MAP_INLINE) {
10744 /*
10745 * It's unlikely we'll ever actually find ourselves
10746 * here, as a file small enough to fit inline won't be
10747 * big enough to store more than the swap header, but in
10748 * case something changes in the future, let's catch it
10749 * here rather than later.
10750 */
10751 btrfs_warn(fs_info, "swapfile must not be inline");
10752 ret = -EINVAL;
10753 goto out;
10754 }
10755 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10756 btrfs_warn(fs_info, "swapfile must not be compressed");
10757 ret = -EINVAL;
10758 goto out;
10759 }
10760
10761 logical_block_start = em->block_start + (start - em->start);
10762 len = min(len, em->len - (start - em->start));
10763 free_extent_map(em);
10764 em = NULL;
10765
10766 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10767 if (ret < 0) {
10768 goto out;
10769 } else if (ret) {
10770 ret = 0;
10771 } else {
10772 btrfs_warn(fs_info,
10773 "swapfile must not be copy-on-write");
10774 ret = -EINVAL;
10775 goto out;
10776 }
10777
10778 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10779 if (IS_ERR(em)) {
10780 ret = PTR_ERR(em);
10781 goto out;
10782 }
10783
10784 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10785 btrfs_warn(fs_info,
10786 "swapfile must have single data profile");
10787 ret = -EINVAL;
10788 goto out;
10789 }
10790
10791 if (device == NULL) {
10792 device = em->map_lookup->stripes[0].dev;
10793 ret = btrfs_add_swapfile_pin(inode, device, false);
10794 if (ret == 1)
10795 ret = 0;
10796 else if (ret)
10797 goto out;
10798 } else if (device != em->map_lookup->stripes[0].dev) {
10799 btrfs_warn(fs_info, "swapfile must be on one device");
10800 ret = -EINVAL;
10801 goto out;
10802 }
10803
10804 physical_block_start = (em->map_lookup->stripes[0].physical +
10805 (logical_block_start - em->start));
10806 len = min(len, em->len - (logical_block_start - em->start));
10807 free_extent_map(em);
10808 em = NULL;
10809
10810 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10811 if (!bg) {
10812 btrfs_warn(fs_info,
10813 "could not find block group containing swapfile");
10814 ret = -EINVAL;
10815 goto out;
10816 }
10817
10818 ret = btrfs_add_swapfile_pin(inode, bg, true);
10819 if (ret) {
10820 btrfs_put_block_group(bg);
10821 if (ret == 1)
10822 ret = 0;
10823 else
10824 goto out;
10825 }
10826
10827 if (bsi.block_len &&
10828 bsi.block_start + bsi.block_len == physical_block_start) {
10829 bsi.block_len += len;
10830 } else {
10831 if (bsi.block_len) {
10832 ret = btrfs_add_swap_extent(sis, &bsi);
10833 if (ret)
10834 goto out;
10835 }
10836 bsi.start = start;
10837 bsi.block_start = physical_block_start;
10838 bsi.block_len = len;
10839 }
10840
10841 start += len;
10842 }
10843
10844 if (bsi.block_len)
10845 ret = btrfs_add_swap_extent(sis, &bsi);
10846
10847out:
10848 if (!IS_ERR_OR_NULL(em))
10849 free_extent_map(em);
10850
10851 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10852
10853 if (ret)
10854 btrfs_swap_deactivate(file);
10855
10856 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10857
10858 if (ret)
10859 return ret;
10860
10861 if (device)
10862 sis->bdev = device->bdev;
10863 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10864 sis->max = bsi.nr_pages;
10865 sis->pages = bsi.nr_pages - 1;
10866 sis->highest_bit = bsi.nr_pages - 1;
10867 return bsi.nr_extents;
10868}
10869#else
10870static void btrfs_swap_deactivate(struct file *file)
10871{
10872}
10873
10874static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10875 sector_t *span)
10876{
10877 return -EOPNOTSUPP;
10878}
10879#endif
10880
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010881static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010882 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010883 .lookup = btrfs_lookup,
10884 .create = btrfs_create,
10885 .unlink = btrfs_unlink,
10886 .link = btrfs_link,
10887 .mkdir = btrfs_mkdir,
10888 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010889 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010890 .symlink = btrfs_symlink,
10891 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010892 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010893 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010894 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010895 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010896 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010897 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010898 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010899};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010900static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010901 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010902 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010903 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010904};
Yan, Zheng76dda932009-09-21 16:00:26 -040010905
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010906static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010907 .llseek = generic_file_llseek,
10908 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010909 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010910 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010911 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010912#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010913 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010914#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010915 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010916 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010917};
10918
David Sterba20e55062015-11-19 11:42:28 +010010919static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010920 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010921 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010922 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10923};
10924
Chris Mason35054392009-01-21 13:11:13 -050010925/*
10926 * btrfs doesn't support the bmap operation because swapfiles
10927 * use bmap to make a mapping of extents in the file. They assume
10928 * these extents won't change over the life of the file and they
10929 * use the bmap result to do IO directly to the drive.
10930 *
10931 * the btrfs bmap call would return logical addresses that aren't
10932 * suitable for IO and they also will change frequently as COW
10933 * operations happen. So, swapfile + btrfs == corruption.
10934 *
10935 * For now we're avoiding this by dropping bmap.
10936 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010937static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010938 .readpage = btrfs_readpage,
10939 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010940 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010941 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010942 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010943 .invalidatepage = btrfs_invalidatepage,
10944 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010945 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010946 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010947 .swap_activate = btrfs_swap_activate,
10948 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010949};
10950
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010951static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010952 .getattr = btrfs_getattr,
10953 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010954 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010955 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010956 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010957 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010958 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010959 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010960};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010961static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010962 .getattr = btrfs_getattr,
10963 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010964 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010965 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010966 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010967 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010968 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010969};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010970static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010971 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010972 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010973 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010974 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010975 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010976 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010977};
Yan, Zheng76dda932009-09-21 16:00:26 -040010978
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010979const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010980 .d_delete = btrfs_dentry_delete,
10981};