blob: 2973608824ecacbfaad11a6f0d57460d113a5e47 [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>
David Sterba92d32172018-04-16 21:10:14 +020031#include <asm/unaligned.h>
Chris Mason39279cc2007-06-12 06:35:45 -040032#include "ctree.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040037#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040038#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040039#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020040#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040041#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050042#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050043#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080044#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000045#include "backref.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000046#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080047#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080048#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049
50struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080051 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040052 struct btrfs_root *root;
53};
54
Filipe Mananaf28a4922015-12-08 19:23:20 +000055struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000056 u64 reserve;
57 u64 unsubmitted_oe_range_start;
58 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080059 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000060};
61
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070062static const struct inode_operations btrfs_dir_inode_operations;
63static const struct inode_operations btrfs_symlink_inode_operations;
64static const struct inode_operations btrfs_dir_ro_inode_operations;
65static const struct inode_operations btrfs_special_inode_operations;
66static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070067static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070068static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010069static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040070
71static struct kmem_cache *btrfs_inode_cachep;
72struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040073struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050074struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075
76#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010077static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040078 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
79 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
80 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
81 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
82 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
83 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
85};
86
Eric Sandeen3972f262013-01-12 02:57:22 +000087static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000088static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040089static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050090static noinline int cow_file_range(struct inode *inode,
91 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +080092 u64 start, u64 end, u64 delalloc_end,
93 int *page_started, unsigned long *nr_written,
94 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -080095static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
96 u64 orig_start, u64 block_start,
97 u64 block_len, u64 orig_block_len,
98 u64 ram_bytes, int compress_type,
99 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400100
Qu Wenruo524272602017-03-08 10:25:52 +0800101static void __endio_write_update_ordered(struct inode *inode,
102 const u64 offset, const u64 bytes,
103 const bool uptodate);
104
105/*
106 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100107 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800108 *
109 * NOTE: caller must ensure that when an error happens, it can not call
110 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
111 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
112 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200113 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800114 */
115static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200116 struct page *locked_page,
117 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800118{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900119 unsigned long index = offset >> PAGE_SHIFT;
120 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200121 u64 page_start = page_offset(locked_page);
122 u64 page_end = page_start + PAGE_SIZE - 1;
123
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900124 struct page *page;
125
126 while (index <= end_index) {
127 page = find_get_page(inode->i_mapping, index);
128 index++;
129 if (!page)
130 continue;
131 ClearPagePrivate2(page);
132 put_page(page);
133 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200134
135 /*
136 * In case this page belongs to the delalloc range being instantiated
137 * then skip it, since the first page of a range is going to be
138 * properly cleaned up by the caller of run_delalloc_range
139 */
140 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
141 offset += PAGE_SIZE;
142 bytes -= PAGE_SIZE;
143 }
144
145 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800146}
147
Eric Sandeen48a3b632013-04-25 20:41:01 +0000148static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400149
Josef Bacik6a3891c2015-03-16 17:38:52 -0400150#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
151void btrfs_test_inode_set_ops(struct inode *inode)
152{
153 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
154}
155#endif
156
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000157static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500158 struct inode *inode, struct inode *dir,
159 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500160{
161 int err;
162
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000163 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500164 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500165 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500166 return err;
167}
168
Chris Masond352ac62008-09-29 15:18:18 -0400169/*
Chris Masonc8b97812008-10-29 14:49:59 -0400170 * this does all the hard work for inserting an inline extent into
171 * the btree. The caller should have done a btrfs_drop_extents so that
172 * no overlapping inline items exist in the btree
173 */
Chris Mason40f76582014-05-21 13:35:51 -0700174static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000175 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400176 struct btrfs_root *root, struct inode *inode,
177 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000178 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400179 struct page **compressed_pages)
180{
Chris Masonc8b97812008-10-29 14:49:59 -0400181 struct extent_buffer *leaf;
182 struct page *page = NULL;
183 char *kaddr;
184 unsigned long ptr;
185 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400186 int ret;
187 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400188 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400189
Li Zefanfe3f5662011-03-28 08:30:38 +0000190 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400191 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400192
Chris Masonc8b97812008-10-29 14:49:59 -0400193 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000194
195 if (!extent_inserted) {
196 struct btrfs_key key;
197 size_t datasize;
198
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200199 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000200 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200201 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000202
203 datasize = btrfs_file_extent_calc_inline_size(cur_size);
204 path->leave_spinning = 1;
205 ret = btrfs_insert_empty_item(trans, root, path, &key,
206 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200207 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000208 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400209 }
210 leaf = path->nodes[0];
211 ei = btrfs_item_ptr(leaf, path->slots[0],
212 struct btrfs_file_extent_item);
213 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
214 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
215 btrfs_set_file_extent_encryption(leaf, ei, 0);
216 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
217 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
218 ptr = btrfs_file_extent_inline_start(ei);
219
Li Zefan261507a02010-12-17 14:21:50 +0800220 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400221 struct page *cpage;
222 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500223 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400224 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500225 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300226 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400227
Cong Wang7ac687d2011-11-25 23:14:28 +0800228 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400229 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800230 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400231
232 i++;
233 ptr += cur_size;
234 compressed_size -= cur_size;
235 }
236 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800237 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 } else {
239 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300240 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400241 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800242 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100243 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800245 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300246 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 }
248 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000249 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400250
Yan, Zhengc2167752009-11-12 09:34:21 +0000251 /*
252 * we're an inline extent, so nobody can
253 * extend the file past i_size without locking
254 * a page we already have locked.
255 *
256 * We must do any isize and inode updates
257 * before we unlock the pages. Otherwise we
258 * could end up racing with unlink.
259 */
Chris Masonc8b97812008-10-29 14:49:59 -0400260 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100261 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000262
Chris Masonc8b97812008-10-29 14:49:59 -0400263fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200264 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400265}
266
267
268/*
269 * conditionally insert an inline extent into the file. This
270 * does the checks required to make sure the data is small enough
271 * to fit as an inline extent.
272 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200273static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400274 u64 end, size_t compressed_size,
275 int compress_type,
276 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400277{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200278 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400279 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400280 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400281 u64 isize = i_size_read(inode);
282 u64 actual_end = min(end + 1, isize);
283 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400284 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400285 u64 data_len = inline_len;
286 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000287 struct btrfs_path *path;
288 int extent_inserted = 0;
289 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400290
291 if (compressed_size)
292 data_len = compressed_size;
293
294 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400295 actual_end > fs_info->sectorsize ||
296 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400297 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400298 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400299 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400300 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400301 return 1;
302 }
303
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000304 path = btrfs_alloc_path();
305 if (!path)
306 return -ENOMEM;
307
Josef Bacik00361582013-08-14 14:02:47 -0400308 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000309 if (IS_ERR(trans)) {
310 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400311 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000312 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400313 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400314
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (compressed_size && compressed_pages)
316 extent_item_size = btrfs_file_extent_calc_inline_size(
317 compressed_size);
318 else
319 extent_item_size = btrfs_file_extent_calc_inline_size(
320 inline_len);
321
322 ret = __btrfs_drop_extents(trans, root, inode, path,
323 start, aligned_end, NULL,
324 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400325 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400326 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400327 goto out;
328 }
Chris Masonc8b97812008-10-29 14:49:59 -0400329
330 if (isize > actual_end)
331 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000332 ret = insert_inline_extent(trans, path, extent_inserted,
333 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400334 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000335 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400336 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400337 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400338 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400339 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400340 ret = 1;
341 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100342 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400343
Josef Bacikbdc20e62013-02-28 13:23:38 -0500344 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200345 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400346out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800347 /*
348 * Don't forget to free the reserved space, as for inlined extent
349 * it won't count as data extent, free them directly here.
350 * And at reserve time, it's always aligned to page size, so
351 * just free one page here.
352 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800353 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000354 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400355 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400356 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400357}
358
Chris Mason771ed682008-11-06 22:02:51 -0500359struct async_extent {
360 u64 start;
361 u64 ram_size;
362 u64 compressed_size;
363 struct page **pages;
364 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800365 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500366 struct list_head list;
367};
368
369struct async_cow {
370 struct inode *inode;
David Sterba600b6cf2018-11-22 17:16:44 +0100371 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct page *locked_page;
373 u64 start;
374 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600375 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500376 struct list_head extents;
377 struct btrfs_work work;
378};
379
380static noinline int add_async_extent(struct async_cow *cow,
381 u64 start, u64 ram_size,
382 u64 compressed_size,
383 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800384 unsigned long nr_pages,
385 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500386{
387 struct async_extent *async_extent;
388
389 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100390 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500391 async_extent->start = start;
392 async_extent->ram_size = ram_size;
393 async_extent->compressed_size = compressed_size;
394 async_extent->pages = pages;
395 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800396 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500397 list_add_tail(&async_extent->list, &cow->extents);
398 return 0;
399}
400
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300401static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800402{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400403 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800404
405 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400406 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800407 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200408 /* defrag ioctl */
409 if (BTRFS_I(inode)->defrag_compress)
410 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800411 /* bad compression ratios */
412 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
413 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400414 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800415 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200416 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300417 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800418 return 0;
419}
420
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200421static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800422 u64 start, u64 end, u64 num_bytes, u64 small_write)
423{
424 /* If this is a small write inside eof, kick off a defrag */
425 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200426 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800427 btrfs_add_inode_defrag(NULL, inode);
428}
429
Chris Masonc8b97812008-10-29 14:49:59 -0400430/*
Chris Mason771ed682008-11-06 22:02:51 -0500431 * we create compressed extents in two phases. The first
432 * phase compresses a range of pages that have already been
433 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400434 *
Chris Mason771ed682008-11-06 22:02:51 -0500435 * This is done inside an ordered work queue, and the compression
436 * is spread across many cpus. The actual IO submission is step
437 * two, and the ordered work queue takes care of making sure that
438 * happens in the same order things were put onto the queue by
439 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400440 *
Chris Mason771ed682008-11-06 22:02:51 -0500441 * If this code finds it can't get good compression, it puts an
442 * entry onto the work queue to write the uncompressed bytes. This
443 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300444 * are written in the same order that the flusher thread sent them
445 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400446 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100447static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500448 struct page *locked_page,
449 u64 start, u64 end,
450 struct async_cow *async_cow,
451 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400452{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400453 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400454 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400455 u64 actual_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400456 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400457 struct page **pages = NULL;
458 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400459 unsigned long total_compressed = 0;
460 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400461 int i;
462 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400463 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400464 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400465
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200466 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
467 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400468
Nikolay Borisov62b37622019-01-03 10:50:00 +0200469 actual_end = min_t(u64, i_size_read(inode), end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400470again:
471 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300472 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100473 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
474 nr_pages = min_t(unsigned long, nr_pages,
475 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400476
Chris Masonf03d9301f2009-02-04 09:31:06 -0500477 /*
478 * we don't want to send crud past the end of i_size through
479 * compression, that's just a waste of CPU time. So, if the
480 * end of the file is before the start of our current
481 * requested range of bytes, we bail out to the uncompressed
482 * cleanup code that can deal with all of this.
483 *
484 * It isn't really the fastest way to fix things, but this is a
485 * very uncommon corner.
486 */
487 if (actual_end <= start)
488 goto cleanup_and_bail_uncompressed;
489
Chris Masonc8b97812008-10-29 14:49:59 -0400490 total_compressed = actual_end - start;
491
Shilong Wang4bcbb332014-10-07 18:44:35 -0400492 /*
493 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400494 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400495 */
496 if (total_compressed <= blocksize &&
497 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
498 goto cleanup_and_bail_uncompressed;
499
David Sterba069eac72017-02-14 19:36:54 +0100500 total_compressed = min_t(unsigned long, total_compressed,
501 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400502 total_in = 0;
503 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400504
Chris Mason771ed682008-11-06 22:02:51 -0500505 /*
506 * we do compression for mount -o compress and when the
507 * inode has not been flagged as nocompress. This flag can
508 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400509 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300510 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400511 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100512 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800513 if (!pages) {
514 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100515 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800516 goto cont;
517 }
Chris Mason179e29e2007-11-01 11:28:41 -0400518
David Sterbaeec63c62017-07-17 19:41:31 +0200519 if (BTRFS_I(inode)->defrag_compress)
520 compress_type = BTRFS_I(inode)->defrag_compress;
521 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200522 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800523
Chris Mason4adaa612013-03-26 13:07:00 -0400524 /*
525 * we need to call clear_page_dirty_for_io on each
526 * page in the range. Otherwise applications with the file
527 * mmap'd can wander in and change the page contents while
528 * we are compressing them.
529 *
530 * If the compression fails for any reason, we set the pages
531 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300532 *
533 * Note that the remaining part is redirtied, the start pointer
534 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400535 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300536 if (!redirty) {
537 extent_range_clear_dirty_for_io(inode, start, end);
538 redirty = 1;
539 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200540
541 /* Compression level is applied here and only here */
542 ret = btrfs_compress_pages(
543 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800544 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100545 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100546 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800547 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100548 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400549
550 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100551 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100552 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400553 char *kaddr;
554
555 /* zero the tail end of the last page, we might be
556 * sending it down to disk
557 */
558 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800559 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400560 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300561 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800562 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400563 }
564 will_compress = 1;
565 }
566 }
Li Zefan560f7d72011-09-08 10:22:01 +0800567cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400568 if (start == 0) {
569 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300570 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400571 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500572 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400573 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200574 ret = cow_file_range_inline(inode, start, end, 0,
575 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400576 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500577 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200578 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000579 total_compressed,
580 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400581 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100582 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400583 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400584 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
585 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100586 unsigned long page_error_op;
587
Filipe Mananae6eb4312014-10-10 10:45:12 +0100588 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400589
Chris Mason771ed682008-11-06 22:02:51 -0500590 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100591 * inline extent creation worked or returned error,
592 * we don't need to create any more async work items.
593 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400594 *
595 * We use DO_ACCOUNTING here because we need the
596 * delalloc_release_metadata to be done _after_ we drop
597 * our outstanding extent for clearing delalloc for this
598 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500599 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800600 extent_clear_unlock_delalloc(inode, start, end, end,
601 NULL, clear_flags,
602 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400603 PAGE_CLEAR_DIRTY |
604 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100605 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400606 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400607 goto free_pages_out;
608 }
609 }
610
611 if (will_compress) {
612 /*
613 * we aren't doing an inline extent round the compressed size
614 * up to a block size boundary so the allocator does sane
615 * things
616 */
Qu Wenruofda28322013-02-26 08:10:22 +0000617 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400618
619 /*
620 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300621 * win, compare the page count read with the blocks on disk,
622 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400623 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300624 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300625 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700626 *num_added += 1;
627
628 /*
629 * The async work queues will take care of doing actual
630 * allocation on disk for these compressed pages, and
631 * will submit them to the elevator.
632 */
Timofey Titovets11708622017-10-03 18:06:01 +0300633 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100634 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700635 compress_type);
636
Timofey Titovets11708622017-10-03 18:06:01 +0300637 if (start + total_in < end) {
638 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700639 pages = NULL;
640 cond_resched();
641 goto again;
642 }
643 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400644 }
645 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700646 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400647 /*
648 * the compression code ran but failed to make things smaller,
649 * free any pages it allocated and our page pointer array
650 */
David Sterba4d3a8002017-02-14 19:04:07 +0100651 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400652 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300653 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400654 }
655 kfree(pages);
656 pages = NULL;
657 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100658 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400659
660 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400661 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200662 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500663 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500664 }
Chris Masonc8b97812008-10-29 14:49:59 -0400665 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500666cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700667 /*
668 * No compression, but we still need to write the pages in the file
669 * we've been given so far. redirty the locked page if it corresponds
670 * to our extent and set things up for the async work queue to run
671 * cow_file_range to do the normal delalloc dance.
672 */
673 if (page_offset(locked_page) >= start &&
674 page_offset(locked_page) <= end)
675 __set_page_dirty_nobuffers(locked_page);
676 /* unlocked later on in the async handlers */
677
678 if (redirty)
679 extent_range_redirty_for_io(inode, start, end);
680 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
681 BTRFS_COMPRESS_NONE);
682 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500683
Filipe Mananac44f6492014-10-09 21:15:44 +0100684 return;
Chris Mason771ed682008-11-06 22:02:51 -0500685
686free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100687 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500688 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300689 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500690 }
Chris Masond3977122009-01-05 21:25:51 -0500691 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500692}
Chris Mason771ed682008-11-06 22:02:51 -0500693
Filipe Manana40ae8372014-10-06 22:14:24 +0100694static void free_async_extent_pages(struct async_extent *async_extent)
695{
696 int i;
697
698 if (!async_extent->pages)
699 return;
700
701 for (i = 0; i < async_extent->nr_pages; i++) {
702 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300703 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100704 }
705 kfree(async_extent->pages);
706 async_extent->nr_pages = 0;
707 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500708}
709
710/*
711 * phase two of compressed writeback. This is the ordered portion
712 * of the code, which only gets called in the order the work was
713 * queued. We walk all the async extents created by compress_file_range
714 * and send them down to the disk.
715 */
Nikolay Borisov532425f2019-01-03 10:49:59 +0200716static noinline void submit_compressed_extents(struct async_cow *async_cow)
Chris Mason771ed682008-11-06 22:02:51 -0500717{
Nikolay Borisov532425f2019-01-03 10:49:59 +0200718 struct inode *inode = async_cow->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400719 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500720 struct async_extent *async_extent;
721 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500722 struct btrfs_key ins;
723 struct extent_map *em;
724 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500725 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500726 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500727
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500728again:
Chris Masond3977122009-01-05 21:25:51 -0500729 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500730 async_extent = list_entry(async_cow->extents.next,
731 struct async_extent, list);
732 list_del(&async_extent->list);
733
734 io_tree = &BTRFS_I(inode)->io_tree;
735
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500736retry:
Chris Mason771ed682008-11-06 22:02:51 -0500737 /* did the compression code fall back to uncompressed IO? */
738 if (!async_extent->pages) {
739 int page_started = 0;
740 unsigned long nr_written = 0;
741
742 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000743 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100744 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500745
746 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500747 ret = cow_file_range(inode, async_cow->locked_page,
748 async_extent->start,
749 async_extent->start +
750 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800751 async_extent->start +
752 async_extent->ram_size - 1,
753 &page_started, &nr_written, 0,
754 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500755
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100756 /* JDM XXX */
757
Chris Mason771ed682008-11-06 22:02:51 -0500758 /*
759 * if page_started, cow_file_range inserted an
760 * inline extent and took care of all the unlocking
761 * and IO for us. Otherwise, we need to submit
762 * all those pages down to the drive.
763 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500764 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200765 extent_write_locked_range(inode,
766 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500767 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500768 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500769 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500770 else if (ret)
771 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500772 kfree(async_extent);
773 cond_resched();
774 continue;
775 }
776
777 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100778 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500779
Wang Xiaoguang18513092016-07-25 15:51:40 +0800780 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500781 async_extent->compressed_size,
782 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800783 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500784 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100785 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500786
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400787 if (ret == -ENOSPC) {
788 unlock_extent(io_tree, async_extent->start,
789 async_extent->start +
790 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800791
792 /*
793 * we need to redirty the pages if we decide to
794 * fallback to uncompressed IO, otherwise we
795 * will not submit these pages down to lower
796 * layers.
797 */
798 extent_range_redirty_for_io(inode,
799 async_extent->start,
800 async_extent->start +
801 async_extent->ram_size - 1);
802
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100803 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400804 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500805 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500806 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000807 /*
808 * here we're doing allocation and writeback of the
809 * compressed pages
810 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800811 em = create_io_em(inode, async_extent->start,
812 async_extent->ram_size, /* len */
813 async_extent->start, /* orig_start */
814 ins.objectid, /* block_start */
815 ins.offset, /* block_len */
816 ins.offset, /* orig_block_len */
817 async_extent->ram_size, /* ram_bytes */
818 async_extent->compress_type,
819 BTRFS_ORDERED_COMPRESSED);
820 if (IS_ERR(em))
821 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500822 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800823 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500824
Li Zefan261507a02010-12-17 14:21:50 +0800825 ret = btrfs_add_ordered_extent_compress(inode,
826 async_extent->start,
827 ins.objectid,
828 async_extent->ram_size,
829 ins.offset,
830 BTRFS_ORDERED_COMPRESSED,
831 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100832 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200833 btrfs_drop_extent_cache(BTRFS_I(inode),
834 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100835 async_extent->start +
836 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500837 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100838 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400839 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500840
Chris Mason771ed682008-11-06 22:02:51 -0500841 /*
842 * clear dirty, set writeback and unlock the pages.
843 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400844 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400845 async_extent->start +
846 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800847 async_extent->start +
848 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400849 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
850 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400851 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200852 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500853 async_extent->start,
854 async_extent->ram_size,
855 ins.objectid,
856 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600857 async_extent->nr_pages,
858 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100859 struct page *p = async_extent->pages[0];
860 const u64 start = async_extent->start;
861 const u64 end = start + async_extent->ram_size - 1;
862
863 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200864 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200865
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100866 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800867 extent_clear_unlock_delalloc(inode, start, end, end,
868 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100869 PAGE_END_WRITEBACK |
870 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100871 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100872 }
Chris Mason771ed682008-11-06 22:02:51 -0500873 alloc_hint = ins.objectid + ins.offset;
874 kfree(async_extent);
875 cond_resched();
876 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100877 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500878out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400879 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400880 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100881out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400882 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500883 async_extent->start +
884 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800885 async_extent->start +
886 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400887 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100888 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400889 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
890 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100891 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
892 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100893 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100894 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500895 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500896}
897
Josef Bacik4b46fce2010-05-23 11:00:55 -0400898static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
899 u64 num_bytes)
900{
901 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
902 struct extent_map *em;
903 u64 alloc_hint = 0;
904
905 read_lock(&em_tree->lock);
906 em = search_extent_mapping(em_tree, start, num_bytes);
907 if (em) {
908 /*
909 * if block start isn't an actual block number then find the
910 * first block in this inode and use that as a hint. If that
911 * block is also bogus then just don't worry about it.
912 */
913 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
914 free_extent_map(em);
915 em = search_extent_mapping(em_tree, 0, 0);
916 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
917 alloc_hint = em->block_start;
918 if (em)
919 free_extent_map(em);
920 } else {
921 alloc_hint = em->block_start;
922 free_extent_map(em);
923 }
924 }
925 read_unlock(&em_tree->lock);
926
927 return alloc_hint;
928}
929
Chris Mason771ed682008-11-06 22:02:51 -0500930/*
931 * when extent_io.c finds a delayed allocation range in the file,
932 * the call backs end up in this code. The basic idea is to
933 * allocate extents on disk for the range, and create ordered data structs
934 * in ram to track those extents.
935 *
936 * locked_page is the page that writepage had locked already. We use
937 * it to make sure we don't do extra locks or unlocks.
938 *
939 * *page_started is set to one if we unlock locked_page and do everything
940 * required to start IO on it. It may be clean and already done with
941 * IO when we return.
942 */
Josef Bacik00361582013-08-14 14:02:47 -0400943static noinline int cow_file_range(struct inode *inode,
944 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800945 u64 start, u64 end, u64 delalloc_end,
946 int *page_started, unsigned long *nr_written,
947 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500948{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400949 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400950 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500951 u64 alloc_hint = 0;
952 u64 num_bytes;
953 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000954 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400955 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500956 struct btrfs_key ins;
957 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000958 unsigned clear_bits;
959 unsigned long page_ops;
960 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500961 int ret = 0;
962
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200963 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400964 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500965 ret = -EINVAL;
966 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400967 }
Chris Mason771ed682008-11-06 22:02:51 -0500968
Qu Wenruofda28322013-02-26 08:10:22 +0000969 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500970 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800971 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500972
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200973 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400974
Chris Mason771ed682008-11-06 22:02:51 -0500975 if (start == 0) {
976 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200977 ret = cow_file_range_inline(inode, start, end, 0,
978 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500979 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400980 /*
981 * We use DO_ACCOUNTING here because we need the
982 * delalloc_release_metadata to be run _after_ we drop
983 * our outstanding extent for clearing delalloc for this
984 * range.
985 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800986 extent_clear_unlock_delalloc(inode, start, end,
987 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400988 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400989 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
990 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400991 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
992 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500993 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300994 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500995 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500996 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100997 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100998 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500999 }
1000 }
Chris Masonc8b97812008-10-29 14:49:59 -04001001
Josef Bacik4b46fce2010-05-23 11:00:55 -04001002 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001003 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1004 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001005
Anand Jain3752d222018-02-15 12:29:38 +08001006 while (num_bytes > 0) {
1007 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001008 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001009 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001010 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001011 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001012 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001013 cur_alloc_size = ins.offset;
1014 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001015
Chris Mason771ed682008-11-06 22:02:51 -05001016 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001017 em = create_io_em(inode, start, ins.offset, /* len */
1018 start, /* orig_start */
1019 ins.objectid, /* block_start */
1020 ins.offset, /* block_len */
1021 ins.offset, /* orig_block_len */
1022 ram_size, /* ram_bytes */
1023 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001024 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001025 if (IS_ERR(em)) {
1026 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001027 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001028 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001029 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001030
Chris Masone6dcd2d2008-07-17 12:53:50 -04001031 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001032 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001033 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001034 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001035
Yan Zheng17d217f2008-12-12 10:03:38 -05001036 if (root->root_key.objectid ==
1037 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1038 ret = btrfs_reloc_clone_csums(inode, start,
1039 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001040 /*
1041 * Only drop cache here, and process as normal.
1042 *
1043 * We must not allow extent_clear_unlock_delalloc()
1044 * at out_unlock label to free meta of this ordered
1045 * extent, as its meta should be freed by
1046 * btrfs_finish_ordered_io().
1047 *
1048 * So we must continue until @start is increased to
1049 * skip current ordered extent.
1050 */
Josef Bacik00361582013-08-14 14:02:47 -04001051 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001052 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1053 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001054 }
1055
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001056 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001057
Chris Masonc8b97812008-10-29 14:49:59 -04001058 /* we're not doing compressed IO, don't unlock the first
1059 * page (which the caller expects to stay locked), don't
1060 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001061 *
1062 * Do set the Private2 bit so we know this page was properly
1063 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001064 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001065 page_ops = unlock ? PAGE_UNLOCK : 0;
1066 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001067
Josef Bacikc2790a22013-07-29 11:20:47 -04001068 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001069 start + ram_size - 1,
1070 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001071 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001072 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001073 if (num_bytes < cur_alloc_size)
1074 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001075 else
Anand Jain3752d222018-02-15 12:29:38 +08001076 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001077 alloc_hint = ins.objectid + ins.offset;
1078 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001079 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001080
1081 /*
1082 * btrfs_reloc_clone_csums() error, since start is increased
1083 * extent_clear_unlock_delalloc() at out_unlock label won't
1084 * free metadata of current ordered extent, we're OK to exit.
1085 */
1086 if (ret)
1087 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001088 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001089out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001090 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001091
Filipe Mananad9f85962014-08-25 10:43:00 +01001092out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001093 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001094out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001095 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001096 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001097out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001098 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1099 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001100 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1101 PAGE_END_WRITEBACK;
1102 /*
1103 * If we reserved an extent for our delalloc range (or a subrange) and
1104 * failed to create the respective ordered extent, then it means that
1105 * when we reserved the extent we decremented the extent's size from
1106 * the data space_info's bytes_may_use counter and incremented the
1107 * space_info's bytes_reserved counter by the same amount. We must make
1108 * sure extent_clear_unlock_delalloc() does not try to decrement again
1109 * the data space_info's bytes_may_use counter, therefore we do not pass
1110 * it the flag EXTENT_CLEAR_DATA_RESV.
1111 */
1112 if (extent_reserved) {
1113 extent_clear_unlock_delalloc(inode, start,
1114 start + cur_alloc_size,
1115 start + cur_alloc_size,
1116 locked_page,
1117 clear_bits,
1118 page_ops);
1119 start += cur_alloc_size;
1120 if (start >= end)
1121 goto out;
1122 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001123 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1124 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001125 clear_bits | EXTENT_CLEAR_DATA_RESV,
1126 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001127 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001128}
Chris Masonc8b97812008-10-29 14:49:59 -04001129
Chris Mason771ed682008-11-06 22:02:51 -05001130/*
1131 * work queue call back to started compression on a file and pages
1132 */
1133static noinline void async_cow_start(struct btrfs_work *work)
1134{
1135 struct async_cow *async_cow;
1136 int num_added = 0;
1137 async_cow = container_of(work, struct async_cow, work);
1138
1139 compress_file_range(async_cow->inode, async_cow->locked_page,
1140 async_cow->start, async_cow->end, async_cow,
1141 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001142 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001143 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001144 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001145 }
Chris Mason771ed682008-11-06 22:02:51 -05001146}
1147
1148/*
1149 * work queue call back to submit previously compressed pages
1150 */
1151static noinline void async_cow_submit(struct btrfs_work *work)
1152{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001153 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001154 struct async_cow *async_cow;
Chris Mason771ed682008-11-06 22:02:51 -05001155 unsigned long nr_pages;
1156
1157 async_cow = container_of(work, struct async_cow, work);
1158
David Sterba600b6cf2018-11-22 17:16:44 +01001159 fs_info = async_cow->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001160 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1161 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001162
David Sterba093258e2018-02-26 16:15:17 +01001163 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001164 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001165 5 * SZ_1M)
1166 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001167
Nikolay Borisov4546d172019-01-03 10:50:03 +02001168 /*
1169 * ->inode could be NULL if async_cow_start has failed to compress,
1170 * in which case we don't have anything to submit, yet we need to
1171 * always adjust ->async_delalloc_pages as its paired with the init
1172 * happening in cow_file_range_async
1173 */
Chris Masond3977122009-01-05 21:25:51 -05001174 if (async_cow->inode)
Nikolay Borisov532425f2019-01-03 10:49:59 +02001175 submit_compressed_extents(async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001176}
Chris Masonc8b97812008-10-29 14:49:59 -04001177
Chris Mason771ed682008-11-06 22:02:51 -05001178static noinline void async_cow_free(struct btrfs_work *work)
1179{
1180 struct async_cow *async_cow;
1181 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001182 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001183 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001184 kfree(async_cow);
1185}
1186
1187static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1188 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001189 unsigned long *nr_written,
1190 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001191{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001192 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001193 struct async_cow *async_cow;
Chris Mason771ed682008-11-06 22:02:51 -05001194 unsigned long nr_pages;
1195 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001196
Chris Masona3429ab2009-10-08 12:30:20 -04001197 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001198 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001199 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001200 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001201 BUG_ON(!async_cow); /* -ENOMEM */
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001202 /*
1203 * igrab is called higher up in the call chain, take only the
1204 * lightweight reference for the callback lifetime
1205 */
1206 ihold(inode);
1207 async_cow->inode = inode;
David Sterba600b6cf2018-11-22 17:16:44 +01001208 async_cow->fs_info = fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001209 async_cow->locked_page = locked_page;
1210 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001211 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001212
Wang Shilongf79707b2014-07-17 11:44:09 +08001213 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001214 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001215 cur_end = end;
1216 else
Byongho Leeee221842015-12-15 01:42:10 +09001217 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001218
1219 async_cow->end = cur_end;
1220 INIT_LIST_HEAD(&async_cow->extents);
1221
Liu Bo9e0af232014-08-15 23:36:53 +08001222 btrfs_init_work(&async_cow->work,
1223 btrfs_delalloc_helper,
1224 async_cow_start, async_cow_submit,
1225 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001226
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001227 nr_pages = (cur_end - start + PAGE_SIZE) >>
1228 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001229 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001230
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001231 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001232
Chris Mason771ed682008-11-06 22:02:51 -05001233 *nr_written += nr_pages;
1234 start = cur_end + 1;
1235 }
1236 *page_started = 1;
1237 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001238}
1239
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001240static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001241 u64 bytenr, u64 num_bytes)
1242{
1243 int ret;
1244 struct btrfs_ordered_sum *sums;
1245 LIST_HEAD(list);
1246
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001247 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001248 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001249 if (ret == 0 && list_empty(&list))
1250 return 0;
1251
1252 while (!list_empty(&list)) {
1253 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1254 list_del(&sums->list);
1255 kfree(sums);
1256 }
Liu Bo58113752018-01-31 17:09:13 -07001257 if (ret < 0)
1258 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001259 return 1;
1260}
1261
Chris Masond352ac62008-09-29 15:18:18 -04001262/*
1263 * when nowcow writeback call back. This checks for snapshots or COW copies
1264 * of the extents that exist in the file, and COWs the file as required.
1265 *
1266 * If no cow copies or snapshots exist, we write directly to the existing
1267 * blocks on disk
1268 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001269static noinline int run_delalloc_nocow(struct inode *inode,
1270 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001271 u64 start, u64 end, int *page_started, int force,
1272 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001273{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001274 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001275 struct btrfs_root *root = BTRFS_I(inode)->root;
1276 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001277 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001279 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001280 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 u64 cow_start;
1282 u64 cur_offset;
1283 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001284 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 u64 disk_bytenr;
1286 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001287 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001288 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 int extent_type;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001290 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001291 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 int nocow;
1293 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001294 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001295 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001296
1297 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001298 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001299 extent_clear_unlock_delalloc(inode, start, end, end,
1300 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001301 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001302 EXTENT_DO_ACCOUNTING |
1303 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001304 PAGE_CLEAR_DIRTY |
1305 PAGE_SET_WRITEBACK |
1306 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001307 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001308 }
Li Zefan82d59022011-04-20 10:33:24 +08001309
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001310 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001311
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 cow_start = (u64)-1;
1313 cur_offset = start;
1314 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001315 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001317 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001318 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1320 leaf = path->nodes[0];
1321 btrfs_item_key_to_cpu(leaf, &found_key,
1322 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001323 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 found_key.type == BTRFS_EXTENT_DATA_KEY)
1325 path->slots[0]--;
1326 }
1327 check_prev = 0;
1328next_slot:
1329 leaf = path->nodes[0];
1330 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1331 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001332 if (ret < 0) {
1333 if (cow_start != (u64)-1)
1334 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001335 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001336 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001337 if (ret > 0)
1338 break;
1339 leaf = path->nodes[0];
1340 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001341
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 nocow = 0;
1343 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001344 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001346
Filipe Manana1d512cb2015-11-09 00:33:58 +00001347 if (found_key.objectid > ino)
1348 break;
1349 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1350 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1351 path->slots[0]++;
1352 goto next_slot;
1353 }
1354 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 found_key.offset > end)
1356 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001357
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 if (found_key.offset > cur_offset) {
1359 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001360 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 goto out_check;
1362 }
Chris Masonc31f8832008-01-08 15:46:31 -05001363
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 fi = btrfs_item_ptr(leaf, path->slots[0],
1365 struct btrfs_file_extent_item);
1366 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001367
Josef Bacikcc95bef2013-04-04 14:31:27 -04001368 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001369 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1370 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001372 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 extent_end = found_key.offset +
1374 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001375 disk_num_bytes =
1376 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 if (extent_end <= start) {
1378 path->slots[0]++;
1379 goto next_slot;
1380 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001381 if (disk_bytenr == 0)
1382 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001383 if (btrfs_file_extent_compression(leaf, fi) ||
1384 btrfs_file_extent_encryption(leaf, fi) ||
1385 btrfs_file_extent_other_encoding(leaf, fi))
1386 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001387 /*
1388 * Do the same check as in btrfs_cross_ref_exist but
1389 * without the unnecessary search.
1390 */
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001391 if (!nolock &&
1392 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001393 btrfs_root_last_snapshot(&root->root_item))
1394 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001395 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1396 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001397 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001398 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001399 ret = btrfs_cross_ref_exist(root, ino,
1400 found_key.offset -
1401 extent_offset, disk_bytenr);
1402 if (ret) {
1403 /*
1404 * ret could be -EIO if the above fails to read
1405 * metadata.
1406 */
1407 if (ret < 0) {
1408 if (cow_start != (u64)-1)
1409 cur_offset = cow_start;
1410 goto error;
1411 }
1412
1413 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001414 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001415 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001416 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001417 disk_bytenr += cur_offset - found_key.offset;
1418 num_bytes = min(end + 1, extent_end) - cur_offset;
1419 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001420 * if there are pending snapshots for this root,
1421 * we fall into common COW way.
1422 */
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001423 if (!nolock && atomic_read(&root->snapshot_force_cow))
1424 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001425 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001426 * force cow if csum exists in the range.
1427 * this ensure that csum for a given extent are
1428 * either valid or do not exist.
1429 */
Liu Bo58113752018-01-31 17:09:13 -07001430 ret = csum_exist_in_range(fs_info, disk_bytenr,
1431 num_bytes);
1432 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001433 /*
1434 * ret could be -EIO if the above fails to read
1435 * metadata.
1436 */
1437 if (ret < 0) {
1438 if (cow_start != (u64)-1)
1439 cur_offset = cow_start;
1440 goto error;
1441 }
1442 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001443 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001444 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001445 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001446 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001447 nocow = 1;
1448 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1449 extent_end = found_key.offset +
Qu Wenruoe41ca582018-06-06 15:41:49 +08001450 btrfs_file_extent_ram_bytes(leaf, fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001451 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001452 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001453 } else {
1454 BUG_ON(1);
1455 }
1456out_check:
1457 if (extent_end <= start) {
1458 path->slots[0]++;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001459 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001460 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001461 goto next_slot;
1462 }
1463 if (!nocow) {
1464 if (cow_start == (u64)-1)
1465 cow_start = cur_offset;
1466 cur_offset = extent_end;
1467 if (cur_offset > end)
1468 break;
1469 path->slots[0]++;
1470 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001471 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001472
David Sterbab3b4aa72011-04-21 01:20:15 +02001473 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001474 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001475 ret = cow_file_range(inode, locked_page,
1476 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001477 end, page_started, nr_written, 1,
1478 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001479 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001480 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001481 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001482 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001483 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001484 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001485 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001486 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001487
Yan Zhengd899e052008-10-30 14:25:28 -04001488 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001489 u64 orig_start = found_key.offset - extent_offset;
1490
1491 em = create_io_em(inode, cur_offset, num_bytes,
1492 orig_start,
1493 disk_bytenr, /* block_start */
1494 num_bytes, /* block_len */
1495 disk_num_bytes, /* orig_block_len */
1496 ram_bytes, BTRFS_COMPRESS_NONE,
1497 BTRFS_ORDERED_PREALLOC);
1498 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001499 if (nocow)
1500 btrfs_dec_nocow_writers(fs_info,
1501 disk_bytenr);
1502 ret = PTR_ERR(em);
1503 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001504 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001505 free_extent_map(em);
1506 }
1507
1508 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001509 type = BTRFS_ORDERED_PREALLOC;
1510 } else {
1511 type = BTRFS_ORDERED_NOCOW;
1512 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001513
1514 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001515 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001516 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001517 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001518 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001519
Yan, Zhengefa56462010-05-16 10:49:59 -04001520 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001521 BTRFS_DATA_RELOC_TREE_OBJECTID)
1522 /*
1523 * Error handled later, as we must prevent
1524 * extent_clear_unlock_delalloc() in error handler
1525 * from freeing metadata of created ordered extent.
1526 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001527 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1528 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001529
Josef Bacikc2790a22013-07-29 11:20:47 -04001530 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001531 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001532 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001533 EXTENT_DELALLOC |
1534 EXTENT_CLEAR_DATA_RESV,
1535 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1536
Yan Zheng80ff3852008-10-30 14:20:02 -04001537 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001538
1539 /*
1540 * btrfs_reloc_clone_csums() error, now we're OK to call error
1541 * handler, as metadata for created ordered extent will only
1542 * be freed by btrfs_finish_ordered_io().
1543 */
1544 if (ret)
1545 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001546 if (cur_offset > end)
1547 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001548 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001549 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001550
Robbie Ko506481b2018-10-30 18:04:04 +08001551 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001552 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001553
Yan Zheng80ff3852008-10-30 14:20:02 -04001554 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001555 cur_offset = end;
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001556 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1557 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001558 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001559 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001560 }
1561
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001562error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001563 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001564 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001565 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001566 EXTENT_DELALLOC | EXTENT_DEFRAG |
1567 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1568 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001569 PAGE_SET_WRITEBACK |
1570 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001571 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001572 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001573}
1574
Wang Shilong47059d92014-07-03 18:22:07 +08001575static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1576{
1577
1578 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1579 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1580 return 0;
1581
1582 /*
1583 * @defrag_bytes is a hint value, no spinlock held here,
1584 * if is not zero, it means the file is defragging.
1585 * Force cow if given extent needs to be defragged.
1586 */
1587 if (BTRFS_I(inode)->defrag_bytes &&
1588 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1589 EXTENT_DEFRAG, 0, NULL))
1590 return 1;
1591
1592 return 0;
1593}
1594
Chris Masond352ac62008-09-29 15:18:18 -04001595/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001596 * Function to process delayed allocation (create CoW) for ranges which are
1597 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001598 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001599int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001600 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1601 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001602{
Chris Masonbe20aa92007-12-17 20:14:01 -05001603 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001604 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001605 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001606
Wang Shilong47059d92014-07-03 18:22:07 +08001607 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001608 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001609 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001610 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001611 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001612 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001613 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001614 ret = cow_file_range(inode, locked_page, start, end, end,
1615 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001616 } else {
1617 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1618 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001619 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001620 page_started, nr_written,
1621 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001622 }
Qu Wenruo524272602017-03-08 10:25:52 +08001623 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001624 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1625 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001626 return ret;
1627}
1628
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001629void btrfs_split_delalloc_extent(struct inode *inode,
1630 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001631{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001632 u64 size;
1633
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001634 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001635 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001636 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001637
Josef Bacikdcab6a32015-02-11 15:08:59 -05001638 size = orig->end - orig->start + 1;
1639 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001640 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 u64 new_size;
1642
1643 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001644 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001645 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001646 */
1647 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001648 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001649 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001650 num_extents += count_max_extents(new_size);
1651 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001652 return;
1653 }
1654
Josef Bacik9e0baf62011-07-15 15:16:44 +00001655 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001656 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001657 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001658}
1659
1660/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001661 * Handle merged delayed allocation extents so we can keep track of new extents
1662 * that are just merged onto old extents, such as when we are doing sequential
1663 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001664 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001665void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1666 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001667{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001668 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001669 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001670
Josef Bacik9ed74f22009-09-11 16:12:44 -04001671 /* not delalloc, ignore it */
1672 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001673 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001674
Josef Bacik8461a3d2015-03-13 15:12:08 -04001675 if (new->start > other->start)
1676 new_size = new->end - other->start + 1;
1677 else
1678 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001679
1680 /* we're not bigger than the max, unreserve the space and go */
1681 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1682 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001683 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001684 spin_unlock(&BTRFS_I(inode)->lock);
1685 return;
1686 }
1687
1688 /*
Josef Bacikba117212015-03-13 15:01:24 -04001689 * We have to add up either side to figure out how many extents were
1690 * accounted for before we merged into one big extent. If the number of
1691 * extents we accounted for is <= the amount we need for the new range
1692 * then we can return, otherwise drop. Think of it like this
1693 *
1694 * [ 4k][MAX_SIZE]
1695 *
1696 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1697 * need 2 outstanding extents, on one side we have 1 and the other side
1698 * we have 1 so they are == and we can return. But in this case
1699 *
1700 * [MAX_SIZE+4k][MAX_SIZE+4k]
1701 *
1702 * Each range on their own accounts for 2 extents, but merged together
1703 * they are only 3 extents worth of accounting, so we need to drop in
1704 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001705 */
Josef Bacikba117212015-03-13 15:01:24 -04001706 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001707 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001708 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001709 num_extents += count_max_extents(old_size);
1710 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001711 return;
1712
Josef Bacik9e0baf62011-07-15 15:16:44 +00001713 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001714 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001715 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001716}
1717
Miao Xieeb73c1b2013-05-15 07:48:22 +00001718static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1719 struct inode *inode)
1720{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001721 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1722
Miao Xieeb73c1b2013-05-15 07:48:22 +00001723 spin_lock(&root->delalloc_lock);
1724 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1725 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1726 &root->delalloc_inodes);
1727 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1728 &BTRFS_I(inode)->runtime_flags);
1729 root->nr_delalloc_inodes++;
1730 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001731 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001732 BUG_ON(!list_empty(&root->delalloc_root));
1733 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001734 &fs_info->delalloc_roots);
1735 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001736 }
1737 }
1738 spin_unlock(&root->delalloc_lock);
1739}
1740
Nikolay Borisov2b877332018-04-27 12:21:51 +03001741
1742void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1743 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001744{
David Sterba3ffbd682018-06-29 10:56:42 +02001745 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001746
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001747 if (!list_empty(&inode->delalloc_inodes)) {
1748 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001749 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001750 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001751 root->nr_delalloc_inodes--;
1752 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001753 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001754 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001755 BUG_ON(list_empty(&root->delalloc_root));
1756 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001757 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001758 }
1759 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001760}
1761
1762static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1763 struct btrfs_inode *inode)
1764{
1765 spin_lock(&root->delalloc_lock);
1766 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001767 spin_unlock(&root->delalloc_lock);
1768}
1769
Chris Masond352ac62008-09-29 15:18:18 -04001770/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001771 * Properly track delayed allocation bytes in the inode and to maintain the
1772 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001773 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001774void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1775 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001776{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001777 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1778
Wang Shilong47059d92014-07-03 18:22:07 +08001779 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1780 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001781 /*
1782 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001783 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001784 * bit, which is only set or cleared with irqs on
1785 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001786 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001787 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001788 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001789 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001790 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001791
Josef Bacik8b62f872017-10-19 14:15:55 -04001792 spin_lock(&BTRFS_I(inode)->lock);
1793 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1794 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001795
Josef Bacik6a3891c2015-03-16 17:38:52 -04001796 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001797 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001798 return;
1799
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001800 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1801 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001802 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001803 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001804 if (*bits & EXTENT_DEFRAG)
1805 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001806 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001807 &BTRFS_I(inode)->runtime_flags))
1808 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001809 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001810 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001811
1812 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1813 (*bits & EXTENT_DELALLOC_NEW)) {
1814 spin_lock(&BTRFS_I(inode)->lock);
1815 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1816 state->start;
1817 spin_unlock(&BTRFS_I(inode)->lock);
1818 }
Chris Mason291d6732008-01-29 15:55:23 -05001819}
1820
Chris Masond352ac62008-09-29 15:18:18 -04001821/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001822 * Once a range is no longer delalloc this function ensures that proper
1823 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001824 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001825void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1826 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001827{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001828 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1829 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001830 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001831 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001832
Filipe Manana4a4b9642017-07-27 19:52:55 +01001833 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1834 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001835 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001836 spin_unlock(&inode->lock);
1837 }
Wang Shilong47059d92014-07-03 18:22:07 +08001838
Chris Mason75eff682008-12-15 15:54:40 -05001839 /*
1840 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001841 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001842 * bit, which is only set or cleared with irqs on
1843 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001844 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001845 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001846 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001847
Josef Bacik8b62f872017-10-19 14:15:55 -04001848 spin_lock(&inode->lock);
1849 btrfs_mod_outstanding_extents(inode, -num_extents);
1850 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001851
Josef Bacikb6d08f02013-09-27 14:57:43 -04001852 /*
1853 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001854 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001855 * error.
1856 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001857 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001858 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001859 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001860
Josef Bacik6a3891c2015-03-16 17:38:52 -04001861 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001862 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001863 return;
1864
Filipe Mananaa315e682017-03-06 23:04:20 +00001865 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1866 do_list && !(state->state & EXTENT_NORESERVE) &&
1867 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001868 btrfs_free_reserved_data_space_noquota(
1869 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001870 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001871
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001872 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1873 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001874 spin_lock(&inode->lock);
1875 inode->delalloc_bytes -= len;
1876 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001877 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001878 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001879 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001880 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001881 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001882
1883 if ((state->state & EXTENT_DELALLOC_NEW) &&
1884 (*bits & EXTENT_DELALLOC_NEW)) {
1885 spin_lock(&inode->lock);
1886 ASSERT(inode->new_delalloc_bytes >= len);
1887 inode->new_delalloc_bytes -= len;
1888 spin_unlock(&inode->lock);
1889 }
Chris Mason291d6732008-01-29 15:55:23 -05001890}
1891
Chris Masond352ac62008-09-29 15:18:18 -04001892/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001893 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
1894 * in a chunk's stripe. This function ensures that bios do not span a
1895 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07001896 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001897 * @page - The page we are about to add to the bio
1898 * @size - size we want to add to the bio
1899 * @bio - bio we want to ensure is smaller than a stripe
1900 * @bio_flags - flags of the bio
1901 *
1902 * return 1 if page cannot be added to the bio
1903 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07001904 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001905 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02001906int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
1907 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001908{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001909 struct inode *inode = page->mapping->host;
1910 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001911 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001912 u64 length = 0;
1913 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001914 int ret;
1915
Chris Mason771ed682008-11-06 22:02:51 -05001916 if (bio_flags & EXTENT_BIO_COMPRESSED)
1917 return 0;
1918
Kent Overstreet4f024f32013-10-11 15:44:27 -07001919 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001920 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001921 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1922 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001923 if (ret < 0)
1924 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001925 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001926 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001927 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001928}
1929
Chris Masond352ac62008-09-29 15:18:18 -04001930/*
1931 * in order to insert checksums into the metadata in large chunks,
1932 * we wait until bio submission time. All the pages in the bio are
1933 * checksummed and sums are attached onto the ordered extent record.
1934 *
1935 * At IO completion time the cums attached on the ordered extent record
1936 * are inserted into the btree
1937 */
David Sterbad0ee3932018-03-08 14:35:48 +01001938static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001939 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001940{
Josef Bacikc6100a42017-05-05 11:57:13 -04001941 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001942 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001943
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001944 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001945 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001946 return 0;
1947}
Chris Masone0156402008-04-16 11:15:20 -04001948
Chris Mason4a69a412008-11-06 22:03:00 -05001949/*
Chris Masoncad321a2008-12-17 14:51:42 -05001950 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001951 * on write, or reading the csums from the tree before a read.
1952 *
1953 * Rules about async/sync submit,
1954 * a) read: sync submit
1955 *
1956 * b) write without checksum: sync submit
1957 *
1958 * c) write with checksum:
1959 * c-1) if bio is issued by fsync: sync submit
1960 * (sync_writers != 0)
1961 *
1962 * c-2) if root is reloc root: sync submit
1963 * (only in case of buffered IO)
1964 *
1965 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001966 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001967static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001968 int mirror_num, unsigned long bio_flags,
1969 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001970{
Josef Bacikc6100a42017-05-05 11:57:13 -04001971 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001972 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001973 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301974 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001975 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001976 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001977 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001978
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001979 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001980
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001981 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301982 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001983
Mike Christie37226b22016-06-05 14:31:52 -05001984 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001985 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001986 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001987 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001988
Chris Masond20f7042008-12-08 16:58:54 -05001989 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001990 ret = btrfs_submit_compressed_read(inode, bio,
1991 mirror_num,
1992 bio_flags);
1993 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001994 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001995 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001996 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001997 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001998 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001999 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002000 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002001 /* csum items have already been cloned */
2002 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2003 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002004 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002005 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2006 bio_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02002007 btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002008 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002009 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002010 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002011 if (ret)
2012 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002013 }
2014
Chris Mason0b86a832008-03-24 15:01:56 -04002015mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002016 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002017
2018out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002019 if (ret) {
2020 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002021 bio_endio(bio);
2022 }
Stefan Behrens61891922012-11-05 18:51:52 +01002023 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002024}
Chris Mason6885f302008-02-20 16:11:05 -05002025
Chris Masond352ac62008-09-29 15:18:18 -04002026/*
2027 * given a list of ordered sums record them in the inode. This happens
2028 * at IO completion time based on sums calculated at bio submission time.
2029 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002030static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002031 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002032{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002033 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002034 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002035
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002036 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002037 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002038 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002039 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002040 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002041 if (ret)
2042 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002043 }
2044 return 0;
2045}
2046
Josef Bacik2ac55d42010-02-03 19:33:23 +00002047int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002048 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002049 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002050{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002051 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002052 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002053 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002054}
2055
Chris Masond352ac62008-09-29 15:18:18 -04002056/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002057struct btrfs_writepage_fixup {
2058 struct page *page;
2059 struct btrfs_work work;
2060};
2061
Christoph Hellwigb2950862008-12-02 09:54:17 -05002062static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002063{
2064 struct btrfs_writepage_fixup *fixup;
2065 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002066 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002067 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002068 struct page *page;
2069 struct inode *inode;
2070 u64 page_start;
2071 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002072 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002073
2074 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2075 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002076again:
Chris Mason247e7432008-07-17 12:53:51 -04002077 lock_page(page);
2078 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2079 ClearPageChecked(page);
2080 goto out_page;
2081 }
2082
2083 inode = page->mapping->host;
2084 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002085 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002086
David Sterbaff13db42015-12-03 14:30:40 +01002087 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002088 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002089
2090 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002091 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002092 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002093
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002094 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002095 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002096 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002097 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002098 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002099 unlock_page(page);
2100 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002101 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002102 goto again;
2103 }
Chris Mason247e7432008-07-17 12:53:51 -04002104
Qu Wenruo364ecf32017-02-27 15:10:38 +08002105 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002106 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002107 if (ret) {
2108 mapping_set_error(page->mapping, ret);
2109 end_extent_writepage(page, ret, page_start, page_end);
2110 ClearPageChecked(page);
2111 goto out;
2112 }
2113
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002114 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2115 &cached_state, 0);
2116 if (ret) {
2117 mapping_set_error(page->mapping, ret);
2118 end_extent_writepage(page, ret, page_start, page_end);
2119 ClearPageChecked(page);
2120 goto out;
2121 }
2122
Chris Mason247e7432008-07-17 12:53:51 -04002123 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002124 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002125 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002126out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002127 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002128 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002129out_page:
2130 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002131 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002132 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002133 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002134}
2135
2136/*
2137 * There are a few paths in the higher layers of the kernel that directly
2138 * set the page dirty bit without asking the filesystem if it is a
2139 * good idea. This causes problems because we want to make sure COW
2140 * properly happens and the data=ordered rules are followed.
2141 *
Chris Masonc8b97812008-10-29 14:49:59 -04002142 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002143 * hasn't been properly setup for IO. We kick off an async process
2144 * to fix it up. The async helper will wait for ordered extents, set
2145 * the delalloc bit and make it safe to write the page.
2146 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002147int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002148{
2149 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002150 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002151 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002152
Chris Mason8b62b722009-09-02 16:53:46 -04002153 /* this page is properly in the ordered list */
2154 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002155 return 0;
2156
2157 if (PageChecked(page))
2158 return -EAGAIN;
2159
2160 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2161 if (!fixup)
2162 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002163
Chris Mason247e7432008-07-17 12:53:51 -04002164 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002165 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002166 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2167 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002168 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002169 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002170 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002171}
2172
Yan Zhengd899e052008-10-30 14:25:28 -04002173static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2174 struct inode *inode, u64 file_pos,
2175 u64 disk_bytenr, u64 disk_num_bytes,
2176 u64 num_bytes, u64 ram_bytes,
2177 u8 compression, u8 encryption,
2178 u16 other_encoding, int extent_type)
2179{
2180 struct btrfs_root *root = BTRFS_I(inode)->root;
2181 struct btrfs_file_extent_item *fi;
2182 struct btrfs_path *path;
2183 struct extent_buffer *leaf;
2184 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002185 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002186 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002187 int ret;
2188
2189 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002190 if (!path)
2191 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002192
Chris Masona1ed8352009-09-11 12:27:37 -04002193 /*
2194 * we may be replacing one extent in the tree with another.
2195 * The new extent is pinned in the extent map, and we don't want
2196 * to drop it from the cache until it is completely in the btree.
2197 *
2198 * So, tell btrfs_drop_extents to leave this extent in the cache.
2199 * the caller is expected to unpin it and allow it to be merged
2200 * with the others.
2201 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002202 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2203 file_pos + num_bytes, NULL, 0,
2204 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002205 if (ret)
2206 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002207
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002208 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002209 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002210 ins.offset = file_pos;
2211 ins.type = BTRFS_EXTENT_DATA_KEY;
2212
2213 path->leave_spinning = 1;
2214 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2215 sizeof(*fi));
2216 if (ret)
2217 goto out;
2218 }
Yan Zhengd899e052008-10-30 14:25:28 -04002219 leaf = path->nodes[0];
2220 fi = btrfs_item_ptr(leaf, path->slots[0],
2221 struct btrfs_file_extent_item);
2222 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2223 btrfs_set_file_extent_type(leaf, fi, extent_type);
2224 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2225 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2226 btrfs_set_file_extent_offset(leaf, fi, 0);
2227 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2228 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2229 btrfs_set_file_extent_compression(leaf, fi, compression);
2230 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2231 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002232
Yan Zhengd899e052008-10-30 14:25:28 -04002233 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002234 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002235
2236 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002237
2238 ins.objectid = disk_bytenr;
2239 ins.offset = disk_num_bytes;
2240 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002241
Qu Wenruo297d7502015-09-08 17:08:37 +08002242 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002243 * Release the reserved range from inode dirty range map, as it is
2244 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002245 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002246 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2247 if (ret < 0)
2248 goto out;
2249 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002250 ret = btrfs_alloc_reserved_file_extent(trans, root,
2251 btrfs_ino(BTRFS_I(inode)),
2252 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002253out:
Yan Zhengd899e052008-10-30 14:25:28 -04002254 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002255
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002256 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002257}
2258
Liu Bo38c227d2013-01-29 03:18:40 +00002259/* snapshot-aware defrag */
2260struct sa_defrag_extent_backref {
2261 struct rb_node node;
2262 struct old_sa_defrag_extent *old;
2263 u64 root_id;
2264 u64 inum;
2265 u64 file_pos;
2266 u64 extent_offset;
2267 u64 num_bytes;
2268 u64 generation;
2269};
2270
2271struct old_sa_defrag_extent {
2272 struct list_head list;
2273 struct new_sa_defrag_extent *new;
2274
2275 u64 extent_offset;
2276 u64 bytenr;
2277 u64 offset;
2278 u64 len;
2279 int count;
2280};
2281
2282struct new_sa_defrag_extent {
2283 struct rb_root root;
2284 struct list_head head;
2285 struct btrfs_path *path;
2286 struct inode *inode;
2287 u64 file_pos;
2288 u64 len;
2289 u64 bytenr;
2290 u64 disk_len;
2291 u8 compress_type;
2292};
2293
2294static int backref_comp(struct sa_defrag_extent_backref *b1,
2295 struct sa_defrag_extent_backref *b2)
2296{
2297 if (b1->root_id < b2->root_id)
2298 return -1;
2299 else if (b1->root_id > b2->root_id)
2300 return 1;
2301
2302 if (b1->inum < b2->inum)
2303 return -1;
2304 else if (b1->inum > b2->inum)
2305 return 1;
2306
2307 if (b1->file_pos < b2->file_pos)
2308 return -1;
2309 else if (b1->file_pos > b2->file_pos)
2310 return 1;
2311
2312 /*
2313 * [------------------------------] ===> (a range of space)
2314 * |<--->| |<---->| =============> (fs/file tree A)
2315 * |<---------------------------->| ===> (fs/file tree B)
2316 *
2317 * A range of space can refer to two file extents in one tree while
2318 * refer to only one file extent in another tree.
2319 *
2320 * So we may process a disk offset more than one time(two extents in A)
2321 * and locate at the same extent(one extent in B), then insert two same
2322 * backrefs(both refer to the extent in B).
2323 */
2324 return 0;
2325}
2326
2327static void backref_insert(struct rb_root *root,
2328 struct sa_defrag_extent_backref *backref)
2329{
2330 struct rb_node **p = &root->rb_node;
2331 struct rb_node *parent = NULL;
2332 struct sa_defrag_extent_backref *entry;
2333 int ret;
2334
2335 while (*p) {
2336 parent = *p;
2337 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2338
2339 ret = backref_comp(backref, entry);
2340 if (ret < 0)
2341 p = &(*p)->rb_left;
2342 else
2343 p = &(*p)->rb_right;
2344 }
2345
2346 rb_link_node(&backref->node, parent, p);
2347 rb_insert_color(&backref->node, root);
2348}
2349
2350/*
2351 * Note the backref might has changed, and in this case we just return 0.
2352 */
2353static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2354 void *ctx)
2355{
2356 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002357 struct old_sa_defrag_extent *old = ctx;
2358 struct new_sa_defrag_extent *new = old->new;
2359 struct btrfs_path *path = new->path;
2360 struct btrfs_key key;
2361 struct btrfs_root *root;
2362 struct sa_defrag_extent_backref *backref;
2363 struct extent_buffer *leaf;
2364 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002365 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002366 int slot;
2367 int ret;
2368 u64 extent_offset;
2369 u64 num_bytes;
2370
2371 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002372 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002373 return 0;
2374
2375 key.objectid = root_id;
2376 key.type = BTRFS_ROOT_ITEM_KEY;
2377 key.offset = (u64)-1;
2378
Liu Bo38c227d2013-01-29 03:18:40 +00002379 root = btrfs_read_fs_root_no_name(fs_info, &key);
2380 if (IS_ERR(root)) {
2381 if (PTR_ERR(root) == -ENOENT)
2382 return 0;
2383 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002384 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002385 inum, offset, root_id);
2386 return PTR_ERR(root);
2387 }
2388
2389 key.objectid = inum;
2390 key.type = BTRFS_EXTENT_DATA_KEY;
2391 if (offset > (u64)-1 << 32)
2392 key.offset = 0;
2393 else
2394 key.offset = offset;
2395
2396 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302397 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002398 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002399 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002400
2401 while (1) {
2402 cond_resched();
2403
2404 leaf = path->nodes[0];
2405 slot = path->slots[0];
2406
2407 if (slot >= btrfs_header_nritems(leaf)) {
2408 ret = btrfs_next_leaf(root, path);
2409 if (ret < 0) {
2410 goto out;
2411 } else if (ret > 0) {
2412 ret = 0;
2413 goto out;
2414 }
2415 continue;
2416 }
2417
2418 path->slots[0]++;
2419
2420 btrfs_item_key_to_cpu(leaf, &key, slot);
2421
2422 if (key.objectid > inum)
2423 goto out;
2424
2425 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2426 continue;
2427
2428 extent = btrfs_item_ptr(leaf, slot,
2429 struct btrfs_file_extent_item);
2430
2431 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2432 continue;
2433
Liu Boe68afa42013-07-01 22:13:26 +08002434 /*
2435 * 'offset' refers to the exact key.offset,
2436 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2437 * (key.offset - extent_offset).
2438 */
2439 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002440 continue;
2441
Liu Boe68afa42013-07-01 22:13:26 +08002442 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002443 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002444
Liu Bo38c227d2013-01-29 03:18:40 +00002445 if (extent_offset >= old->extent_offset + old->offset +
2446 old->len || extent_offset + num_bytes <=
2447 old->extent_offset + old->offset)
2448 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002449 break;
2450 }
2451
2452 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2453 if (!backref) {
2454 ret = -ENOENT;
2455 goto out;
2456 }
2457
2458 backref->root_id = root_id;
2459 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002460 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002461 backref->num_bytes = num_bytes;
2462 backref->extent_offset = extent_offset;
2463 backref->generation = btrfs_file_extent_generation(leaf, extent);
2464 backref->old = old;
2465 backref_insert(&new->root, backref);
2466 old->count++;
2467out:
2468 btrfs_release_path(path);
2469 WARN_ON(ret);
2470 return ret;
2471}
2472
2473static noinline bool record_extent_backrefs(struct btrfs_path *path,
2474 struct new_sa_defrag_extent *new)
2475{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002476 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002477 struct old_sa_defrag_extent *old, *tmp;
2478 int ret;
2479
2480 new->path = path;
2481
2482 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002483 ret = iterate_inodes_from_logical(old->bytenr +
2484 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002485 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002486 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002487 if (ret < 0 && ret != -ENOENT)
2488 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002489
2490 /* no backref to be processed for this extent */
2491 if (!old->count) {
2492 list_del(&old->list);
2493 kfree(old);
2494 }
2495 }
2496
2497 if (list_empty(&new->head))
2498 return false;
2499
2500 return true;
2501}
2502
2503static int relink_is_mergable(struct extent_buffer *leaf,
2504 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002505 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002506{
Liu Bo116e0022013-08-02 16:30:40 +08002507 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002508 return 0;
2509
2510 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2511 return 0;
2512
Liu Bo116e0022013-08-02 16:30:40 +08002513 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2514 return 0;
2515
2516 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002517 btrfs_file_extent_other_encoding(leaf, fi))
2518 return 0;
2519
2520 return 1;
2521}
2522
2523/*
2524 * Note the backref might has changed, and in this case we just return 0.
2525 */
2526static noinline int relink_extent_backref(struct btrfs_path *path,
2527 struct sa_defrag_extent_backref *prev,
2528 struct sa_defrag_extent_backref *backref)
2529{
2530 struct btrfs_file_extent_item *extent;
2531 struct btrfs_file_extent_item *item;
2532 struct btrfs_ordered_extent *ordered;
2533 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002534 struct btrfs_root *root;
2535 struct btrfs_key key;
2536 struct extent_buffer *leaf;
2537 struct old_sa_defrag_extent *old = backref->old;
2538 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002539 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002540 struct inode *inode;
2541 struct extent_state *cached = NULL;
2542 int ret = 0;
2543 u64 start;
2544 u64 len;
2545 u64 lock_start;
2546 u64 lock_end;
2547 bool merge = false;
2548 int index;
2549
2550 if (prev && prev->root_id == backref->root_id &&
2551 prev->inum == backref->inum &&
2552 prev->file_pos + prev->num_bytes == backref->file_pos)
2553 merge = true;
2554
2555 /* step 1: get root */
2556 key.objectid = backref->root_id;
2557 key.type = BTRFS_ROOT_ITEM_KEY;
2558 key.offset = (u64)-1;
2559
Liu Bo38c227d2013-01-29 03:18:40 +00002560 index = srcu_read_lock(&fs_info->subvol_srcu);
2561
2562 root = btrfs_read_fs_root_no_name(fs_info, &key);
2563 if (IS_ERR(root)) {
2564 srcu_read_unlock(&fs_info->subvol_srcu, index);
2565 if (PTR_ERR(root) == -ENOENT)
2566 return 0;
2567 return PTR_ERR(root);
2568 }
Liu Bo38c227d2013-01-29 03:18:40 +00002569
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002570 if (btrfs_root_readonly(root)) {
2571 srcu_read_unlock(&fs_info->subvol_srcu, index);
2572 return 0;
2573 }
2574
Liu Bo38c227d2013-01-29 03:18:40 +00002575 /* step 2: get inode */
2576 key.objectid = backref->inum;
2577 key.type = BTRFS_INODE_ITEM_KEY;
2578 key.offset = 0;
2579
2580 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2581 if (IS_ERR(inode)) {
2582 srcu_read_unlock(&fs_info->subvol_srcu, index);
2583 return 0;
2584 }
2585
2586 srcu_read_unlock(&fs_info->subvol_srcu, index);
2587
2588 /* step 3: relink backref */
2589 lock_start = backref->file_pos;
2590 lock_end = backref->file_pos + backref->num_bytes - 1;
2591 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002592 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002593
2594 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2595 if (ordered) {
2596 btrfs_put_ordered_extent(ordered);
2597 goto out_unlock;
2598 }
2599
2600 trans = btrfs_join_transaction(root);
2601 if (IS_ERR(trans)) {
2602 ret = PTR_ERR(trans);
2603 goto out_unlock;
2604 }
2605
2606 key.objectid = backref->inum;
2607 key.type = BTRFS_EXTENT_DATA_KEY;
2608 key.offset = backref->file_pos;
2609
2610 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2611 if (ret < 0) {
2612 goto out_free_path;
2613 } else if (ret > 0) {
2614 ret = 0;
2615 goto out_free_path;
2616 }
2617
2618 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2619 struct btrfs_file_extent_item);
2620
2621 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2622 backref->generation)
2623 goto out_free_path;
2624
2625 btrfs_release_path(path);
2626
2627 start = backref->file_pos;
2628 if (backref->extent_offset < old->extent_offset + old->offset)
2629 start += old->extent_offset + old->offset -
2630 backref->extent_offset;
2631
2632 len = min(backref->extent_offset + backref->num_bytes,
2633 old->extent_offset + old->offset + old->len);
2634 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2635
2636 ret = btrfs_drop_extents(trans, root, inode, start,
2637 start + len, 1);
2638 if (ret)
2639 goto out_free_path;
2640again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002641 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002642 key.type = BTRFS_EXTENT_DATA_KEY;
2643 key.offset = start;
2644
Liu Boa09a0a72013-03-11 09:20:58 +00002645 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002646 if (merge) {
2647 struct btrfs_file_extent_item *fi;
2648 u64 extent_len;
2649 struct btrfs_key found_key;
2650
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002651 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002652 if (ret < 0)
2653 goto out_free_path;
2654
2655 path->slots[0]--;
2656 leaf = path->nodes[0];
2657 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2658
2659 fi = btrfs_item_ptr(leaf, path->slots[0],
2660 struct btrfs_file_extent_item);
2661 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2662
Liu Bo116e0022013-08-02 16:30:40 +08002663 if (extent_len + found_key.offset == start &&
2664 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002665 btrfs_set_file_extent_num_bytes(leaf, fi,
2666 extent_len + len);
2667 btrfs_mark_buffer_dirty(leaf);
2668 inode_add_bytes(inode, len);
2669
2670 ret = 1;
2671 goto out_free_path;
2672 } else {
2673 merge = false;
2674 btrfs_release_path(path);
2675 goto again;
2676 }
2677 }
2678
2679 ret = btrfs_insert_empty_item(trans, root, path, &key,
2680 sizeof(*extent));
2681 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002682 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002683 goto out_free_path;
2684 }
2685
2686 leaf = path->nodes[0];
2687 item = btrfs_item_ptr(leaf, path->slots[0],
2688 struct btrfs_file_extent_item);
2689 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2690 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2691 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2692 btrfs_set_file_extent_num_bytes(leaf, item, len);
2693 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2694 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2695 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2696 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2697 btrfs_set_file_extent_encryption(leaf, item, 0);
2698 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2699
2700 btrfs_mark_buffer_dirty(leaf);
2701 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002702 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002703
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002704 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002705 new->disk_len, 0,
2706 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002707 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002708 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002709 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002710 goto out_free_path;
2711 }
2712
2713 ret = 1;
2714out_free_path:
2715 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002716 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002717 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002718out_unlock:
2719 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002720 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002721 iput(inode);
2722 return ret;
2723}
2724
Liu Bo6f519562013-10-29 10:45:05 +08002725static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2726{
2727 struct old_sa_defrag_extent *old, *tmp;
2728
2729 if (!new)
2730 return;
2731
2732 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002733 kfree(old);
2734 }
2735 kfree(new);
2736}
2737
Liu Bo38c227d2013-01-29 03:18:40 +00002738static void relink_file_extents(struct new_sa_defrag_extent *new)
2739{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002740 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002741 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002742 struct sa_defrag_extent_backref *backref;
2743 struct sa_defrag_extent_backref *prev = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002744 struct rb_node *node;
2745 int ret;
2746
Liu Bo38c227d2013-01-29 03:18:40 +00002747 path = btrfs_alloc_path();
2748 if (!path)
2749 return;
2750
2751 if (!record_extent_backrefs(path, new)) {
2752 btrfs_free_path(path);
2753 goto out;
2754 }
2755 btrfs_release_path(path);
2756
2757 while (1) {
2758 node = rb_first(&new->root);
2759 if (!node)
2760 break;
2761 rb_erase(node, &new->root);
2762
2763 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2764
2765 ret = relink_extent_backref(path, prev, backref);
2766 WARN_ON(ret < 0);
2767
2768 kfree(prev);
2769
2770 if (ret == 1)
2771 prev = backref;
2772 else
2773 prev = NULL;
2774 cond_resched();
2775 }
2776 kfree(prev);
2777
2778 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002779out:
Liu Bo6f519562013-10-29 10:45:05 +08002780 free_sa_defrag_extent(new);
2781
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002782 atomic_dec(&fs_info->defrag_running);
2783 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002784}
2785
2786static struct new_sa_defrag_extent *
2787record_old_file_extents(struct inode *inode,
2788 struct btrfs_ordered_extent *ordered)
2789{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002790 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002791 struct btrfs_root *root = BTRFS_I(inode)->root;
2792 struct btrfs_path *path;
2793 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002794 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002795 struct new_sa_defrag_extent *new;
2796 int ret;
2797
2798 new = kmalloc(sizeof(*new), GFP_NOFS);
2799 if (!new)
2800 return NULL;
2801
2802 new->inode = inode;
2803 new->file_pos = ordered->file_offset;
2804 new->len = ordered->len;
2805 new->bytenr = ordered->start;
2806 new->disk_len = ordered->disk_len;
2807 new->compress_type = ordered->compress_type;
2808 new->root = RB_ROOT;
2809 INIT_LIST_HEAD(&new->head);
2810
2811 path = btrfs_alloc_path();
2812 if (!path)
2813 goto out_kfree;
2814
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002815 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002816 key.type = BTRFS_EXTENT_DATA_KEY;
2817 key.offset = new->file_pos;
2818
2819 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2820 if (ret < 0)
2821 goto out_free_path;
2822 if (ret > 0 && path->slots[0] > 0)
2823 path->slots[0]--;
2824
2825 /* find out all the old extents for the file range */
2826 while (1) {
2827 struct btrfs_file_extent_item *extent;
2828 struct extent_buffer *l;
2829 int slot;
2830 u64 num_bytes;
2831 u64 offset;
2832 u64 end;
2833 u64 disk_bytenr;
2834 u64 extent_offset;
2835
2836 l = path->nodes[0];
2837 slot = path->slots[0];
2838
2839 if (slot >= btrfs_header_nritems(l)) {
2840 ret = btrfs_next_leaf(root, path);
2841 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002842 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002843 else if (ret > 0)
2844 break;
2845 continue;
2846 }
2847
2848 btrfs_item_key_to_cpu(l, &key, slot);
2849
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002850 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002851 break;
2852 if (key.type != BTRFS_EXTENT_DATA_KEY)
2853 break;
2854 if (key.offset >= new->file_pos + new->len)
2855 break;
2856
2857 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2858
2859 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2860 if (key.offset + num_bytes < new->file_pos)
2861 goto next;
2862
2863 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2864 if (!disk_bytenr)
2865 goto next;
2866
2867 extent_offset = btrfs_file_extent_offset(l, extent);
2868
2869 old = kmalloc(sizeof(*old), GFP_NOFS);
2870 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002871 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002872
2873 offset = max(new->file_pos, key.offset);
2874 end = min(new->file_pos + new->len, key.offset + num_bytes);
2875
2876 old->bytenr = disk_bytenr;
2877 old->extent_offset = extent_offset;
2878 old->offset = offset - key.offset;
2879 old->len = end - offset;
2880 old->new = new;
2881 old->count = 0;
2882 list_add_tail(&old->list, &new->head);
2883next:
2884 path->slots[0]++;
2885 cond_resched();
2886 }
2887
2888 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002889 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002890
2891 return new;
2892
Liu Bo38c227d2013-01-29 03:18:40 +00002893out_free_path:
2894 btrfs_free_path(path);
2895out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002896 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002897 return NULL;
2898}
2899
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002900static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002901 u64 start, u64 len)
2902{
2903 struct btrfs_block_group_cache *cache;
2904
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002905 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002906 ASSERT(cache);
2907
2908 spin_lock(&cache->lock);
2909 cache->delalloc_bytes -= len;
2910 spin_unlock(&cache->lock);
2911
2912 btrfs_put_block_group(cache);
2913}
2914
Chris Masond352ac62008-09-29 15:18:18 -04002915/* as ordered data IO finishes, this gets called so we can finish
2916 * an ordered extent if the range of bytes in the file it covers are
2917 * fully written.
2918 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002919static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002920{
Josef Bacik5fd02042012-05-02 14:00:54 -04002921 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002922 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002923 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002924 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002925 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002926 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002927 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002928 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002929 int ret = 0;
2930 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002931 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002932 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002933 bool range_locked = false;
2934 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002935 bool clear_reserved_extent = true;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002936
2937 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2938 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2939 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2940 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002941
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002942 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002943
Josef Bacik5fd02042012-05-02 14:00:54 -04002944 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2945 ret = -EIO;
2946 goto out;
2947 }
2948
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002949 btrfs_free_io_failure_record(BTRFS_I(inode),
2950 ordered_extent->file_offset,
2951 ordered_extent->file_offset +
2952 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002953
Josef Bacik77cef2e2013-08-29 13:57:21 -04002954 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2955 truncated = true;
2956 logical_len = ordered_extent->truncated_len;
2957 /* Truncated the entire extent, don't bother adding */
2958 if (!logical_len)
2959 goto out;
2960 }
2961
Yan, Zhengc2167752009-11-12 09:34:21 +00002962 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002963 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002964
2965 /*
2966 * For mwrite(mmap + memset to write) case, we still reserve
2967 * space for NOCOW range.
2968 * As NOCOW won't cause a new delayed ref, just free the space
2969 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002970 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002971 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002972 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2973 if (nolock)
2974 trans = btrfs_join_transaction_nolock(root);
2975 else
2976 trans = btrfs_join_transaction(root);
2977 if (IS_ERR(trans)) {
2978 ret = PTR_ERR(trans);
2979 trans = NULL;
2980 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002981 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002982 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002983 ret = btrfs_update_inode_fallback(trans, root, inode);
2984 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002985 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002986 goto out;
2987 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002988
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002989 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002990 lock_extent_bits(io_tree, ordered_extent->file_offset,
2991 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002992 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002993
Liu Bo38c227d2013-01-29 03:18:40 +00002994 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2995 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002996 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002997 if (ret) {
2998 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002999 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003000 /* the inode is shared */
3001 new = record_old_file_extents(inode, ordered_extent);
3002
3003 clear_extent_bit(io_tree, ordered_extent->file_offset,
3004 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003005 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003006 }
3007
Josef Bacik0cb59c92010-07-02 12:14:14 -04003008 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003009 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003010 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003011 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003012 if (IS_ERR(trans)) {
3013 ret = PTR_ERR(trans);
3014 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003015 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003016 }
Chris Masona79b7d42014-05-22 16:18:52 -07003017
Josef Bacik69fe2d72017-10-19 14:15:57 -04003018 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003019
Chris Masonc8b97812008-10-29 14:49:59 -04003020 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003021 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003022 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003023 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003024 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3025 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003026 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003027 ordered_extent->file_offset,
3028 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003029 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003030 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003031 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003032 ret = insert_reserved_file_extent(trans, inode,
3033 ordered_extent->file_offset,
3034 ordered_extent->start,
3035 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003036 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003037 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003038 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04003039 if (!ret) {
3040 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003041 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003042 ordered_extent->start,
3043 ordered_extent->disk_len);
Josef Bacik49940bd2018-10-11 15:54:21 -04003044 }
Yan Zhengd899e052008-10-30 14:25:28 -04003045 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003046 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3047 ordered_extent->file_offset, ordered_extent->len,
3048 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003050 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003051 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003052 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003053
Nikolay Borisovac01f262018-01-08 10:59:43 +02003054 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3055 if (ret) {
3056 btrfs_abort_transaction(trans, ret);
3057 goto out;
3058 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003059
Josef Bacik6c760c02012-11-09 10:53:21 -05003060 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3061 ret = btrfs_update_inode_fallback(trans, root, inode);
3062 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003063 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003064 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003065 }
3066 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003067out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003068 if (range_locked || clear_new_delalloc_bytes) {
3069 unsigned int clear_bits = 0;
3070
3071 if (range_locked)
3072 clear_bits |= EXTENT_LOCKED;
3073 if (clear_new_delalloc_bytes)
3074 clear_bits |= EXTENT_DELALLOC_NEW;
3075 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3076 ordered_extent->file_offset,
3077 ordered_extent->file_offset +
3078 ordered_extent->len - 1,
3079 clear_bits,
3080 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003081 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003082 }
3083
Miao Xiea698d0752012-09-20 01:51:59 -06003084 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003085 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003086
Josef Bacik77cef2e2013-08-29 13:57:21 -04003087 if (ret || truncated) {
3088 u64 start, end;
3089
3090 if (truncated)
3091 start = ordered_extent->file_offset + logical_len;
3092 else
3093 start = ordered_extent->file_offset;
3094 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003095 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003096
3097 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003098 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003099
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003100 /*
3101 * If the ordered extent had an IOERR or something else went
3102 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003103 * back to the allocator. We only free the extent in the
3104 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04003105 *
3106 * If we made it past insert_reserved_file_extent before we
3107 * errored out then we don't need to do this as the accounting
3108 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003109 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003110 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04003111 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04003112 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003113 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003114 btrfs_free_reserved_extent(fs_info,
3115 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003116 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003117 }
3118
3119
Josef Bacik5fd02042012-05-02 14:00:54 -04003120 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003121 * This needs to be done to make sure anybody waiting knows we are done
3122 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003123 */
3124 btrfs_remove_ordered_extent(inode, ordered_extent);
3125
Liu Bo38c227d2013-01-29 03:18:40 +00003126 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003127 if (new) {
3128 if (ret) {
3129 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003130 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003131 } else {
3132 relink_file_extents(new);
3133 }
3134 }
Liu Bo38c227d2013-01-29 03:18:40 +00003135
Chris Masone6dcd2d2008-07-17 12:53:50 -04003136 /* once for us */
3137 btrfs_put_ordered_extent(ordered_extent);
3138 /* once for the tree */
3139 btrfs_put_ordered_extent(ordered_extent);
3140
Josef Bacik5fd02042012-05-02 14:00:54 -04003141 return ret;
3142}
3143
3144static void finish_ordered_fn(struct btrfs_work *work)
3145{
3146 struct btrfs_ordered_extent *ordered_extent;
3147 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3148 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003149}
3150
Nikolay Borisovc6297322018-11-08 10:18:08 +02003151void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3152 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04003153{
Josef Bacik5fd02042012-05-02 14:00:54 -04003154 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003155 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003156 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003157 struct btrfs_workqueue *wq;
3158 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003159
liubo1abe9b82011-03-24 11:18:59 +00003160 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3161
Chris Mason8b62b722009-09-02 16:53:46 -04003162 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003163 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3164 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003165 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003166
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003167 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003168 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003169 func = btrfs_freespace_write_helper;
3170 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003171 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003172 func = btrfs_endio_write_helper;
3173 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003174
Liu Bo9e0af232014-08-15 23:36:53 +08003175 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3176 NULL);
3177 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003178}
3179
Miao Xiedc380ae2014-09-12 18:43:55 +08003180static int __readpage_endio_check(struct inode *inode,
3181 struct btrfs_io_bio *io_bio,
3182 int icsum, struct page *page,
3183 int pgoff, u64 start, size_t len)
3184{
3185 char *kaddr;
3186 u32 csum_expected;
3187 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003188
3189 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3190
3191 kaddr = kmap_atomic(page);
3192 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003193 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003194 if (csum != csum_expected)
3195 goto zeroit;
3196
3197 kunmap_atomic(kaddr);
3198 return 0;
3199zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003200 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003201 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003202 memset(kaddr + pgoff, 1, len);
3203 flush_dcache_page(page);
3204 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003205 return -EIO;
3206}
3207
Chris Masond352ac62008-09-29 15:18:18 -04003208/*
Chris Masond352ac62008-09-29 15:18:18 -04003209 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003210 * if there's a match, we allow the bio to finish. If not, the code in
3211 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003212 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003213static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3214 u64 phy_offset, struct page *page,
3215 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003216{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003217 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003218 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003219 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003220 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003221
Chris Masond20f7042008-12-08 16:58:54 -05003222 if (PageChecked(page)) {
3223 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003224 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003225 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003226
3227 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003228 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003229
Yan Zheng17d217f2008-12-12 10:03:38 -05003230 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003231 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003232 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003233 return 0;
3234 }
3235
Miao Xiefacc8a222013-07-25 19:22:34 +08003236 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003237 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3238 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003239}
Chris Masonb888db22007-08-27 16:49:44 -04003240
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003241/*
3242 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3243 *
3244 * @inode: The inode we want to perform iput on
3245 *
3246 * This function uses the generic vfs_inode::i_count to track whether we should
3247 * just decrement it (in case it's > 1) or if this is the last iput then link
3248 * the inode to the delayed iput machinery. Delayed iputs are processed at
3249 * transaction commit time/superblock commit/cleaner kthread.
3250 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003251void btrfs_add_delayed_iput(struct inode *inode)
3252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003253 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003254 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003255
3256 if (atomic_add_unless(&inode->i_count, -1, 1))
3257 return;
3258
Josef Bacik034f7842018-12-03 11:06:52 -05003259 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003260 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003261 ASSERT(list_empty(&binode->delayed_iput));
3262 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003263 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003264 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3265 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003266}
3267
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003268void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003269{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003270
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003271 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003272 while (!list_empty(&fs_info->delayed_iputs)) {
3273 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003274
David Sterba8089fe62015-11-19 14:15:51 +01003275 inode = list_first_entry(&fs_info->delayed_iputs,
3276 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003277 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003278 spin_unlock(&fs_info->delayed_iput_lock);
3279 iput(&inode->vfs_inode);
Josef Bacik034f7842018-12-03 11:06:52 -05003280 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3281 wake_up(&fs_info->delayed_iputs_wait);
David Sterba8089fe62015-11-19 14:15:51 +01003282 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003283 }
David Sterba8089fe62015-11-19 14:15:51 +01003284 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003285}
3286
Josef Bacik034f7842018-12-03 11:06:52 -05003287/**
3288 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3289 * @fs_info - the fs_info for this fs
3290 * @return - EINTR if we were killed, 0 if nothing's pending
3291 *
3292 * This will wait on any delayed iputs that are currently running with KILLABLE
3293 * set. Once they are all done running we will return, unless we are killed in
3294 * which case we return EINTR. This helps in user operations like fallocate etc
3295 * that might get blocked on the iputs.
3296 */
3297int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3298{
3299 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3300 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3301 if (ret)
3302 return -EINTR;
3303 return 0;
3304}
3305
Yan, Zhengd68fc572010-05-16 10:49:58 -04003306/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003307 * This creates an orphan entry for the given inode in case something goes wrong
3308 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003309 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003310int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003311 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003312{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003314
Omar Sandoval27919062018-05-11 13:13:37 -07003315 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3316 if (ret && ret != -EEXIST) {
3317 btrfs_abort_transaction(trans, ret);
3318 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003319 }
3320
Yan, Zhengd68fc572010-05-16 10:49:58 -04003321 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003322}
3323
3324/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003325 * We have done the delete so we can go ahead and remove the orphan item for
3326 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003327 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003328static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003329 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003330{
Omar Sandoval27919062018-05-11 13:13:37 -07003331 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003332}
3333
3334/*
3335 * this cleans up any orphans that may be left on the list from the last use
3336 * of this root.
3337 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003338int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003339{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003340 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003341 struct btrfs_path *path;
3342 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003343 struct btrfs_key key, found_key;
3344 struct btrfs_trans_handle *trans;
3345 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003346 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003347 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003348
Yan, Zhengd68fc572010-05-16 10:49:58 -04003349 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003350 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003351
3352 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003353 if (!path) {
3354 ret = -ENOMEM;
3355 goto out;
3356 }
David Sterbae4058b52015-11-27 16:31:35 +01003357 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003358
3359 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003360 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003361 key.offset = (u64)-1;
3362
Josef Bacik7b128762008-07-24 12:17:14 -04003363 while (1) {
3364 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003365 if (ret < 0)
3366 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003367
3368 /*
3369 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003370 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003371 * find the key and see if we have stuff that matches
3372 */
3373 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003374 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003375 if (path->slots[0] == 0)
3376 break;
3377 path->slots[0]--;
3378 }
3379
3380 /* pull out the item */
3381 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003382 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3383
3384 /* make sure the item matches what we want */
3385 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3386 break;
David Sterba962a2982014-06-04 18:41:45 +02003387 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003388 break;
3389
3390 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003391 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003392
3393 /*
3394 * this is where we are basically btrfs_lookup, without the
3395 * crossing root thing. we store the inode number in the
3396 * offset of the orphan item.
3397 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003398
3399 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003400 btrfs_err(fs_info,
3401 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003402 ret = -EINVAL;
3403 goto out;
3404 }
3405
3406 last_objectid = found_key.offset;
3407
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003408 found_key.objectid = found_key.offset;
3409 found_key.type = BTRFS_INODE_ITEM_KEY;
3410 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003411 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303412 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003413 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003414 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003415
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003416 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003417 struct btrfs_root *dead_root;
3418 struct btrfs_fs_info *fs_info = root->fs_info;
3419 int is_dead_root = 0;
3420
3421 /*
3422 * this is an orphan in the tree root. Currently these
3423 * could come from 2 sources:
3424 * a) a snapshot deletion in progress
3425 * b) a free space cache inode
3426 * We need to distinguish those two, as the snapshot
3427 * orphan must not get deleted.
3428 * find_dead_roots already ran before us, so if this
3429 * is a snapshot deletion, we should find the root
3430 * in the dead_roots list
3431 */
3432 spin_lock(&fs_info->trans_lock);
3433 list_for_each_entry(dead_root, &fs_info->dead_roots,
3434 root_list) {
3435 if (dead_root->root_key.objectid ==
3436 found_key.objectid) {
3437 is_dead_root = 1;
3438 break;
3439 }
3440 }
3441 spin_unlock(&fs_info->trans_lock);
3442 if (is_dead_root) {
3443 /* prevent this orphan from being found again */
3444 key.offset = found_key.objectid - 1;
3445 continue;
3446 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003447
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003448 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003449
Josef Bacika8c9e572011-09-21 16:55:59 -04003450 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003451 * If we have an inode with links, there are a couple of
3452 * possibilities. Old kernels (before v3.12) used to create an
3453 * orphan item for truncate indicating that there were possibly
3454 * extent items past i_size that needed to be deleted. In v3.12,
3455 * truncate was changed to update i_size in sync with the extent
3456 * items, but the (useless) orphan item was still created. Since
3457 * v4.18, we don't create the orphan item for truncate at all.
3458 *
3459 * So, this item could mean that we need to do a truncate, but
3460 * only if this filesystem was last used on a pre-v3.12 kernel
3461 * and was not cleanly unmounted. The odds of that are quite
3462 * slim, and it's a pain to do the truncate now, so just delete
3463 * the orphan item.
3464 *
3465 * It's also possible that this orphan item was supposed to be
3466 * deleted but wasn't. The inode number may have been reused,
3467 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003468 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003469 if (ret == -ENOENT || inode->i_nlink) {
3470 if (!ret)
3471 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003472 trans = btrfs_start_transaction(root, 1);
3473 if (IS_ERR(trans)) {
3474 ret = PTR_ERR(trans);
3475 goto out;
3476 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003477 btrfs_debug(fs_info, "auto deleting %Lu",
3478 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003479 ret = btrfs_del_orphan_item(trans, root,
3480 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003481 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003482 if (ret)
3483 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003484 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003485 }
Josef Bacik7b128762008-07-24 12:17:14 -04003486
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003487 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003488
3489 /* this will do delete_inode and everything for us */
3490 iput(inode);
3491 }
Miao Xie3254c872011-11-10 20:45:05 -05003492 /* release the path since we're done with it */
3493 btrfs_release_path(path);
3494
Yan, Zhengd68fc572010-05-16 10:49:58 -04003495 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3496
Omar Sandovala575cee2018-05-11 13:13:38 -07003497 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003498 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003499 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003500 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003501 }
Josef Bacik7b128762008-07-24 12:17:14 -04003502
3503 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003504 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003505
3506out:
3507 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003508 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003509 btrfs_free_path(path);
3510 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003511}
3512
Chris Masond352ac62008-09-29 15:18:18 -04003513/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003514 * very simple check to peek ahead in the leaf looking for xattrs. If we
3515 * don't find any xattrs, we know there can't be any acls.
3516 *
3517 * slot is the slot the inode is in, objectid is the objectid of the inode
3518 */
3519static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003520 int slot, u64 objectid,
3521 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003522{
3523 u32 nritems = btrfs_header_nritems(leaf);
3524 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003525 static u64 xattr_access = 0;
3526 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003527 int scanned = 0;
3528
Josef Bacikf23b5a52013-06-19 10:16:26 -04003529 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003530 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3531 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3532 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3533 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003534 }
3535
Chris Mason46a53cc2009-04-27 11:47:50 -04003536 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003537 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003538 while (slot < nritems) {
3539 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3540
3541 /* we found a different objectid, there must not be acls */
3542 if (found_key.objectid != objectid)
3543 return 0;
3544
3545 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003546 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003547 if (*first_xattr_slot == -1)
3548 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003549 if (found_key.offset == xattr_access ||
3550 found_key.offset == xattr_default)
3551 return 1;
3552 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003553
3554 /*
3555 * we found a key greater than an xattr key, there can't
3556 * be any acls later on
3557 */
3558 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3559 return 0;
3560
3561 slot++;
3562 scanned++;
3563
3564 /*
3565 * it goes inode, inode backrefs, xattrs, extents,
3566 * so if there are a ton of hard links to an inode there can
3567 * be a lot of backrefs. Don't waste time searching too hard,
3568 * this is just an optimization
3569 */
3570 if (scanned >= 8)
3571 break;
3572 }
3573 /* we hit the end of the leaf before we found an xattr or
3574 * something larger than an xattr. We have to assume the inode
3575 * has acls
3576 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003577 if (*first_xattr_slot == -1)
3578 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003579 return 1;
3580}
3581
3582/*
Chris Masond352ac62008-09-29 15:18:18 -04003583 * read an inode from the btree into the in-memory inode
3584 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003585static int btrfs_read_locked_inode(struct inode *inode,
3586 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003587{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003588 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003589 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003590 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003591 struct btrfs_inode_item *inode_item;
3592 struct btrfs_root *root = BTRFS_I(inode)->root;
3593 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003594 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003595 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003596 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003597 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003598 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003599 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003600
3601 ret = btrfs_fill_inode(inode, &rdev);
3602 if (!ret)
3603 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003604
Filipe Manana4222ea72018-10-24 10:13:03 +01003605 if (!path) {
3606 path = btrfs_alloc_path();
3607 if (!path)
3608 return -ENOMEM;
3609 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003610
Chris Mason39279cc2007-06-12 06:35:45 -04003611 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003612
Chris Mason39279cc2007-06-12 06:35:45 -04003613 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003614 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003615 if (path != in_path)
3616 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003617 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003618 }
Chris Mason39279cc2007-06-12 06:35:45 -04003619
Chris Mason5f39d392007-10-15 16:14:19 -04003620 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003621
3622 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003623 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003624
Chris Mason5f39d392007-10-15 16:14:19 -04003625 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3626 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003627 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003628 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003629 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3630 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003631 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003632
David Sterbaa937b972014-12-12 17:39:12 +01003633 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3634 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003635
David Sterbaa937b972014-12-12 17:39:12 +01003636 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3637 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003638
David Sterbaa937b972014-12-12 17:39:12 +01003639 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3640 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003641
chandan r9cc97d62012-07-04 12:48:07 +05303642 BTRFS_I(inode)->i_otime.tv_sec =
3643 btrfs_timespec_sec(leaf, &inode_item->otime);
3644 BTRFS_I(inode)->i_otime.tv_nsec =
3645 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003646
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003647 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003648 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003649 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3650
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003651 inode_set_iversion_queried(inode,
3652 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003653 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003654 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003655 rdev = btrfs_inode_rdev(leaf, inode_item);
3656
Josef Bacikaec74772008-07-24 12:12:38 -04003657 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003658 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003659
3660cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003661 /*
3662 * If we were modified in the current generation and evicted from memory
3663 * and then re-read we need to do a full sync since we don't have any
3664 * idea about which extents were modified before we were evicted from
3665 * cache.
3666 *
3667 * This is required for both inode re-read from disk and delayed inode
3668 * in delayed_nodes_tree.
3669 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003670 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003671 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3672 &BTRFS_I(inode)->runtime_flags);
3673
Filipe Mananabde6c242015-07-24 00:00:19 +01003674 /*
3675 * We don't persist the id of the transaction where an unlink operation
3676 * against the inode was last made. So here we assume the inode might
3677 * have been evicted, and therefore the exact value of last_unlink_trans
3678 * lost, and set it to last_trans to avoid metadata inconsistencies
3679 * between the inode and its parent if the inode is fsync'ed and the log
3680 * replayed. For example, in the scenario:
3681 *
3682 * touch mydir/foo
3683 * ln mydir/foo mydir/bar
3684 * sync
3685 * unlink mydir/bar
3686 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3687 * xfs_io -c fsync mydir/foo
3688 * <power failure>
3689 * mount fs, triggers fsync log replay
3690 *
3691 * We must make sure that when we fsync our inode foo we also log its
3692 * parent inode, otherwise after log replay the parent still has the
3693 * dentry with the "bar" name but our inode foo has a link count of 1
3694 * and doesn't have an inode ref with the name "bar" anymore.
3695 *
3696 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003697 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003698 * transaction commits on fsync if our inode is a directory, or if our
3699 * inode is not a directory, logging its parent unnecessarily.
3700 */
3701 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
Filipe Manana41bd6062018-11-28 14:54:28 +00003702 /*
3703 * Similar reasoning for last_link_trans, needs to be set otherwise
3704 * for a case like the following:
3705 *
3706 * mkdir A
3707 * touch foo
3708 * ln foo A/bar
3709 * echo 2 > /proc/sys/vm/drop_caches
3710 * fsync foo
3711 * <power failure>
3712 *
3713 * Would result in link bar and directory A not existing after the power
3714 * failure.
3715 */
3716 BTRFS_I(inode)->last_link_trans = BTRFS_I(inode)->last_trans;
Filipe Mananabde6c242015-07-24 00:00:19 +01003717
Miao Xie67de1172013-12-26 13:07:06 +08003718 path->slots[0]++;
3719 if (inode->i_nlink != 1 ||
3720 path->slots[0] >= btrfs_header_nritems(leaf))
3721 goto cache_acl;
3722
3723 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003724 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003725 goto cache_acl;
3726
3727 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3728 if (location.type == BTRFS_INODE_REF_KEY) {
3729 struct btrfs_inode_ref *ref;
3730
3731 ref = (struct btrfs_inode_ref *)ptr;
3732 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3733 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3734 struct btrfs_inode_extref *extref;
3735
3736 extref = (struct btrfs_inode_extref *)ptr;
3737 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3738 extref);
3739 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003740cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003741 /*
3742 * try to precache a NULL acl entry for files that don't have
3743 * any xattrs or acls
3744 */
Li Zefan33345d012011-04-20 10:31:50 +08003745 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003746 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003747 if (first_xattr_slot != -1) {
3748 path->slots[0] = first_xattr_slot;
3749 ret = btrfs_load_inode_props(inode, path);
3750 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003751 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003752 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003753 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003754 root->root_key.objectid, ret);
3755 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003756 if (path != in_path)
3757 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003758
Al Viro72c04902009-06-24 16:58:48 -04003759 if (!maybe_acls)
3760 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003761
Chris Mason39279cc2007-06-12 06:35:45 -04003762 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003763 case S_IFREG:
3764 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003765 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003766 inode->i_fop = &btrfs_file_operations;
3767 inode->i_op = &btrfs_file_inode_operations;
3768 break;
3769 case S_IFDIR:
3770 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003771 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003772 break;
3773 case S_IFLNK:
3774 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003775 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003776 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003777 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003778 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003779 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003780 init_special_inode(inode, inode->i_mode, rdev);
3781 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003782 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003783
David Sterba7b6a2212018-03-26 18:40:21 +02003784 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003785 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003786}
3787
Chris Masond352ac62008-09-29 15:18:18 -04003788/*
3789 * given a leaf and an inode, copy the inode fields into the leaf
3790 */
Chris Masone02119d2008-09-05 16:13:11 -04003791static void fill_inode_item(struct btrfs_trans_handle *trans,
3792 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003793 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003794 struct inode *inode)
3795{
Liu Bo51fab692012-12-27 09:01:21 +00003796 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003797
Liu Bo51fab692012-12-27 09:01:21 +00003798 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003799
Liu Bo51fab692012-12-27 09:01:21 +00003800 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3801 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3802 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3803 &token);
3804 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3805 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003806
David Sterbaa937b972014-12-12 17:39:12 +01003807 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003808 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003809 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003810 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003811
David Sterbaa937b972014-12-12 17:39:12 +01003812 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003813 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003814 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003815 inode->i_mtime.tv_nsec, &token);
3816
David Sterbaa937b972014-12-12 17:39:12 +01003817 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003818 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003819 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003820 inode->i_ctime.tv_nsec, &token);
3821
chandan r9cc97d62012-07-04 12:48:07 +05303822 btrfs_set_token_timespec_sec(leaf, &item->otime,
3823 BTRFS_I(inode)->i_otime.tv_sec, &token);
3824 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3825 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3826
Liu Bo51fab692012-12-27 09:01:21 +00003827 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3828 &token);
3829 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3830 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003831 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3832 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003833 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3834 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3835 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3836 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003837}
3838
Chris Masond352ac62008-09-29 15:18:18 -04003839/*
3840 * copy everything in the in-memory inode into the btree.
3841 */
Chris Mason21151332011-11-10 20:39:08 -05003842static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003843 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003844{
3845 struct btrfs_inode_item *inode_item;
3846 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003847 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003848 int ret;
3849
3850 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003851 if (!path)
3852 return -ENOMEM;
3853
Chris Masonb9473432009-03-13 11:00:37 -04003854 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003855 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3856 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003857 if (ret) {
3858 if (ret > 0)
3859 ret = -ENOENT;
3860 goto failed;
3861 }
3862
Chris Mason5f39d392007-10-15 16:14:19 -04003863 leaf = path->nodes[0];
3864 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003865 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003866
Chris Masone02119d2008-09-05 16:13:11 -04003867 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003868 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003869 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003870 ret = 0;
3871failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003872 btrfs_free_path(path);
3873 return ret;
3874}
3875
Chris Masond352ac62008-09-29 15:18:18 -04003876/*
Chris Mason21151332011-11-10 20:39:08 -05003877 * copy everything in the in-memory inode into the btree.
3878 */
3879noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3880 struct btrfs_root *root, struct inode *inode)
3881{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003882 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003883 int ret;
3884
3885 /*
3886 * If the inode is a free space inode, we can deadlock during commit
3887 * if we put it into the delayed code.
3888 *
3889 * The data relocation inode should also be directly updated
3890 * without delay
3891 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003892 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003893 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003894 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003895 btrfs_update_root_times(trans, root);
3896
Chris Mason21151332011-11-10 20:39:08 -05003897 ret = btrfs_delayed_update_inode(trans, root, inode);
3898 if (!ret)
3899 btrfs_set_inode_last_trans(trans, inode);
3900 return ret;
3901 }
3902
3903 return btrfs_update_inode_item(trans, root, inode);
3904}
3905
Josef Bacikbe6aef62012-10-22 15:43:12 -04003906noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3907 struct btrfs_root *root,
3908 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003909{
3910 int ret;
3911
3912 ret = btrfs_update_inode(trans, root, inode);
3913 if (ret == -ENOSPC)
3914 return btrfs_update_inode_item(trans, root, inode);
3915 return ret;
3916}
3917
3918/*
Chris Masond352ac62008-09-29 15:18:18 -04003919 * unlink helper that gets used here in inode.c and in the tree logging
3920 * recovery code. It remove a link in a directory with a given name, and
3921 * also drops the back refs in the inode to the directory
3922 */
Al Viro92986792011-03-04 17:14:37 +00003923static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3924 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003925 struct btrfs_inode *dir,
3926 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003927 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003928{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003929 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003930 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003931 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003932 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003933 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003934 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003935 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003936 u64 ino = btrfs_ino(inode);
3937 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003938
3939 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003940 if (!path) {
3941 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003942 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003943 }
3944
Chris Masonb9473432009-03-13 11:00:37 -04003945 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003946 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003947 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003948 if (IS_ERR_OR_NULL(di)) {
3949 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003950 goto err;
3951 }
Chris Mason5f39d392007-10-15 16:14:19 -04003952 leaf = path->nodes[0];
3953 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003954 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003955 if (ret)
3956 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003957 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003958
Miao Xie67de1172013-12-26 13:07:06 +08003959 /*
3960 * If we don't have dir index, we have to get it by looking up
3961 * the inode ref, since we get the inode ref, remove it directly,
3962 * it is unnecessary to do delayed deletion.
3963 *
3964 * But if we have dir index, needn't search inode ref to get it.
3965 * Since the inode ref is close to the inode item, it is better
3966 * that we delay to delete it, and just do this deletion when
3967 * we update the inode item.
3968 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003969 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003970 ret = btrfs_delayed_delete_inode_ref(inode);
3971 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003972 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003973 goto skip_backref;
3974 }
3975 }
3976
Li Zefan33345d012011-04-20 10:31:50 +08003977 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3978 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003979 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003980 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003981 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003982 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003983 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003984 goto err;
3985 }
Miao Xie67de1172013-12-26 13:07:06 +08003986skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003987 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003988 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003989 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003990 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003991 }
Chris Mason39279cc2007-06-12 06:35:45 -04003992
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003993 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3994 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003995 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003996 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003997 goto err;
3998 }
Chris Masone02119d2008-09-05 16:13:11 -04003999
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004000 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4001 index);
Chris Mason6418c962010-10-30 07:34:24 -04004002 if (ret == -ENOENT)
4003 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004004 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004005 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004006err:
4007 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004008 if (ret)
4009 goto out;
4010
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004011 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004012 inode_inc_iversion(&inode->vfs_inode);
4013 inode_inc_iversion(&dir->vfs_inode);
4014 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4015 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4016 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004017out:
Chris Mason39279cc2007-06-12 06:35:45 -04004018 return ret;
4019}
4020
Al Viro92986792011-03-04 17:14:37 +00004021int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4022 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004023 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004024 const char *name, int name_len)
4025{
4026 int ret;
4027 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4028 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004029 drop_nlink(&inode->vfs_inode);
4030 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004031 }
4032 return ret;
4033}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004034
4035/*
4036 * helper to start transaction for unlink and rmdir.
4037 *
Josef Bacikd52be812013-05-29 14:54:47 -04004038 * unlink and rmdir are special in btrfs, they do not always free space, so
4039 * if we cannot make our reservations the normal way try and see if there is
4040 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4041 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004042 */
Josef Bacikd52be812013-05-29 14:54:47 -04004043static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004044{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004046
Josef Bacike70bea52011-10-11 14:18:24 -04004047 /*
4048 * 1 for the possible orphan item
4049 * 1 for the dir item
4050 * 1 for the dir index
4051 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004052 * 1 for the inode
4053 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004054 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004055}
4056
Chris Mason39279cc2007-06-12 06:35:45 -04004057static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4058{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004059 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004060 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004061 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004062 int ret;
4063
Josef Bacikd52be812013-05-29 14:54:47 -04004064 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004065 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004066 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004067
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004068 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4069 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004070
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004071 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4072 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4073 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004074 if (ret)
4075 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004076
Yan, Zhenga22285a2010-05-16 10:48:46 -04004077 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004078 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004079 if (ret)
4080 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004081 }
Josef Bacik7b128762008-07-24 12:17:14 -04004082
Tsutomu Itohb5324022011-07-19 07:27:20 +00004083out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004084 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004085 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004086 return ret;
4087}
4088
Misono Tomohirof60a2362018-04-18 11:34:52 +09004089static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004090 struct inode *dir, u64 objectid,
4091 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004092{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004093 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004094 struct btrfs_path *path;
4095 struct extent_buffer *leaf;
4096 struct btrfs_dir_item *di;
4097 struct btrfs_key key;
4098 u64 index;
4099 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004100 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004101
4102 path = btrfs_alloc_path();
4103 if (!path)
4104 return -ENOMEM;
4105
Li Zefan33345d012011-04-20 10:31:50 +08004106 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004107 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004108 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004109 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004110 goto out;
4111 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112
4113 leaf = path->nodes[0];
4114 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4115 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4116 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004117 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004118 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004119 goto out;
4120 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004121 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004122
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004123 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4124 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004125 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004126 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004127 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004128 goto out;
4129 }
Li Zefan33345d012011-04-20 10:31:50 +08004130 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004131 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004132 if (IS_ERR_OR_NULL(di)) {
4133 if (!di)
4134 ret = -ENOENT;
4135 else
4136 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004137 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004138 goto out;
4139 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004140
4141 leaf = path->nodes[0];
4142 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004143 index = key.offset;
4144 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004145 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004146
Lu Fengqi9add2942018-08-01 11:32:26 +08004147 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004149 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004150 goto out;
4151 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004152
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004153 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004154 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004155 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004156 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004157 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004158 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004159out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004160 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004161 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004162}
4163
Misono Tomohiroec42f162018-04-18 11:34:13 +09004164/*
4165 * Helper to check if the subvolume references other subvolumes or if it's
4166 * default.
4167 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004168static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004169{
4170 struct btrfs_fs_info *fs_info = root->fs_info;
4171 struct btrfs_path *path;
4172 struct btrfs_dir_item *di;
4173 struct btrfs_key key;
4174 u64 dir_id;
4175 int ret;
4176
4177 path = btrfs_alloc_path();
4178 if (!path)
4179 return -ENOMEM;
4180
4181 /* Make sure this root isn't set as the default subvol */
4182 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4183 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4184 dir_id, "default", 7, 0);
4185 if (di && !IS_ERR(di)) {
4186 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4187 if (key.objectid == root->root_key.objectid) {
4188 ret = -EPERM;
4189 btrfs_err(fs_info,
4190 "deleting default subvolume %llu is not allowed",
4191 key.objectid);
4192 goto out;
4193 }
4194 btrfs_release_path(path);
4195 }
4196
4197 key.objectid = root->root_key.objectid;
4198 key.type = BTRFS_ROOT_REF_KEY;
4199 key.offset = (u64)-1;
4200
4201 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4202 if (ret < 0)
4203 goto out;
4204 BUG_ON(ret == 0);
4205
4206 ret = 0;
4207 if (path->slots[0] > 0) {
4208 path->slots[0]--;
4209 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4210 if (key.objectid == root->root_key.objectid &&
4211 key.type == BTRFS_ROOT_REF_KEY)
4212 ret = -ENOTEMPTY;
4213 }
4214out:
4215 btrfs_free_path(path);
4216 return ret;
4217}
4218
Nikolay Borisov20a68002018-04-27 14:36:24 +03004219/* Delete all dentries for inodes belonging to the root */
4220static void btrfs_prune_dentries(struct btrfs_root *root)
4221{
4222 struct btrfs_fs_info *fs_info = root->fs_info;
4223 struct rb_node *node;
4224 struct rb_node *prev;
4225 struct btrfs_inode *entry;
4226 struct inode *inode;
4227 u64 objectid = 0;
4228
4229 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4230 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4231
4232 spin_lock(&root->inode_lock);
4233again:
4234 node = root->inode_tree.rb_node;
4235 prev = NULL;
4236 while (node) {
4237 prev = node;
4238 entry = rb_entry(node, struct btrfs_inode, rb_node);
4239
David Sterba37508512018-06-29 10:56:40 +02004240 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004241 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004242 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004243 node = node->rb_right;
4244 else
4245 break;
4246 }
4247 if (!node) {
4248 while (prev) {
4249 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004250 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004251 node = prev;
4252 break;
4253 }
4254 prev = rb_next(prev);
4255 }
4256 }
4257 while (node) {
4258 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004259 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004260 inode = igrab(&entry->vfs_inode);
4261 if (inode) {
4262 spin_unlock(&root->inode_lock);
4263 if (atomic_read(&inode->i_count) > 1)
4264 d_prune_aliases(inode);
4265 /*
4266 * btrfs_drop_inode will have it removed from the inode
4267 * cache when its usage count hits zero.
4268 */
4269 iput(inode);
4270 cond_resched();
4271 spin_lock(&root->inode_lock);
4272 goto again;
4273 }
4274
4275 if (cond_resched_lock(&root->inode_lock))
4276 goto again;
4277
4278 node = rb_next(node);
4279 }
4280 spin_unlock(&root->inode_lock);
4281}
4282
Misono Tomohirof60a2362018-04-18 11:34:52 +09004283int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4284{
4285 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4286 struct btrfs_root *root = BTRFS_I(dir)->root;
4287 struct inode *inode = d_inode(dentry);
4288 struct btrfs_root *dest = BTRFS_I(inode)->root;
4289 struct btrfs_trans_handle *trans;
4290 struct btrfs_block_rsv block_rsv;
4291 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004292 int ret;
4293 int err;
4294
4295 /*
4296 * Don't allow to delete a subvolume with send in progress. This is
4297 * inside the inode lock so the error handling that has to drop the bit
4298 * again is not run concurrently.
4299 */
4300 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004301 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004302 spin_unlock(&dest->root_item_lock);
4303 btrfs_warn(fs_info,
4304 "attempt to delete subvolume %llu during send",
4305 dest->root_key.objectid);
4306 return -EPERM;
4307 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004308 root_flags = btrfs_root_flags(&dest->root_item);
4309 btrfs_set_root_flags(&dest->root_item,
4310 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4311 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004312
4313 down_write(&fs_info->subvol_sem);
4314
4315 err = may_destroy_subvol(dest);
4316 if (err)
4317 goto out_up_write;
4318
4319 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4320 /*
4321 * One for dir inode,
4322 * two for dir entries,
4323 * two for root ref/backref.
4324 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004325 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004326 if (err)
4327 goto out_up_write;
4328
4329 trans = btrfs_start_transaction(root, 0);
4330 if (IS_ERR(trans)) {
4331 err = PTR_ERR(trans);
4332 goto out_release;
4333 }
4334 trans->block_rsv = &block_rsv;
4335 trans->bytes_reserved = block_rsv.size;
4336
4337 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4338
Lu Fengqi401b3b12018-08-01 11:32:30 +08004339 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4340 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004341 if (ret) {
4342 err = ret;
4343 btrfs_abort_transaction(trans, ret);
4344 goto out_end_trans;
4345 }
4346
4347 btrfs_record_root_in_trans(trans, dest);
4348
4349 memset(&dest->root_item.drop_progress, 0,
4350 sizeof(dest->root_item.drop_progress));
4351 dest->root_item.drop_level = 0;
4352 btrfs_set_root_refs(&dest->root_item, 0);
4353
4354 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4355 ret = btrfs_insert_orphan_item(trans,
4356 fs_info->tree_root,
4357 dest->root_key.objectid);
4358 if (ret) {
4359 btrfs_abort_transaction(trans, ret);
4360 err = ret;
4361 goto out_end_trans;
4362 }
4363 }
4364
Lu Fengqid1957792018-05-29 15:01:54 +08004365 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004366 BTRFS_UUID_KEY_SUBVOL,
4367 dest->root_key.objectid);
4368 if (ret && ret != -ENOENT) {
4369 btrfs_abort_transaction(trans, ret);
4370 err = ret;
4371 goto out_end_trans;
4372 }
4373 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004374 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004375 dest->root_item.received_uuid,
4376 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4377 dest->root_key.objectid);
4378 if (ret && ret != -ENOENT) {
4379 btrfs_abort_transaction(trans, ret);
4380 err = ret;
4381 goto out_end_trans;
4382 }
4383 }
4384
4385out_end_trans:
4386 trans->block_rsv = NULL;
4387 trans->bytes_reserved = 0;
4388 ret = btrfs_end_transaction(trans);
4389 if (ret && !err)
4390 err = ret;
4391 inode->i_flags |= S_DEAD;
4392out_release:
4393 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4394out_up_write:
4395 up_write(&fs_info->subvol_sem);
4396 if (err) {
4397 spin_lock(&dest->root_item_lock);
4398 root_flags = btrfs_root_flags(&dest->root_item);
4399 btrfs_set_root_flags(&dest->root_item,
4400 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4401 spin_unlock(&dest->root_item_lock);
4402 } else {
4403 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004404 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004405 ASSERT(dest->send_in_progress == 0);
4406
4407 /* the last ref */
4408 if (dest->ino_cache_inode) {
4409 iput(dest->ino_cache_inode);
4410 dest->ino_cache_inode = NULL;
4411 }
4412 }
4413
4414 return err;
4415}
4416
Chris Mason39279cc2007-06-12 06:35:45 -04004417static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4418{
David Howells2b0143b2015-03-17 22:25:59 +00004419 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004420 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004421 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004422 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004423 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004424
David Sterbab3ae2442012-09-13 16:04:34 -06004425 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004426 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004427 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004428 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004429
Josef Bacikd52be812013-05-29 14:54:47 -04004430 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004431 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004432 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004433
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004434 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004435 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004436 BTRFS_I(inode)->location.objectid,
4437 dentry->d_name.name,
4438 dentry->d_name.len);
4439 goto out;
4440 }
4441
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004442 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004443 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004444 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004445
Filipe Manana44f714d2016-06-06 16:11:13 +01004446 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4447
Chris Mason39279cc2007-06-12 06:35:45 -04004448 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004449 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4450 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4451 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004452 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004453 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004454 /*
4455 * Propagate the last_unlink_trans value of the deleted dir to
4456 * its parent directory. This is to prevent an unrecoverable
4457 * log tree in the case we do something like this:
4458 * 1) create dir foo
4459 * 2) create snapshot under dir foo
4460 * 3) delete the snapshot
4461 * 4) rmdir foo
4462 * 5) mkdir foo
4463 * 6) fsync foo or some file inside foo
4464 */
4465 if (last_unlink_trans >= trans->transid)
4466 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4467 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004468out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004469 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004470 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004471
Chris Mason39279cc2007-06-12 06:35:45 -04004472 return err;
4473}
4474
Josef Bacikddfae632017-10-19 14:16:02 -04004475/*
4476 * Return this if we need to call truncate_block for the last bit of the
4477 * truncate.
4478 */
4479#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004480
Chris Mason323ac952008-10-01 19:05:46 -04004481/*
Chris Mason39279cc2007-06-12 06:35:45 -04004482 * this can truncate away extent items, csum items and directory items.
4483 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004484 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004485 *
4486 * csum items that cross the new i_size are truncated to the new size
4487 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004488 *
4489 * min_type is the minimum key type to truncate down to. If set to 0, this
4490 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004491 */
Yan, Zheng80825102009-11-12 09:35:36 +00004492int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4493 struct btrfs_root *root,
4494 struct inode *inode,
4495 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004496{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004497 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004498 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004499 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004500 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004501 struct btrfs_key key;
4502 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004503 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004504 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004505 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004506 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004507 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004508 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004509 int found_extent;
4510 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004511 int pending_del_nr = 0;
4512 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004513 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004514 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004515 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004516 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004517 bool be_nice = false;
4518 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004519
4520 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004521
Chris Mason28ed1342014-12-17 09:41:04 -08004522 /*
4523 * for non-free space inodes and ref cows, we want to back off from
4524 * time to time
4525 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004526 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004527 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004528 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004529
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004530 path = btrfs_alloc_path();
4531 if (!path)
4532 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004533 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004534
Josef Bacik5dc562c2012-08-17 13:14:17 -04004535 /*
4536 * We want to drop from the next block forward in case this new size is
4537 * not block aligned since we will be keeping the last block of the
4538 * extent just the way it is.
4539 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004540 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004541 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004542 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004543 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004544 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004545
Miao Xie16cdcec2011-04-22 18:12:22 +08004546 /*
4547 * This function is also used to drop the items in the log tree before
4548 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004549 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004550 * items.
4551 */
4552 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004553 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004554
Li Zefan33345d012011-04-20 10:31:50 +08004555 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004556 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004557 key.type = (u8)-1;
4558
Chris Mason85e21ba2008-01-29 15:11:36 -05004559search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004560 /*
4561 * with a 16K leaf size and 128MB extents, you can actually queue
4562 * up a huge file in a single leaf. Most of the time that
4563 * bytes_deleted is > 0, it will be huge by the time we get here
4564 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004565 if (be_nice && bytes_deleted > SZ_32M &&
4566 btrfs_should_end_transaction(trans)) {
4567 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004568 goto out;
4569 }
Chris Masond3977122009-01-05 21:25:51 -05004570
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004571 path->leave_spinning = 1;
4572 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4573 if (ret < 0)
4574 goto out;
4575
Chris Mason85e21ba2008-01-29 15:11:36 -05004576 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004577 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004578 /* there are no items in the tree for us to truncate, we're
4579 * done
4580 */
Yan, Zheng80825102009-11-12 09:35:36 +00004581 if (path->slots[0] == 0)
4582 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004583 path->slots[0]--;
4584 }
4585
Chris Masond3977122009-01-05 21:25:51 -05004586 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004587 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004588 leaf = path->nodes[0];
4589 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004590 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004591
Li Zefan33345d012011-04-20 10:31:50 +08004592 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004593 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004594
Chris Mason85e21ba2008-01-29 15:11:36 -05004595 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004596 break;
4597
Chris Mason5f39d392007-10-15 16:14:19 -04004598 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004599 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004600 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004601 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004602 extent_type = btrfs_file_extent_type(leaf, fi);
4603 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004604 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004605 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004606
4607 trace_btrfs_truncate_show_fi_regular(
4608 BTRFS_I(inode), leaf, fi,
4609 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004610 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004611 item_end += btrfs_file_extent_ram_bytes(leaf,
4612 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004613
4614 trace_btrfs_truncate_show_fi_inline(
4615 BTRFS_I(inode), leaf, fi, path->slots[0],
4616 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004617 }
Yan008630c2007-11-07 13:31:09 -05004618 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004619 }
Yan, Zheng80825102009-11-12 09:35:36 +00004620 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004621 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004622 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004623 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004624 break;
4625 if (found_key.offset >= new_size)
4626 del_item = 1;
4627 else
4628 del_item = 0;
4629 }
Chris Mason39279cc2007-06-12 06:35:45 -04004630 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004631 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004632 if (found_type != BTRFS_EXTENT_DATA_KEY)
4633 goto delete;
4634
4635 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004636 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004637 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004638 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004639 u64 orig_num_bytes =
4640 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004641 extent_num_bytes = ALIGN(new_size -
4642 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004643 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004644 btrfs_set_file_extent_num_bytes(leaf, fi,
4645 extent_num_bytes);
4646 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004647 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004648 if (test_bit(BTRFS_ROOT_REF_COWS,
4649 &root->state) &&
4650 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004651 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004652 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004653 } else {
Chris Masondb945352007-10-15 16:15:53 -04004654 extent_num_bytes =
4655 btrfs_file_extent_disk_num_bytes(leaf,
4656 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004657 extent_offset = found_key.offset -
4658 btrfs_file_extent_offset(leaf, fi);
4659
Chris Mason39279cc2007-06-12 06:35:45 -04004660 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004661 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004662 if (extent_start != 0) {
4663 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004664 if (test_bit(BTRFS_ROOT_REF_COWS,
4665 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004666 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004667 }
4668 }
Chris Mason90692182008-02-08 13:49:28 -05004669 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004670 /*
4671 * we can't truncate inline items that have had
4672 * special encodings
4673 */
4674 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004675 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004676 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4677 btrfs_file_extent_compression(leaf, fi) == 0) {
4678 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004679
Josef Bacikddfae632017-10-19 14:16:02 -04004680 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4681 size = btrfs_file_extent_calc_inline_size(size);
4682 btrfs_truncate_item(root->fs_info, path, size, 1);
4683 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004684 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004685 * We have to bail so the last_size is set to
4686 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004687 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004688 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004689 break;
Chris Mason90692182008-02-08 13:49:28 -05004690 }
Josef Bacikddfae632017-10-19 14:16:02 -04004691
4692 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4693 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004694 }
Chris Mason179e29e2007-11-01 11:28:41 -04004695delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004696 if (del_item)
4697 last_size = found_key.offset;
4698 else
4699 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004700 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004701 if (!pending_del_nr) {
4702 /* no pending yet, add ourselves */
4703 pending_del_slot = path->slots[0];
4704 pending_del_nr = 1;
4705 } else if (pending_del_nr &&
4706 path->slots[0] + 1 == pending_del_slot) {
4707 /* hop on the pending chunk */
4708 pending_del_nr++;
4709 pending_del_slot = path->slots[0];
4710 } else {
Chris Masond3977122009-01-05 21:25:51 -05004711 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004712 }
Chris Mason39279cc2007-06-12 06:35:45 -04004713 } else {
4714 break;
4715 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004716 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004717
Miao Xie27cdeb72014-04-02 19:51:05 +08004718 if (found_extent &&
4719 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004720 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004721 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004722 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004723 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004724 extent_num_bytes, 0,
4725 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004726 ino, extent_offset);
Omar Sandoval05522102018-05-11 13:13:31 -07004727 if (ret) {
4728 btrfs_abort_transaction(trans, ret);
4729 break;
4730 }
Chris Mason28f75a02015-02-04 06:59:29 -08004731 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004732 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004733 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004734 }
Chris Mason39279cc2007-06-12 06:35:45 -04004735 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004736
Yan, Zheng80825102009-11-12 09:35:36 +00004737 if (found_type == BTRFS_INODE_ITEM_KEY)
4738 break;
4739
4740 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004741 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004742 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004743 if (pending_del_nr) {
4744 ret = btrfs_del_items(trans, root, path,
4745 pending_del_slot,
4746 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004747 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004748 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004749 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004750 }
Yan, Zheng80825102009-11-12 09:35:36 +00004751 pending_del_nr = 0;
4752 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004753 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004754
Chris Mason28f75a02015-02-04 06:59:29 -08004755 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004756 * We can generate a lot of delayed refs, so we need to
4757 * throttle every once and a while and make sure we're
4758 * adding enough space to keep up with the work we are
4759 * generating. Since we hold a transaction here we
4760 * can't flush, and we don't want to FLUSH_LIMIT because
4761 * we could have generated too many delayed refs to
4762 * actually allocate, so just bail if we're short and
4763 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004764 */
Josef Bacik28bad212018-12-03 10:20:38 -05004765 if (should_throttle) {
4766 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4767 BTRFS_RESERVE_NO_FLUSH);
4768 if (ret) {
4769 ret = -EAGAIN;
4770 break;
4771 }
Chris Mason28f75a02015-02-04 06:59:29 -08004772 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004773 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004774 } else {
4775 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004776 }
Chris Mason39279cc2007-06-12 06:35:45 -04004777 }
Yan, Zheng80825102009-11-12 09:35:36 +00004778out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004779 if (ret >= 0 && pending_del_nr) {
4780 int err;
4781
4782 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004783 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004784 if (err) {
4785 btrfs_abort_transaction(trans, err);
4786 ret = err;
4787 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004788 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004789 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4790 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004791 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004792 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004793 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004794 }
Chris Mason28ed1342014-12-17 09:41:04 -08004795
Chris Mason39279cc2007-06-12 06:35:45 -04004796 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004797 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004798}
4799
Chris Masona52d9a82007-08-27 16:49:44 -04004800/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304801 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004802 * @inode - inode that we're zeroing
4803 * @from - the offset to start zeroing
4804 * @len - the length to zero, 0 to zero the entire range respective to the
4805 * offset
4806 * @front - zero up to the offset instead of from the offset on
4807 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304808 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004809 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004810 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304811int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004812 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004813{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004814 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004815 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004816 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4817 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004818 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004819 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004820 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004821 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004822 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304823 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004824 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004825 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004826 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304827 u64 block_start;
4828 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004829
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004830 if (IS_ALIGNED(offset, blocksize) &&
4831 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004832 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304833
Josef Bacik8b62f872017-10-19 14:15:55 -04004834 block_start = round_down(from, blocksize);
4835 block_end = block_start + blocksize - 1;
4836
Qu Wenruo364ecf32017-02-27 15:10:38 +08004837 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004838 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004839 if (ret)
4840 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004841
Chris Mason211c17f2008-05-15 09:13:45 -04004842again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004843 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004844 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004845 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004846 block_start, blocksize, true);
4847 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004848 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004849 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004850 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004851
Chris Masona52d9a82007-08-27 16:49:44 -04004852 if (!PageUptodate(page)) {
4853 ret = btrfs_readpage(NULL, page);
4854 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004855 if (page->mapping != mapping) {
4856 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004857 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004858 goto again;
4859 }
Chris Masona52d9a82007-08-27 16:49:44 -04004860 if (!PageUptodate(page)) {
4861 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004862 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004863 }
4864 }
Chris Mason211c17f2008-05-15 09:13:45 -04004865 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004866
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304867 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004868 set_page_extent_mapped(page);
4869
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304870 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004871 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304872 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004873 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004874 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004875 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004876 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004877 btrfs_put_ordered_extent(ordered);
4878 goto again;
4879 }
4880
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304881 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004882 EXTENT_DIRTY | EXTENT_DELALLOC |
4883 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004884 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004885
Filipe Mananae3b8a482017-11-04 00:16:59 +00004886 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004887 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004888 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304889 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004890 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004891 goto out_unlock;
4892 }
4893
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304894 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004895 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304896 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004897 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004898 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304899 memset(kaddr + (block_start - page_offset(page)),
4900 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004901 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304902 memset(kaddr + (block_start - page_offset(page)) + offset,
4903 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004904 flush_dcache_page(page);
4905 kunmap(page);
4906 }
Chris Mason247e7432008-07-17 12:53:51 -04004907 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004908 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004909 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004910
Chris Mason89642222008-07-24 09:41:53 -04004911out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004912 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004913 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004914 blocksize, true);
4915 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004916 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004917 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004918out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004919 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004920 return ret;
4921}
4922
Josef Bacik16e75492013-10-22 12:18:51 -04004923static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4924 u64 offset, u64 len)
4925{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004926 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004927 struct btrfs_trans_handle *trans;
4928 int ret;
4929
4930 /*
4931 * Still need to make sure the inode looks like it's been updated so
4932 * that any holes get logged if we fsync.
4933 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004934 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4935 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004936 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4937 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4938 return 0;
4939 }
4940
4941 /*
4942 * 1 - for the one we're dropping
4943 * 1 - for the one we're adding
4944 * 1 - for updating the inode.
4945 */
4946 trans = btrfs_start_transaction(root, 3);
4947 if (IS_ERR(trans))
4948 return PTR_ERR(trans);
4949
4950 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4951 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004952 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004953 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004954 return ret;
4955 }
4956
David Sterbaf85b7372017-01-20 14:54:07 +01004957 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4958 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004959 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004960 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004961 else
4962 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004963 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004964 return ret;
4965}
4966
Josef Bacik695a0d02011-03-04 15:46:53 -05004967/*
4968 * This function puts in dummy file extents for the area we're creating a hole
4969 * for. So if we are truncating this file to a larger size we need to insert
4970 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4971 * the range between oldsize and size
4972 */
Josef Bacika41ad392011-01-31 15:30:16 -05004973int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004974{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004975 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004976 struct btrfs_root *root = BTRFS_I(inode)->root;
4977 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004978 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004979 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004980 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004981 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4982 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004983 u64 last_byte;
4984 u64 cur_offset;
4985 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004986 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004987
Josef Bacika71754f2013-06-17 17:14:39 -04004988 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304989 * If our size started in the middle of a block we need to zero out the
4990 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004991 * expose stale data.
4992 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304993 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004994 if (err)
4995 return err;
4996
Yan Zheng9036c102008-10-30 14:19:41 -04004997 if (size <= hole_start)
4998 return 0;
4999
Yan Zheng9036c102008-10-30 14:19:41 -04005000 while (1) {
5001 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08005002
David Sterbaff13db42015-12-03 14:30:40 +01005003 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005004 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02005005 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08005006 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04005007 if (!ordered)
5008 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005009 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01005010 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08005011 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04005012 btrfs_put_ordered_extent(ordered);
5013 }
5014
Yan Zheng9036c102008-10-30 14:19:41 -04005015 cur_offset = hole_start;
5016 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02005017 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04005018 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005019 if (IS_ERR(em)) {
5020 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00005021 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005022 break;
5023 }
Yan Zheng9036c102008-10-30 14:19:41 -04005024 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005025 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005026 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005027 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005028 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005029
Josef Bacik16e75492013-10-22 12:18:51 -04005030 err = maybe_insert_hole(root, inode, cur_offset,
5031 hole_size);
5032 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005033 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005034 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005035 cur_offset + hole_size - 1, 0);
5036 hole_em = alloc_extent_map();
5037 if (!hole_em) {
5038 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5039 &BTRFS_I(inode)->runtime_flags);
5040 goto next;
5041 }
5042 hole_em->start = cur_offset;
5043 hole_em->len = hole_size;
5044 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005045
Josef Bacik5dc562c2012-08-17 13:14:17 -04005046 hole_em->block_start = EXTENT_MAP_HOLE;
5047 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005048 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005049 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005050 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005051 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005052 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005053
5054 while (1) {
5055 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005056 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005057 write_unlock(&em_tree->lock);
5058 if (err != -EEXIST)
5059 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005060 btrfs_drop_extent_cache(BTRFS_I(inode),
5061 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005062 cur_offset +
5063 hole_size - 1, 0);
5064 }
5065 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005066 }
Josef Bacik16e75492013-10-22 12:18:51 -04005067next:
Yan Zheng9036c102008-10-30 14:19:41 -04005068 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005069 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005070 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005071 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005072 break;
5073 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005074 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005075 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005076 return err;
5077}
5078
Eric Sandeen3972f262013-01-12 02:57:22 +00005079static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005080{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005081 struct btrfs_root *root = BTRFS_I(inode)->root;
5082 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005083 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005084 loff_t newsize = attr->ia_size;
5085 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005086 int ret;
5087
Eric Sandeen3972f262013-01-12 02:57:22 +00005088 /*
5089 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5090 * special case where we need to update the times despite not having
5091 * these flags set. For all other operations the VFS set these flags
5092 * explicitly if it wants a timestamp update.
5093 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005094 if (newsize != oldsize) {
5095 inode_inc_iversion(inode);
5096 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5097 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005098 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005099 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005100
Josef Bacika41ad392011-01-31 15:30:16 -05005101 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005102 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005103 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005104 * This is to ensure the snapshot captures a fully consistent
5105 * state of this file - if the snapshot captures this expanding
5106 * truncation, it must capture all writes that happened before
5107 * this truncation.
5108 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005109 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005110 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005111 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005112 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005113 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005114 }
Yan, Zheng80825102009-11-12 09:35:36 +00005115
Miao Xief4a2f4c2011-12-14 20:12:01 -05005116 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005117 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005118 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005119 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005120 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005121
5122 i_size_write(inode, newsize);
5123 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305124 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005125 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005126 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005127 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005128 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005129
Josef Bacika41ad392011-01-31 15:30:16 -05005130 /*
5131 * We're truncating a file that used to have good data down to
5132 * zero. Make sure it gets into the ordered flush list so that
5133 * any new writes get down to disk quickly.
5134 */
5135 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005136 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5137 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005138
Josef Bacika41ad392011-01-31 15:30:16 -05005139 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005140
Andrea Gelmini52042d82018-11-28 12:05:13 +01005141 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005142 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005143 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005144 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005145
Filipe Manana213e8c52018-02-06 20:40:31 +00005146 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005147 if (ret && inode->i_nlink) {
5148 int err;
5149
5150 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005151 * Truncate failed, so fix up the in-memory size. We
5152 * adjusted disk_i_size down as we removed extents, so
5153 * wait for disk_i_size to be stable and then update the
5154 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005155 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005156 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005157 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005158 return err;
5159 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005160 }
Yan, Zheng80825102009-11-12 09:35:36 +00005161 }
5162
Josef Bacika41ad392011-01-31 15:30:16 -05005163 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005164}
5165
Chris Mason39279cc2007-06-12 06:35:45 -04005166static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5167{
David Howells2b0143b2015-03-17 22:25:59 +00005168 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005169 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005170 int err;
5171
Li Zefanb83cc962010-12-20 16:04:08 +08005172 if (btrfs_root_readonly(root))
5173 return -EROFS;
5174
Jan Kara31051c82016-05-26 16:55:18 +02005175 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005176 if (err)
5177 return err;
5178
Chris Mason5a3f23d2009-03-31 13:27:11 -04005179 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005180 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005181 if (err)
5182 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005183 }
Yan Zheng9036c102008-10-30 14:19:41 -04005184
Christoph Hellwig10257742010-06-04 11:30:02 +02005185 if (attr->ia_valid) {
5186 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005187 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005188 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005189
Josef Bacik22c44fe2011-11-30 10:45:38 -05005190 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005191 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005192 }
5193
Chris Mason39279cc2007-06-12 06:35:45 -04005194 return err;
5195}
Chris Mason61295eb2008-01-14 16:24:38 -05005196
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005197/*
5198 * While truncating the inode pages during eviction, we get the VFS calling
5199 * btrfs_invalidatepage() against each page of the inode. This is slow because
5200 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5201 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5202 * extent_state structures over and over, wasting lots of time.
5203 *
5204 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5205 * those expensive operations on a per page basis and do only the ordered io
5206 * finishing, while we release here the extent_map and extent_state structures,
5207 * without the excessive merging and splitting.
5208 */
5209static void evict_inode_truncate_pages(struct inode *inode)
5210{
5211 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5212 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5213 struct rb_node *node;
5214
5215 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005216 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005217
5218 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005219 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005220 struct extent_map *em;
5221
Liu Bo07e1ce02018-08-23 03:51:52 +08005222 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005223 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005224 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5225 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005226 remove_extent_mapping(map_tree, em);
5227 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005228 if (need_resched()) {
5229 write_unlock(&map_tree->lock);
5230 cond_resched();
5231 write_lock(&map_tree->lock);
5232 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005233 }
5234 write_unlock(&map_tree->lock);
5235
Filipe Manana6ca07092015-05-26 00:55:42 +01005236 /*
5237 * Keep looping until we have no more ranges in the io tree.
5238 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005239 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5240 * still in progress (unlocked the pages in the bio but did not yet
5241 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005242 * ranges can still be locked and eviction started because before
5243 * submitting those bios, which are executed by a separate task (work
5244 * queue kthread), inode references (inode->i_count) were not taken
5245 * (which would be dropped in the end io callback of each bio).
5246 * Therefore here we effectively end up waiting for those bios and
5247 * anyone else holding locked ranges without having bumped the inode's
5248 * reference count - if we don't do it, when they access the inode's
5249 * io_tree to unlock a range it may be too late, leading to an
5250 * use-after-free issue.
5251 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005252 spin_lock(&io_tree->lock);
5253 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5254 struct extent_state *state;
5255 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005256 u64 start;
5257 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005258 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005259
5260 node = rb_first(&io_tree->state);
5261 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005262 start = state->start;
5263 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005264 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005265 spin_unlock(&io_tree->lock);
5266
David Sterbaff13db42015-12-03 14:30:40 +01005267 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005268
5269 /*
5270 * If still has DELALLOC flag, the extent didn't reach disk,
5271 * and its reserved space won't be freed by delayed_ref.
5272 * So we need to free its reserved space here.
5273 * (Refer to comment in btrfs_invalidatepage, case 2)
5274 *
5275 * Note, end is the bytenr of last byte, so we need + 1 here.
5276 */
Filipe Manana421f0922018-10-12 13:02:48 +01005277 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005278 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005279
Filipe Manana6ca07092015-05-26 00:55:42 +01005280 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005281 EXTENT_LOCKED | EXTENT_DIRTY |
5282 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005283 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005284
Filipe Manana7064dd52014-08-08 02:47:05 +01005285 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005286 spin_lock(&io_tree->lock);
5287 }
5288 spin_unlock(&io_tree->lock);
5289}
5290
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005291static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005292 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005293{
5294 struct btrfs_fs_info *fs_info = root->fs_info;
5295 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik260e7702018-11-21 14:03:13 -05005296 u64 delayed_refs_extra = btrfs_calc_trans_metadata_size(fs_info, 1);
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005297 int failures = 0;
5298
5299 for (;;) {
5300 struct btrfs_trans_handle *trans;
5301 int ret;
5302
Josef Bacik260e7702018-11-21 14:03:13 -05005303 ret = btrfs_block_rsv_refill(root, rsv,
5304 rsv->size + delayed_refs_extra,
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005305 BTRFS_RESERVE_FLUSH_LIMIT);
5306
5307 if (ret && ++failures > 2) {
5308 btrfs_warn(fs_info,
5309 "could not allocate space for a delete; will truncate on mount");
5310 return ERR_PTR(-ENOSPC);
5311 }
5312
Josef Bacik260e7702018-11-21 14:03:13 -05005313 /*
5314 * Evict can generate a large amount of delayed refs without
5315 * having a way to add space back since we exhaust our temporary
5316 * block rsv. We aren't allowed to do FLUSH_ALL in this case
5317 * because we could deadlock with so many things in the flushing
5318 * code, so we have to try and hold some extra space to
5319 * compensate for our delayed ref generation. If we can't get
5320 * that space then we need see if we can steal our minimum from
5321 * the global reserve. We will be ratelimited by the amount of
5322 * space we have for the delayed refs rsv, so we'll end up
5323 * committing and trying again.
5324 */
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005325 trans = btrfs_join_transaction(root);
Josef Bacik260e7702018-11-21 14:03:13 -05005326 if (IS_ERR(trans) || !ret) {
5327 if (!IS_ERR(trans)) {
5328 trans->block_rsv = &fs_info->trans_block_rsv;
5329 trans->bytes_reserved = delayed_refs_extra;
5330 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5331 delayed_refs_extra, 1);
5332 }
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005333 return trans;
Josef Bacik260e7702018-11-21 14:03:13 -05005334 }
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005335
5336 /*
5337 * Try to steal from the global reserve if there is space for
5338 * it.
5339 */
Josef Bacik644036122018-12-03 10:20:36 -05005340 if (!btrfs_check_space_for_delayed_refs(fs_info) &&
5341 !btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0))
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005342 return trans;
5343
5344 /* If not, commit and try again. */
5345 ret = btrfs_commit_transaction(trans);
5346 if (ret)
5347 return ERR_PTR(ret);
5348 }
5349}
5350
Al Virobd555972010-06-07 11:35:40 -04005351void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005352{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005353 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005354 struct btrfs_trans_handle *trans;
5355 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005356 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005357 int ret;
5358
liubo1abe9b82011-03-24 11:18:59 +00005359 trace_btrfs_inode_evict(inode);
5360
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005361 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005362 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005363 return;
5364 }
5365
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005366 evict_inode_truncate_pages(inode);
5367
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005368 if (inode->i_nlink &&
5369 ((btrfs_root_refs(&root->root_item) != 0 &&
5370 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005371 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005372 goto no_delete;
5373
Omar Sandoval27919062018-05-11 13:13:37 -07005374 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005375 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005376
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005377 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005378
Omar Sandoval7b40b692018-05-11 13:13:33 -07005379 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005380 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005381
Yan, Zheng76dda932009-09-21 16:00:26 -04005382 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005383 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5384 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005385 goto no_delete;
5386 }
5387
Nikolay Borisovaa790212017-01-10 20:35:40 +02005388 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005389 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005390 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005391
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005392 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005393 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005394 goto no_delete;
Josef Bacikad80cf52018-09-28 07:18:19 -04005395 rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005396 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005397
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005398 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005399
Yan, Zheng80825102009-11-12 09:35:36 +00005400 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005401 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005402 if (IS_ERR(trans))
5403 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005404
5405 trans->block_rsv = rsv;
5406
Yan, Zhengd68fc572010-05-16 10:49:58 -04005407 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005408 trans->block_rsv = &fs_info->trans_block_rsv;
5409 btrfs_end_transaction(trans);
5410 btrfs_btree_balance_dirty(fs_info);
5411 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5412 goto free_rsv;
5413 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005414 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005415 }
5416
Josef Bacik4ef31a42013-08-13 14:10:08 -04005417 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005418 * Errors here aren't a big deal, it just means we leave orphan items in
5419 * the tree. They will be cleaned up on the next mount. If the inode
5420 * number gets reused, cleanup deletes the orphan item without doing
5421 * anything, and unlink reuses the existing orphan item.
5422 *
5423 * If it turns out that we are dropping too many of these, we might want
5424 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005425 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005426 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005427 if (!IS_ERR(trans)) {
5428 trans->block_rsv = rsv;
5429 btrfs_orphan_del(trans, BTRFS_I(inode));
5430 trans->block_rsv = &fs_info->trans_block_rsv;
5431 btrfs_end_transaction(trans);
5432 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005433
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005434 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005435 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005436 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005437
Omar Sandoval27919062018-05-11 13:13:37 -07005438free_rsv:
5439 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005440no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005441 /*
5442 * If we didn't successfully delete, the orphan item will still be in
5443 * the tree and we'll retry on the next mount. Again, we might also want
5444 * to retry these periodically in the future.
5445 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005446 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005447 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005448}
5449
5450/*
5451 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005452 * If no dir entries were found, returns -ENOENT.
5453 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005454 */
5455static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5456 struct btrfs_key *location)
5457{
5458 const char *name = dentry->d_name.name;
5459 int namelen = dentry->d_name.len;
5460 struct btrfs_dir_item *di;
5461 struct btrfs_path *path;
5462 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005463 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005464
5465 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005466 if (!path)
5467 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005468
David Sterbaf85b7372017-01-20 14:54:07 +01005469 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5470 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005471 if (IS_ERR_OR_NULL(di)) {
5472 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005473 goto out;
5474 }
Chris Masond3977122009-01-05 21:25:51 -05005475
Chris Mason5f39d392007-10-15 16:14:19 -04005476 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005477 if (location->type != BTRFS_INODE_ITEM_KEY &&
5478 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005479 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005480 btrfs_warn(root->fs_info,
5481"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5482 __func__, name, btrfs_ino(BTRFS_I(dir)),
5483 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005484 }
Chris Mason39279cc2007-06-12 06:35:45 -04005485out:
Chris Mason39279cc2007-06-12 06:35:45 -04005486 btrfs_free_path(path);
5487 return ret;
5488}
5489
5490/*
5491 * when we hit a tree root in a directory, the btrfs part of the inode
5492 * needs to be changed to reflect the root directory of the tree root. This
5493 * is kind of like crossing a mount point.
5494 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005495static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005496 struct inode *dir,
5497 struct dentry *dentry,
5498 struct btrfs_key *location,
5499 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005500{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005501 struct btrfs_path *path;
5502 struct btrfs_root *new_root;
5503 struct btrfs_root_ref *ref;
5504 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005505 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005506 int ret;
5507 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005508
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005509 path = btrfs_alloc_path();
5510 if (!path) {
5511 err = -ENOMEM;
5512 goto out;
5513 }
Chris Mason39279cc2007-06-12 06:35:45 -04005514
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005515 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005516 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5517 key.type = BTRFS_ROOT_REF_KEY;
5518 key.offset = location->objectid;
5519
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005520 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521 if (ret) {
5522 if (ret < 0)
5523 err = ret;
5524 goto out;
5525 }
Chris Mason39279cc2007-06-12 06:35:45 -04005526
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005527 leaf = path->nodes[0];
5528 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005529 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5531 goto out;
5532
5533 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5534 (unsigned long)(ref + 1),
5535 dentry->d_name.len);
5536 if (ret)
5537 goto out;
5538
David Sterbab3b4aa72011-04-21 01:20:15 +02005539 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005540
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005541 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005542 if (IS_ERR(new_root)) {
5543 err = PTR_ERR(new_root);
5544 goto out;
5545 }
5546
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005547 *sub_root = new_root;
5548 location->objectid = btrfs_root_dirid(&new_root->root_item);
5549 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005550 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005551 err = 0;
5552out:
5553 btrfs_free_path(path);
5554 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005555}
5556
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005557static void inode_tree_add(struct inode *inode)
5558{
5559 struct btrfs_root *root = BTRFS_I(inode)->root;
5560 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005561 struct rb_node **p;
5562 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005563 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005564 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005565
Al Viro1d3382cb2010-10-23 15:19:20 -04005566 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005567 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005568 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005569 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005570 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005571 while (*p) {
5572 parent = *p;
5573 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5574
David Sterba37508512018-06-29 10:56:40 +02005575 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005576 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005577 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005578 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005579 else {
5580 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005581 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005582 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005583 RB_CLEAR_NODE(parent);
5584 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005585 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005586 }
5587 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005588 rb_link_node(new, parent, p);
5589 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005590 spin_unlock(&root->inode_lock);
5591}
5592
5593static void inode_tree_del(struct inode *inode)
5594{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005595 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005596 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005597 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005598
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005599 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005602 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005603 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005604 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005605 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005606
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005607 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005608 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005609 spin_lock(&root->inode_lock);
5610 empty = RB_EMPTY_ROOT(&root->inode_tree);
5611 spin_unlock(&root->inode_lock);
5612 if (empty)
5613 btrfs_add_dead_root(root);
5614 }
5615}
5616
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617
Chris Masone02119d2008-09-05 16:13:11 -04005618static int btrfs_init_locked_inode(struct inode *inode, void *p)
5619{
5620 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005621 inode->i_ino = args->location->objectid;
5622 memcpy(&BTRFS_I(inode)->location, args->location,
5623 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005624 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005625 return 0;
5626}
5627
5628static int btrfs_find_actor(struct inode *inode, void *opaque)
5629{
5630 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005631 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005632 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005633}
5634
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005635static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005636 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005637 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005638{
5639 struct inode *inode;
5640 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005641 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005642
Chris Mason90d3e592014-01-09 17:28:00 -08005643 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005644 args.root = root;
5645
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005646 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005647 btrfs_init_locked_inode,
5648 (void *)&args);
5649 return inode;
5650}
5651
Balaji Rao1a54ef82008-07-21 02:01:04 +05305652/* Get an inode object given its location and corresponding root.
5653 * Returns in *is_new if the inode was read from disk
5654 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005655struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5656 struct btrfs_root *root, int *new,
5657 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305658{
5659 struct inode *inode;
5660
Chris Mason90d3e592014-01-09 17:28:00 -08005661 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305662 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005663 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305664
5665 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005666 int ret;
5667
Filipe Manana4222ea72018-10-24 10:13:03 +01005668 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005669 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005670 inode_tree_add(inode);
5671 unlock_new_inode(inode);
5672 if (new)
5673 *new = 1;
5674 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005675 iget_failed(inode);
5676 /*
5677 * ret > 0 can come from btrfs_search_slot called by
5678 * btrfs_read_locked_inode, this means the inode item
5679 * was not found.
5680 */
5681 if (ret > 0)
5682 ret = -ENOENT;
5683 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005684 }
5685 }
5686
Balaji Rao1a54ef82008-07-21 02:01:04 +05305687 return inode;
5688}
5689
Filipe Manana4222ea72018-10-24 10:13:03 +01005690struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5691 struct btrfs_root *root, int *new)
5692{
5693 return btrfs_iget_path(s, location, root, new, NULL);
5694}
5695
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005696static struct inode *new_simple_dir(struct super_block *s,
5697 struct btrfs_key *key,
5698 struct btrfs_root *root)
5699{
5700 struct inode *inode = new_inode(s);
5701
5702 if (!inode)
5703 return ERR_PTR(-ENOMEM);
5704
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005705 BTRFS_I(inode)->root = root;
5706 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005707 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005708
5709 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005710 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005711 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005712 inode->i_fop = &simple_dir_operations;
5713 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005714 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305715 inode->i_atime = inode->i_mtime;
5716 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005717 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005718
5719 return inode;
5720}
5721
Chris Mason3de45862008-11-17 21:02:50 -05005722struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005723{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005724 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005725 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005726 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005727 struct btrfs_root *sub_root = root;
5728 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005729 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005730 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005731
5732 if (dentry->d_name.len > BTRFS_NAME_LEN)
5733 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005734
Jeff Layton39e3c952012-11-28 11:30:53 -05005735 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005736 if (ret < 0)
5737 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005738
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005739 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005740 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005741 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005742 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005743
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005744 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005745 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005746 &location, &sub_root);
5747 if (ret < 0) {
5748 if (ret != -ENOENT)
5749 inode = ERR_PTR(ret);
5750 else
5751 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5752 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005753 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005754 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005755 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005756
Julia Lawall34d19ba2011-01-24 19:55:19 +00005757 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005758 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005759 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005760 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005761 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005762 if (ret) {
5763 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005764 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005765 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005766 }
5767
Chris Mason3de45862008-11-17 21:02:50 -05005768 return inode;
5769}
5770
Nick Pigginfe15ce42011-01-07 17:49:23 +11005771static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005772{
5773 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005774 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005775
Li Zefan848cce02012-02-21 17:04:28 +08005776 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005777 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005778
Li Zefan848cce02012-02-21 17:04:28 +08005779 if (inode) {
5780 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005781 if (btrfs_root_refs(&root->root_item) == 0)
5782 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005783
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005784 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005785 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005786 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005787 return 0;
5788}
5789
Chris Mason3de45862008-11-17 21:02:50 -05005790static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005791 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005792{
Al Viro3837d202018-10-10 16:38:27 -04005793 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005794
Al Viro3837d202018-10-10 16:38:27 -04005795 if (inode == ERR_PTR(-ENOENT))
5796 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005797 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005798}
5799
Miao Xie16cdcec2011-04-22 18:12:22 +08005800unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005801 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5802};
5803
Josef Bacik23b5ec72017-07-24 15:14:25 -04005804/*
5805 * All this infrastructure exists because dir_emit can fault, and we are holding
5806 * the tree lock when doing readdir. For now just allocate a buffer and copy
5807 * our information into that, and then dir_emit from the buffer. This is
5808 * similar to what NFS does, only we don't keep the buffer around in pagecache
5809 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5810 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5811 * tree lock.
5812 */
5813static int btrfs_opendir(struct inode *inode, struct file *file)
5814{
5815 struct btrfs_file_private *private;
5816
5817 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5818 if (!private)
5819 return -ENOMEM;
5820 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5821 if (!private->filldir_buf) {
5822 kfree(private);
5823 return -ENOMEM;
5824 }
5825 file->private_data = private;
5826 return 0;
5827}
5828
5829struct dir_entry {
5830 u64 ino;
5831 u64 offset;
5832 unsigned type;
5833 int name_len;
5834};
5835
5836static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5837{
5838 while (entries--) {
5839 struct dir_entry *entry = addr;
5840 char *name = (char *)(entry + 1);
5841
David Sterba92d32172018-04-16 21:10:14 +02005842 ctx->pos = get_unaligned(&entry->offset);
5843 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5844 get_unaligned(&entry->ino),
5845 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005846 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005847 addr += sizeof(struct dir_entry) +
5848 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005849 ctx->pos++;
5850 }
5851 return 0;
5852}
5853
Al Viro9cdda8d2013-05-22 16:48:09 -04005854static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005855{
Al Viro9cdda8d2013-05-22 16:48:09 -04005856 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005857 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005858 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005859 struct btrfs_dir_item *di;
5860 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005861 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005862 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005863 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005864 struct list_head ins_list;
5865 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005866 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005867 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005868 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005869 char *name_ptr;
5870 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005871 int entries = 0;
5872 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005873 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005874 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005875
Al Viro9cdda8d2013-05-22 16:48:09 -04005876 if (!dir_emit_dots(file, ctx))
5877 return 0;
5878
David Woodhouse49593bf2008-08-17 17:08:36 +01005879 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005880 if (!path)
5881 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005882
Josef Bacik23b5ec72017-07-24 15:14:25 -04005883 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005884 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005885
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005886 INIT_LIST_HEAD(&ins_list);
5887 INIT_LIST_HEAD(&del_list);
5888 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005889
Josef Bacik23b5ec72017-07-24 15:14:25 -04005890again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005891 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005892 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005893 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005894
Chris Mason39279cc2007-06-12 06:35:45 -04005895 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5896 if (ret < 0)
5897 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005898
5899 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005900 struct dir_entry *entry;
5901
Chris Mason5f39d392007-10-15 16:14:19 -04005902 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005903 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005904 if (slot >= btrfs_header_nritems(leaf)) {
5905 ret = btrfs_next_leaf(root, path);
5906 if (ret < 0)
5907 goto err;
5908 else if (ret > 0)
5909 break;
5910 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005911 }
Chris Mason3de45862008-11-17 21:02:50 -05005912
Chris Mason5f39d392007-10-15 16:14:19 -04005913 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5914
5915 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005916 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005917 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005918 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005919 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005920 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005921 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005922 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005923 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005924 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005925 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5926 PAGE_SIZE) {
5927 btrfs_release_path(path);
5928 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5929 if (ret)
5930 goto nopos;
5931 addr = private->filldir_buf;
5932 entries = 0;
5933 total_len = 0;
5934 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005935 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005936
5937 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005938 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005939 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005940 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5941 name_len);
David Sterba92d32172018-04-16 21:10:14 +02005942 put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
5943 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005944 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005945 put_unaligned(location.objectid, &entry->ino);
5946 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005947 entries++;
5948 addr += sizeof(struct dir_entry) + name_len;
5949 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005950next:
5951 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005952 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005953 btrfs_release_path(path);
5954
5955 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5956 if (ret)
5957 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005958
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005959 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005960 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005961 goto nopos;
5962
Zach Browndb62efb2013-07-11 16:19:42 -07005963 /*
5964 * Stop new entries from being returned after we return the last
5965 * entry.
5966 *
5967 * New directory entries are assigned a strictly increasing
5968 * offset. This means that new entries created during readdir
5969 * are *guaranteed* to be seen in the future by that readdir.
5970 * This has broken buggy programs which operate on names as
5971 * they're returned by readdir. Until we re-use freed offsets
5972 * we have this hack to stop new entries from being returned
5973 * under the assumption that they'll never reach this huge
5974 * offset.
5975 *
5976 * This is being careful not to overflow 32bit loff_t unless the
5977 * last entry requires it because doing so has broken 32bit apps
5978 * in the past.
5979 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005980 if (ctx->pos >= INT_MAX)
5981 ctx->pos = LLONG_MAX;
5982 else
5983 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005984nopos:
5985 ret = 0;
5986err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005987 if (put)
5988 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005989 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005990 return ret;
5991}
5992
Chris Mason39279cc2007-06-12 06:35:45 -04005993/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005994 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005995 * inode changes. But, it is most likely to find the inode in cache.
5996 * FIXME, needs more benchmarking...there are no reasons other than performance
5997 * to keep or drop this code.
5998 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005999static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006000{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006001 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006002 struct btrfs_root *root = BTRFS_I(inode)->root;
6003 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006004 int ret;
6005
Josef Bacik72ac3c02012-05-23 14:13:11 -04006006 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006007 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006008
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006009 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006010 if (IS_ERR(trans))
6011 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006012
6013 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006014 if (ret && ret == -ENOSPC) {
6015 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006016 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006017 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006018 if (IS_ERR(trans))
6019 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006020
Chris Mason94b60442010-05-26 11:02:00 -04006021 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006022 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006023 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006024 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006025 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006026
6027 return ret;
6028}
6029
6030/*
6031 * This is a copy of file_update_time. We need this so we can return error on
6032 * ENOSPC for updating the inode in the case of file write and mmap writes.
6033 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07006034static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04006035 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006036{
Alexander Block2bc5565282012-06-15 09:49:33 +02006037 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006038 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006039
6040 if (btrfs_root_readonly(root))
6041 return -EROFS;
6042
Josef Bacike41f9412012-03-26 09:46:47 -04006043 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006044 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006045 if (flags & S_CTIME)
6046 inode->i_ctime = *now;
6047 if (flags & S_MTIME)
6048 inode->i_mtime = *now;
6049 if (flags & S_ATIME)
6050 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006051 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006052}
6053
Chris Masond352ac62008-09-29 15:18:18 -04006054/*
6055 * find the highest existing sequence number in a directory
6056 * and then set the in-memory index_cnt variable to reflect
6057 * free sequence numbers
6058 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006059static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006060{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006061 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006062 struct btrfs_key key, found_key;
6063 struct btrfs_path *path;
6064 struct extent_buffer *leaf;
6065 int ret;
6066
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006067 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006068 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006069 key.offset = (u64)-1;
6070
6071 path = btrfs_alloc_path();
6072 if (!path)
6073 return -ENOMEM;
6074
6075 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6076 if (ret < 0)
6077 goto out;
6078 /* FIXME: we should be able to handle this */
6079 if (ret == 0)
6080 goto out;
6081 ret = 0;
6082
6083 /*
6084 * MAGIC NUMBER EXPLANATION:
6085 * since we search a directory based on f_pos we have to start at 2
6086 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6087 * else has to start at 2
6088 */
6089 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006090 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006091 goto out;
6092 }
6093
6094 path->slots[0]--;
6095
6096 leaf = path->nodes[0];
6097 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6098
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006099 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006100 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006101 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006102 goto out;
6103 }
6104
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006105 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006106out:
6107 btrfs_free_path(path);
6108 return ret;
6109}
6110
Chris Masond352ac62008-09-29 15:18:18 -04006111/*
6112 * helper to find a free sequence number in a given directory. This current
6113 * code is very simple, later versions will do smarter things in the btree
6114 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006115int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006116{
6117 int ret = 0;
6118
Nikolay Borisov877574e2017-02-20 13:50:33 +02006119 if (dir->index_cnt == (u64)-1) {
6120 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006121 if (ret) {
6122 ret = btrfs_set_inode_index_count(dir);
6123 if (ret)
6124 return ret;
6125 }
Josef Bacikaec74772008-07-24 12:12:38 -04006126 }
6127
Nikolay Borisov877574e2017-02-20 13:50:33 +02006128 *index = dir->index_cnt;
6129 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006130
6131 return ret;
6132}
6133
Chris Masonb0d5d102014-09-08 13:08:51 -07006134static int btrfs_insert_inode_locked(struct inode *inode)
6135{
6136 struct btrfs_iget_args args;
6137 args.location = &BTRFS_I(inode)->location;
6138 args.root = BTRFS_I(inode)->root;
6139
6140 return insert_inode_locked4(inode,
6141 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6142 btrfs_find_actor, &args);
6143}
6144
Anand Jain19aee8d2017-07-18 17:37:05 +08006145/*
6146 * Inherit flags from the parent inode.
6147 *
6148 * Currently only the compression flags and the cow flags are inherited.
6149 */
6150static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6151{
6152 unsigned int flags;
6153
6154 if (!dir)
6155 return;
6156
6157 flags = BTRFS_I(dir)->flags;
6158
6159 if (flags & BTRFS_INODE_NOCOMPRESS) {
6160 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6161 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6162 } else if (flags & BTRFS_INODE_COMPRESS) {
6163 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6164 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6165 }
6166
6167 if (flags & BTRFS_INODE_NODATACOW) {
6168 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6169 if (S_ISREG(inode->i_mode))
6170 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6171 }
6172
David Sterba7b6a2212018-03-26 18:40:21 +02006173 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006174}
6175
Chris Mason39279cc2007-06-12 06:35:45 -04006176static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6177 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006178 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006179 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006180 u64 ref_objectid, u64 objectid,
6181 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006182{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006183 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006184 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006185 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006186 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006187 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006188 struct btrfs_inode_ref *ref;
6189 struct btrfs_key key[2];
6190 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006191 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006192 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006193 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006194
Chris Mason5f39d392007-10-15 16:14:19 -04006195 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006196 if (!path)
6197 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006198
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006199 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006200 if (!inode) {
6201 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006202 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006203 }
Chris Mason39279cc2007-06-12 06:35:45 -04006204
Li Zefan581bb052011-04-20 10:06:11 +08006205 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006206 * O_TMPFILE, set link count to 0, so that after this point,
6207 * we fill in an inode item with the correct link count.
6208 */
6209 if (!name)
6210 set_nlink(inode, 0);
6211
6212 /*
Li Zefan581bb052011-04-20 10:06:11 +08006213 * we have to initialize this early, so we can reclaim the inode
6214 * number if we fail afterwards in this function.
6215 */
6216 inode->i_ino = objectid;
6217
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006218 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006219 trace_btrfs_inode_request(dir);
6220
Nikolay Borisov877574e2017-02-20 13:50:33 +02006221 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006222 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006223 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006224 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006225 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006226 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006227 } else if (dir) {
6228 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006229 }
6230 /*
6231 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006232 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006233 * number
6234 */
6235 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006236 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006237 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006238 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006239 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006240
Josef Bacik5dc562c2012-08-17 13:14:17 -04006241 /*
6242 * We could have gotten an inode number from somebody who was fsynced
6243 * and then removed in this same transaction, so let's just set full
6244 * sync since it will be a full sync anyway and this will blow away the
6245 * old info in the log.
6246 */
6247 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6248
Chris Mason9c583092008-01-29 15:15:18 -05006249 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006250 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006251 key[0].offset = 0;
6252
Chris Mason9c583092008-01-29 15:15:18 -05006253 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006254
6255 if (name) {
6256 /*
6257 * Start new inodes with an inode_ref. This is slightly more
6258 * efficient for small numbers of hard links since they will
6259 * be packed into one item. Extended refs will kick in if we
6260 * add more hard links than can fit in the ref item.
6261 */
6262 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006263 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006264 key[1].offset = ref_objectid;
6265
6266 sizes[1] = name_len + sizeof(*ref);
6267 }
Chris Mason9c583092008-01-29 15:15:18 -05006268
Chris Masonb0d5d102014-09-08 13:08:51 -07006269 location = &BTRFS_I(inode)->location;
6270 location->objectid = objectid;
6271 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006272 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006273
6274 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006275 if (ret < 0) {
6276 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006277 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006278 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006279
Chris Masonb9473432009-03-13 11:00:37 -04006280 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006281 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006282 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006283 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006284
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006285 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006286 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306287
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006288 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306289 inode->i_atime = inode->i_mtime;
6290 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006291 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306292
Chris Mason5f39d392007-10-15 16:14:19 -04006293 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6294 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006295 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006296 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006297 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006298
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006299 if (name) {
6300 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6301 struct btrfs_inode_ref);
6302 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6303 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6304 ptr = (unsigned long)(ref + 1);
6305 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6306 }
Chris Mason9c583092008-01-29 15:15:18 -05006307
Chris Mason5f39d392007-10-15 16:14:19 -04006308 btrfs_mark_buffer_dirty(path->nodes[0]);
6309 btrfs_free_path(path);
6310
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006311 btrfs_inherit_iflags(inode, dir);
6312
Al Viro569254b2011-07-24 17:08:40 -04006313 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006314 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006315 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006316 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006317 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6318 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006319 }
6320
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006321 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006322
6323 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006324 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006325
Alexander Block8ea05e32012-07-25 17:35:53 +02006326 btrfs_update_root_times(trans, root);
6327
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006328 ret = btrfs_inode_inherit_props(trans, inode, dir);
6329 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006330 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006331 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006332 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006333
Chris Mason39279cc2007-06-12 06:35:45 -04006334 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006335
6336fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006337 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006338fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006339 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006340 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006341 btrfs_free_path(path);
6342 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006343}
6344
6345static inline u8 btrfs_inode_type(struct inode *inode)
6346{
6347 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6348}
6349
Chris Masond352ac62008-09-29 15:18:18 -04006350/*
6351 * utility function to add 'inode' into 'parent_inode' with
6352 * a give name and a given sequence number.
6353 * if 'add_backref' is true, also insert a backref from the
6354 * inode to the parent directory.
6355 */
Chris Masone02119d2008-09-05 16:13:11 -04006356int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006357 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006358 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006359{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006360 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006361 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006362 struct btrfs_root *root = parent_inode->root;
6363 u64 ino = btrfs_ino(inode);
6364 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006365
Li Zefan33345d012011-04-20 10:31:50 +08006366 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006367 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006368 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006369 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006370 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006371 key.offset = 0;
6372 }
Chris Mason39279cc2007-06-12 06:35:45 -04006373
Li Zefan33345d012011-04-20 10:31:50 +08006374 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006375 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006376 root->root_key.objectid, parent_ino,
6377 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006378 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006379 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6380 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006381 }
6382
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006383 /* Nothing to clean up yet */
6384 if (ret)
6385 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006386
Lu Fengqi684572d2018-08-04 21:10:57 +08006387 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006388 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006389 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006390 goto fail_dir_item;
6391 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006392 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006393 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006394 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006395
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006396 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006397 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006398 inode_inc_iversion(&parent_inode->vfs_inode);
6399 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6400 current_time(&parent_inode->vfs_inode);
6401 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006402 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006403 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006404 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006405
6406fail_dir_item:
6407 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6408 u64 local_index;
6409 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006410 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006411 root->root_key.objectid, parent_ino,
6412 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006413 if (err)
6414 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006415 } else if (add_backref) {
6416 u64 local_index;
6417 int err;
6418
6419 err = btrfs_del_inode_ref(trans, root, name, name_len,
6420 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006421 if (err)
6422 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006423 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006424
6425 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006426 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006427}
6428
6429static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006430 struct btrfs_inode *dir, struct dentry *dentry,
6431 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006432{
Josef Bacika1b075d2010-11-19 20:36:11 +00006433 int err = btrfs_add_link(trans, dir, inode,
6434 dentry->d_name.name, dentry->d_name.len,
6435 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006436 if (err > 0)
6437 err = -EEXIST;
6438 return err;
6439}
6440
Josef Bacik618e21d2007-07-11 10:18:17 -04006441static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006442 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006443{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006444 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006445 struct btrfs_trans_handle *trans;
6446 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006447 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006448 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006449 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006450 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006451
Josef Bacik9ed74f22009-09-11 16:12:44 -04006452 /*
6453 * 2 for inode item and ref
6454 * 2 for dir items
6455 * 1 for xattr if selinux is on
6456 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006457 trans = btrfs_start_transaction(root, 5);
6458 if (IS_ERR(trans))
6459 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006460
Li Zefan581bb052011-04-20 10:06:11 +08006461 err = btrfs_find_free_ino(root, &objectid);
6462 if (err)
6463 goto out_unlock;
6464
Josef Bacikaec74772008-07-24 12:12:38 -04006465 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006466 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6467 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006468 if (IS_ERR(inode)) {
6469 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006470 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006471 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006472 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006473
Casey Schauflerad19db72011-12-15 10:09:07 -05006474 /*
6475 * If the active LSM wants to access the inode during
6476 * d_instantiate it needs these. Smack checks to see
6477 * if the filesystem supports xattrs by looking at the
6478 * ops vector.
6479 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006480 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006481 init_special_inode(inode, inode->i_mode, rdev);
6482
6483 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006484 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006485 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006486
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006487 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6488 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006489 if (err)
6490 goto out_unlock;
6491
6492 btrfs_update_inode(trans, root, inode);
6493 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006494
Josef Bacik618e21d2007-07-11 10:18:17 -04006495out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006496 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006497 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006498 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006499 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006500 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006501 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006502 return err;
6503}
6504
Chris Mason39279cc2007-06-12 06:35:45 -04006505static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006506 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006507{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006508 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006509 struct btrfs_trans_handle *trans;
6510 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006511 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006512 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006513 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006514 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006515
Josef Bacik9ed74f22009-09-11 16:12:44 -04006516 /*
6517 * 2 for inode item and ref
6518 * 2 for dir items
6519 * 1 for xattr if selinux is on
6520 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006521 trans = btrfs_start_transaction(root, 5);
6522 if (IS_ERR(trans))
6523 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006524
Li Zefan581bb052011-04-20 10:06:11 +08006525 err = btrfs_find_free_ino(root, &objectid);
6526 if (err)
6527 goto out_unlock;
6528
Josef Bacikaec74772008-07-24 12:12:38 -04006529 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006530 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6531 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006532 if (IS_ERR(inode)) {
6533 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006534 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006535 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006536 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006537 /*
6538 * If the active LSM wants to access the inode during
6539 * d_instantiate it needs these. Smack checks to see
6540 * if the filesystem supports xattrs by looking at the
6541 * ops vector.
6542 */
6543 inode->i_fop = &btrfs_file_operations;
6544 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006545 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006546
6547 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6548 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006549 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006550
6551 err = btrfs_update_inode(trans, root, inode);
6552 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006553 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006554
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006555 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6556 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006557 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006558 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006559
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006560 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006561 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006562
Chris Mason39279cc2007-06-12 06:35:45 -04006563out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006564 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006565 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006566 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006567 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006568 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006569 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006570 return err;
6571}
6572
6573static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6574 struct dentry *dentry)
6575{
Filipe Manana271dba452016-01-05 16:24:05 +00006576 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006577 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006578 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006579 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006580 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006581 int err;
6582 int drop_inode = 0;
6583
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006584 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006585 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006586 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006587
Mark Fashehf1863732012-08-08 11:32:27 -07006588 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006589 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006590
Nikolay Borisov877574e2017-02-20 13:50:33 +02006591 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006592 if (err)
6593 goto fail;
6594
Yan, Zhenga22285a2010-05-16 10:48:46 -04006595 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006596 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006597 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006598 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006599 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006600 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006601 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006602 if (IS_ERR(trans)) {
6603 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006604 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006605 goto fail;
6606 }
Chris Mason5f39d392007-10-15 16:14:19 -04006607
Miao Xie67de1172013-12-26 13:07:06 +08006608 /* There are several dir indexes for this inode, clear the cache. */
6609 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006610 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006611 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006612 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006613 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006614 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006615
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006616 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6617 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006618
Yan, Zhenga5719522009-09-24 09:17:31 -04006619 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006620 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006621 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006622 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006623 int ret;
6624
Yan, Zhenga5719522009-09-24 09:17:31 -04006625 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006626 if (err)
6627 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006628 if (inode->i_nlink == 1) {
6629 /*
6630 * If new hard link count is 1, it's a file created
6631 * with open(2) O_TMPFILE flag.
6632 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006633 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006634 if (err)
6635 goto fail;
6636 }
Filipe Manana41bd6062018-11-28 14:54:28 +00006637 BTRFS_I(inode)->last_link_trans = trans->transid;
Al Viro08c422c2011-12-23 07:58:13 -05006638 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006639 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6640 true, NULL);
6641 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6642 err = btrfs_commit_transaction(trans);
6643 trans = NULL;
6644 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006645 }
Chris Mason39279cc2007-06-12 06:35:45 -04006646
Chris Mason1832a6d2007-12-21 16:27:21 -05006647fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006648 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006649 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006650 if (drop_inode) {
6651 inode_dec_link_count(inode);
6652 iput(inode);
6653 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006654 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006655 return err;
6656}
6657
Al Viro18bb1db2011-07-26 01:41:39 -04006658static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006659{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006660 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006661 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006662 struct btrfs_trans_handle *trans;
6663 struct btrfs_root *root = BTRFS_I(dir)->root;
6664 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006665 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006666 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006667
Josef Bacik9ed74f22009-09-11 16:12:44 -04006668 /*
6669 * 2 items for inode and ref
6670 * 2 items for dir items
6671 * 1 for xattr if selinux is on
6672 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006673 trans = btrfs_start_transaction(root, 5);
6674 if (IS_ERR(trans))
6675 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006676
Li Zefan581bb052011-04-20 10:06:11 +08006677 err = btrfs_find_free_ino(root, &objectid);
6678 if (err)
6679 goto out_fail;
6680
Josef Bacikaec74772008-07-24 12:12:38 -04006681 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006682 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6683 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006684 if (IS_ERR(inode)) {
6685 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006686 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006687 goto out_fail;
6688 }
Chris Mason5f39d392007-10-15 16:14:19 -04006689
Chris Masonb0d5d102014-09-08 13:08:51 -07006690 /* these must be set before we unlock the inode */
6691 inode->i_op = &btrfs_dir_inode_operations;
6692 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006693
Eric Paris2a7dba32011-02-01 11:05:39 -05006694 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006695 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006696 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006697
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006698 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006699 err = btrfs_update_inode(trans, root, inode);
6700 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006701 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006702
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006703 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6704 dentry->d_name.name,
6705 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006706 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006707 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006708
Al Viro1e2e5472018-05-04 08:23:01 -04006709 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006710
6711out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006712 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006713 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006714 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006715 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006716 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006717 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006718 return err;
6719}
6720
Chris Masonc8b97812008-10-29 14:49:59 -04006721static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006722 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006723 size_t pg_offset, u64 extent_offset,
6724 struct btrfs_file_extent_item *item)
6725{
6726 int ret;
6727 struct extent_buffer *leaf = path->nodes[0];
6728 char *tmp;
6729 size_t max_size;
6730 unsigned long inline_size;
6731 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006732 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006733
6734 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006735 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006736 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6737 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006738 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006739 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006740 if (!tmp)
6741 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006742 ptr = btrfs_file_extent_inline_start(item);
6743
6744 read_extent_buffer(leaf, tmp, ptr, inline_size);
6745
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006746 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006747 ret = btrfs_decompress(compress_type, tmp, page,
6748 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006749
6750 /*
6751 * decompression code contains a memset to fill in any space between the end
6752 * of the uncompressed data and the end of max_size in case the decompressed
6753 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6754 * the end of an inline extent and the beginning of the next block, so we
6755 * cover that region here.
6756 */
6757
6758 if (max_size + pg_offset < PAGE_SIZE) {
6759 char *map = kmap(page);
6760 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6761 kunmap(page);
6762 }
Chris Masonc8b97812008-10-29 14:49:59 -04006763 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006764 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006765}
6766
Chris Masond352ac62008-09-29 15:18:18 -04006767/*
6768 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006769 * the ugly parts come from merging extents from the disk with the in-ram
6770 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006771 * where the in-ram extents might be locked pending data=ordered completion.
6772 *
6773 * This also copies inline extents directly into the page.
6774 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006775struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006776 struct page *page,
6777 size_t pg_offset, u64 start, u64 len,
6778 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006779{
David Sterba3ffbd682018-06-29 10:56:42 +02006780 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006781 int ret;
6782 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006783 u64 extent_start = 0;
6784 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006785 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006786 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006787 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006788 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006789 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006790 struct extent_buffer *leaf;
6791 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006792 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006793 struct extent_map_tree *em_tree = &inode->extent_tree;
6794 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006795 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006796
Chris Mason890871b2009-09-02 16:24:52 -04006797 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006798 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006799 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006800 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006801 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006802
Chris Masona52d9a82007-08-27 16:49:44 -04006803 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006804 if (em->start > start || em->start + em->len <= start)
6805 free_extent_map(em);
6806 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006807 free_extent_map(em);
6808 else
6809 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006810 }
David Sterba172ddd62011-04-21 00:48:27 +02006811 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006812 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006813 err = -ENOMEM;
6814 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006815 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006816 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006817 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006818 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006819 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006820 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006821
Liu Bobee6ec82018-08-17 05:05:28 +08006822 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006823 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006824 err = -ENOMEM;
6825 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006826 }
6827
Liu Bobee6ec82018-08-17 05:05:28 +08006828 /* Chances are we'll be called again, so go ahead and do readahead */
6829 path->reada = READA_FORWARD;
6830
Liu Boe49aabd2018-08-25 13:47:09 +08006831 /*
6832 * Unless we're going to uncompress the inline extent, no sleep would
6833 * happen.
6834 */
6835 path->leave_spinning = 1;
6836
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006837 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006838 if (ret < 0) {
6839 err = ret;
6840 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006841 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006842 if (path->slots[0] == 0)
6843 goto not_found;
6844 path->slots[0]--;
6845 }
6846
Chris Mason5f39d392007-10-15 16:14:19 -04006847 leaf = path->nodes[0];
6848 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006849 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006850 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006851 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006852 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006853 /*
6854 * If we backup past the first extent we want to move forward
6855 * and see if there is an extent in front of us, otherwise we'll
6856 * say there is a hole for our whole search range which can
6857 * cause problems.
6858 */
6859 extent_end = start;
6860 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006861 }
6862
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006863 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006864 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006865 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6866 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006867 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006868 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006869
6870 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6871 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006872 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04006873 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006874
6875 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006876 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006877 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006878
6879 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6880 path->slots[0],
6881 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006882 }
Josef Bacik25a50342013-10-14 12:08:38 -04006883next:
Yan Zheng9036c102008-10-30 14:19:41 -04006884 if (start >= extent_end) {
6885 path->slots[0]++;
6886 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6887 ret = btrfs_next_leaf(root, path);
6888 if (ret < 0) {
6889 err = ret;
6890 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006891 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006892 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006893 }
Yan Zheng9036c102008-10-30 14:19:41 -04006894 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006895 }
Yan Zheng9036c102008-10-30 14:19:41 -04006896 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6897 if (found_key.objectid != objectid ||
6898 found_key.type != BTRFS_EXTENT_DATA_KEY)
6899 goto not_found;
6900 if (start + len <= found_key.offset)
6901 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006902 if (start > found_key.offset)
6903 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006904
6905 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006906 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006907 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006908 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006909 em->block_start = EXTENT_MAP_HOLE;
6910 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006911 }
6912
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006913 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006914 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006915
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006916 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6917 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006918 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006919 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006920 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006921 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006922 size_t size;
6923 size_t extent_offset;
6924 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006925
Filipe Manana7ffbb592014-06-09 03:48:05 +01006926 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006927 goto out;
Yan689f9342007-10-29 11:41:07 -04006928
Qu Wenruoe41ca582018-06-06 15:41:49 +08006929 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006930 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006931 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6932 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006933 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006934 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006935 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006936 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006937 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006938
6939 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006940 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006941 if (btrfs_file_extent_compression(leaf, item) !=
6942 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006943 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006944 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006945 if (ret) {
6946 err = ret;
6947 goto out;
6948 }
Chris Masonc8b97812008-10-29 14:49:59 -04006949 } else {
6950 map = kmap(page);
6951 read_extent_buffer(leaf, map + pg_offset, ptr,
6952 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006953 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006954 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006955 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006956 copy_size);
6957 }
Chris Masonc8b97812008-10-29 14:49:59 -04006958 kunmap(page);
6959 }
Chris Mason179e29e2007-11-01 11:28:41 -04006960 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006961 }
Chris Masond1310b22008-01-24 16:13:08 -05006962 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006963 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006964 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006965 }
6966not_found:
6967 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006968 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006969 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006970 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006971insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006972 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006973 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006974 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006975 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6976 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006977 err = -EIO;
6978 goto out;
6979 }
Chris Masond1310b22008-01-24 16:13:08 -05006980
6981 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006982 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006983 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006984 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006985out:
Liu Boc6414282018-08-23 07:36:17 +08006986 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006987
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006988 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006989
Chris Masona52d9a82007-08-27 16:49:44 -04006990 if (err) {
6991 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006992 return ERR_PTR(err);
6993 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006994 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006995 return em;
6996}
6997
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006998struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006999 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05007000{
7001 struct extent_map *em;
7002 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007003 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05007004 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007005 u64 delalloc_len;
7006 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05007007 int err = 0;
7008
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02007009 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05007010 if (IS_ERR(em))
7011 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007012 /*
7013 * If our em maps to:
7014 * - a hole or
7015 * - a pre-alloc extent,
7016 * there might actually be delalloc bytes behind it.
7017 */
7018 if (em->block_start != EXTENT_MAP_HOLE &&
7019 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7020 return em;
7021 else
7022 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007023
7024 /* check to see if we've wrapped (len == -1 or similar) */
7025 end = start + len;
7026 if (end < start)
7027 end = (u64)-1;
7028 else
7029 end -= 1;
7030
7031 em = NULL;
7032
7033 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007034 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007035 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007036 delalloc_end = delalloc_start + delalloc_len;
7037 if (delalloc_end < delalloc_start)
7038 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05007039
7040 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007041 * We didn't find anything useful, return the original results from
7042 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05007043 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007044 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007045 em = hole_em;
7046 hole_em = NULL;
7047 goto out;
7048 }
7049
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007050 /*
7051 * Adjust the delalloc_start to make sure it doesn't go backwards from
7052 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007053 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007054 delalloc_start = max(start, delalloc_start);
7055 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007056
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007057 if (delalloc_len > 0) {
7058 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007059 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007060 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007061
David Sterba172ddd62011-04-21 00:48:27 +02007062 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007063 if (!em) {
7064 err = -ENOMEM;
7065 goto out;
7066 }
Chris Masonec29ed52011-02-23 16:23:20 -05007067 em->bdev = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007068
7069 ASSERT(hole_em);
7070 /*
7071 * When btrfs_get_extent can't find anything it returns one
7072 * huge hole
7073 *
7074 * Make sure what it found really fits our range, and adjust to
7075 * make sure it is based on the start from the caller
7076 */
7077 if (hole_end <= start || hole_em->start > end) {
7078 free_extent_map(hole_em);
7079 hole_em = NULL;
7080 } else {
7081 hole_start = max(hole_em->start, start);
7082 hole_len = hole_end - hole_start;
7083 }
7084
7085 if (hole_em && delalloc_start > hole_start) {
7086 /*
7087 * Our hole starts before our delalloc, so we have to
7088 * return just the parts of the hole that go until the
7089 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007090 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007091 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007092 em->start = hole_start;
7093 em->orig_start = hole_start;
7094 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007095 * Don't adjust block start at all, it is fixed at
7096 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007097 */
7098 em->block_start = hole_em->block_start;
7099 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007100 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7101 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007102 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007103 /*
7104 * Hole is out of passed range or it starts after
7105 * delalloc range
7106 */
7107 em->start = delalloc_start;
7108 em->len = delalloc_len;
7109 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007110 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007111 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007112 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007113 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007114 return hole_em;
7115 }
7116out:
7117
7118 free_extent_map(hole_em);
7119 if (err) {
7120 free_extent_map(em);
7121 return ERR_PTR(err);
7122 }
7123 return em;
7124}
7125
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007126static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7127 const u64 start,
7128 const u64 len,
7129 const u64 orig_start,
7130 const u64 block_start,
7131 const u64 block_len,
7132 const u64 orig_block_len,
7133 const u64 ram_bytes,
7134 const int type)
7135{
7136 struct extent_map *em = NULL;
7137 int ret;
7138
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007139 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007140 em = create_io_em(inode, start, len, orig_start,
7141 block_start, block_len, orig_block_len,
7142 ram_bytes,
7143 BTRFS_COMPRESS_NONE, /* compress_type */
7144 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007145 if (IS_ERR(em))
7146 goto out;
7147 }
7148 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7149 len, block_len, type);
7150 if (ret) {
7151 if (em) {
7152 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007153 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007154 start + len - 1, 0);
7155 }
7156 em = ERR_PTR(ret);
7157 }
7158 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007159
7160 return em;
7161}
7162
Josef Bacik4b46fce2010-05-23 11:00:55 -04007163static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7164 u64 start, u64 len)
7165{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007166 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007167 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007168 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007169 struct btrfs_key ins;
7170 u64 alloc_hint;
7171 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007172
Josef Bacik4b46fce2010-05-23 11:00:55 -04007173 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007174 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007175 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007176 if (ret)
7177 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007178
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007179 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7180 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007181 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007182 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007183 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007184 btrfs_free_reserved_extent(fs_info, ins.objectid,
7185 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007186
Josef Bacik4b46fce2010-05-23 11:00:55 -04007187 return em;
7188}
7189
Chris Mason46bfbb52010-05-26 11:04:10 -04007190/*
7191 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7192 * block must be cow'd
7193 */
Josef Bacik00361582013-08-14 14:02:47 -04007194noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007195 u64 *orig_start, u64 *orig_block_len,
7196 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007197{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007198 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007199 struct btrfs_path *path;
7200 int ret;
7201 struct extent_buffer *leaf;
7202 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007203 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007204 struct btrfs_file_extent_item *fi;
7205 struct btrfs_key key;
7206 u64 disk_bytenr;
7207 u64 backref_offset;
7208 u64 extent_end;
7209 u64 num_bytes;
7210 int slot;
7211 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007212 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007213
Chris Mason46bfbb52010-05-26 11:04:10 -04007214 path = btrfs_alloc_path();
7215 if (!path)
7216 return -ENOMEM;
7217
David Sterbaf85b7372017-01-20 14:54:07 +01007218 ret = btrfs_lookup_file_extent(NULL, root, path,
7219 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007220 if (ret < 0)
7221 goto out;
7222
7223 slot = path->slots[0];
7224 if (ret == 1) {
7225 if (slot == 0) {
7226 /* can't find the item, must cow */
7227 ret = 0;
7228 goto out;
7229 }
7230 slot--;
7231 }
7232 ret = 0;
7233 leaf = path->nodes[0];
7234 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007235 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007236 key.type != BTRFS_EXTENT_DATA_KEY) {
7237 /* not our file or wrong item type, must cow */
7238 goto out;
7239 }
7240
7241 if (key.offset > offset) {
7242 /* Wrong offset, must cow */
7243 goto out;
7244 }
7245
7246 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7247 found_type = btrfs_file_extent_type(leaf, fi);
7248 if (found_type != BTRFS_FILE_EXTENT_REG &&
7249 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7250 /* not a regular extent, must cow */
7251 goto out;
7252 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007253
7254 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7255 goto out;
7256
Miao Xiee77751a2013-12-27 21:11:50 +08007257 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7258 if (extent_end <= offset)
7259 goto out;
7260
Chris Mason46bfbb52010-05-26 11:04:10 -04007261 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007262 if (disk_bytenr == 0)
7263 goto out;
7264
7265 if (btrfs_file_extent_compression(leaf, fi) ||
7266 btrfs_file_extent_encryption(leaf, fi) ||
7267 btrfs_file_extent_other_encoding(leaf, fi))
7268 goto out;
7269
Ethan Lien78d42952018-05-17 14:58:29 +08007270 /*
7271 * Do the same check as in btrfs_cross_ref_exist but without the
7272 * unnecessary search.
7273 */
7274 if (btrfs_file_extent_generation(leaf, fi) <=
7275 btrfs_root_last_snapshot(&root->root_item))
7276 goto out;
7277
Chris Mason46bfbb52010-05-26 11:04:10 -04007278 backref_offset = btrfs_file_extent_offset(leaf, fi);
7279
Josef Bacik7ee9e442013-06-21 16:37:03 -04007280 if (orig_start) {
7281 *orig_start = key.offset - backref_offset;
7282 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7283 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7284 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007285
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007286 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007287 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007288
7289 num_bytes = min(offset + *len, extent_end) - offset;
7290 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7291 u64 range_end;
7292
Jeff Mahoneyda170662016-06-15 09:22:56 -04007293 range_end = round_up(offset + num_bytes,
7294 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007295 ret = test_range_bit(io_tree, offset, range_end,
7296 EXTENT_DELALLOC, 0, NULL);
7297 if (ret) {
7298 ret = -EAGAIN;
7299 goto out;
7300 }
7301 }
7302
Josef Bacik1bda19e2013-10-18 12:10:36 -04007303 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007304
7305 /*
7306 * look for other files referencing this extent, if we
7307 * find any we must cow
7308 */
Josef Bacik00361582013-08-14 14:02:47 -04007309
Liu Boe4c3b2d2017-01-30 12:25:28 -08007310 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007311 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007312 if (ret) {
7313 ret = 0;
7314 goto out;
7315 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007316
7317 /*
7318 * adjust disk_bytenr and num_bytes to cover just the bytes
7319 * in this extent we are about to write. If there
7320 * are any csums in that range we have to cow in order
7321 * to keep the csums correct
7322 */
7323 disk_bytenr += backref_offset;
7324 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007325 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7326 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007327 /*
7328 * all of the above have passed, it is safe to overwrite this extent
7329 * without cow
7330 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007331 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007332 ret = 1;
7333out:
7334 btrfs_free_path(path);
7335 return ret;
7336}
7337
Josef Bacikeb838e72012-07-31 16:28:48 -04007338static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7339 struct extent_state **cached_state, int writing)
7340{
7341 struct btrfs_ordered_extent *ordered;
7342 int ret = 0;
7343
7344 while (1) {
7345 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007346 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007347 /*
7348 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007349 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007350 * extents in this range.
7351 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007352 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007353 lockend - lockstart + 1);
7354
7355 /*
7356 * We need to make sure there are no buffered pages in this
7357 * range either, we could have raced between the invalidate in
7358 * generic_file_direct_write and locking the extent. The
7359 * invalidate needs to happen so that reads after a write do not
7360 * get stale data.
7361 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007362 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007363 (!writing || !filemap_range_has_page(inode->i_mapping,
7364 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007365 break;
7366
7367 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007368 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007369
7370 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007371 /*
7372 * If we are doing a DIO read and the ordered extent we
7373 * found is for a buffered write, we can not wait for it
7374 * to complete and retry, because if we do so we can
7375 * deadlock with concurrent buffered writes on page
7376 * locks. This happens only if our DIO read covers more
7377 * than one extent map, if at this point has already
7378 * created an ordered extent for a previous extent map
7379 * and locked its range in the inode's io tree, and a
7380 * concurrent write against that previous extent map's
7381 * range and this range started (we unlock the ranges
7382 * in the io tree only when the bios complete and
7383 * buffered writes always lock pages before attempting
7384 * to lock range in the io tree).
7385 */
7386 if (writing ||
7387 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7388 btrfs_start_ordered_extent(inode, ordered, 1);
7389 else
7390 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007391 btrfs_put_ordered_extent(ordered);
7392 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007393 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007394 * We could trigger writeback for this range (and wait
7395 * for it to complete) and then invalidate the pages for
7396 * this range (through invalidate_inode_pages2_range()),
7397 * but that can lead us to a deadlock with a concurrent
7398 * call to readpages() (a buffered read or a defrag call
7399 * triggered a readahead) on a page lock due to an
7400 * ordered dio extent we created before but did not have
7401 * yet a corresponding bio submitted (whence it can not
7402 * complete), which makes readpages() wait for that
7403 * ordered extent to complete while holding a lock on
7404 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007405 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007406 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007407 }
7408
Filipe Mananaade77022016-02-18 14:28:55 +00007409 if (ret)
7410 break;
7411
Josef Bacikeb838e72012-07-31 16:28:48 -04007412 cond_resched();
7413 }
7414
7415 return ret;
7416}
7417
Liu Bo6f9994d2017-01-31 07:50:22 -08007418/* The callers of this must take lock_extent() */
7419static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7420 u64 orig_start, u64 block_start,
7421 u64 block_len, u64 orig_block_len,
7422 u64 ram_bytes, int compress_type,
7423 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007424{
7425 struct extent_map_tree *em_tree;
7426 struct extent_map *em;
7427 struct btrfs_root *root = BTRFS_I(inode)->root;
7428 int ret;
7429
Liu Bo6f9994d2017-01-31 07:50:22 -08007430 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7431 type == BTRFS_ORDERED_COMPRESSED ||
7432 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007433 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007434
Josef Bacik69ffb542012-09-11 15:40:07 -04007435 em_tree = &BTRFS_I(inode)->extent_tree;
7436 em = alloc_extent_map();
7437 if (!em)
7438 return ERR_PTR(-ENOMEM);
7439
7440 em->start = start;
7441 em->orig_start = orig_start;
7442 em->len = len;
7443 em->block_len = block_len;
7444 em->block_start = block_start;
7445 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007446 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007447 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007448 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007449 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007450 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007451 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007452 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007453 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7454 em->compress_type = compress_type;
7455 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007456
7457 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007458 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007459 em->start + em->len - 1, 0);
7460 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007461 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007462 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007463 /*
7464 * The caller has taken lock_extent(), who could race with us
7465 * to add em?
7466 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007467 } while (ret == -EEXIST);
7468
7469 if (ret) {
7470 free_extent_map(em);
7471 return ERR_PTR(ret);
7472 }
7473
Liu Bo6f9994d2017-01-31 07:50:22 -08007474 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007475 return em;
7476}
7477
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007478
7479static int btrfs_get_blocks_direct_read(struct extent_map *em,
7480 struct buffer_head *bh_result,
7481 struct inode *inode,
7482 u64 start, u64 len)
7483{
7484 if (em->block_start == EXTENT_MAP_HOLE ||
7485 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7486 return -ENOENT;
7487
7488 len = min(len, em->len - (start - em->start));
7489
7490 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7491 inode->i_blkbits;
7492 bh_result->b_size = len;
7493 bh_result->b_bdev = em->bdev;
7494 set_buffer_mapped(bh_result);
7495
7496 return 0;
7497}
7498
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007499static int btrfs_get_blocks_direct_write(struct extent_map **map,
7500 struct buffer_head *bh_result,
7501 struct inode *inode,
7502 struct btrfs_dio_data *dio_data,
7503 u64 start, u64 len)
7504{
7505 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7506 struct extent_map *em = *map;
7507 int ret = 0;
7508
7509 /*
7510 * We don't allocate a new extent in the following cases
7511 *
7512 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7513 * existing extent.
7514 * 2) The extent is marked as PREALLOC. We're good to go here and can
7515 * just use the extent.
7516 *
7517 */
7518 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7519 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7520 em->block_start != EXTENT_MAP_HOLE)) {
7521 int type;
7522 u64 block_start, orig_start, orig_block_len, ram_bytes;
7523
7524 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7525 type = BTRFS_ORDERED_PREALLOC;
7526 else
7527 type = BTRFS_ORDERED_NOCOW;
7528 len = min(len, em->len - (start - em->start));
7529 block_start = em->block_start + (start - em->start);
7530
7531 if (can_nocow_extent(inode, start, &len, &orig_start,
7532 &orig_block_len, &ram_bytes) == 1 &&
7533 btrfs_inc_nocow_writers(fs_info, block_start)) {
7534 struct extent_map *em2;
7535
7536 em2 = btrfs_create_dio_extent(inode, start, len,
7537 orig_start, block_start,
7538 len, orig_block_len,
7539 ram_bytes, type);
7540 btrfs_dec_nocow_writers(fs_info, block_start);
7541 if (type == BTRFS_ORDERED_PREALLOC) {
7542 free_extent_map(em);
7543 *map = em = em2;
7544 }
7545
7546 if (em2 && IS_ERR(em2)) {
7547 ret = PTR_ERR(em2);
7548 goto out;
7549 }
7550 /*
7551 * For inode marked NODATACOW or extent marked PREALLOC,
7552 * use the existing or preallocated extent, so does not
7553 * need to adjust btrfs_space_info's bytes_may_use.
7554 */
7555 btrfs_free_reserved_data_space_noquota(inode, start,
7556 len);
7557 goto skip_cow;
7558 }
7559 }
7560
7561 /* this will cow the extent */
7562 len = bh_result->b_size;
7563 free_extent_map(em);
7564 *map = em = btrfs_new_extent_direct(inode, start, len);
7565 if (IS_ERR(em)) {
7566 ret = PTR_ERR(em);
7567 goto out;
7568 }
7569
7570 len = min(len, em->len - (start - em->start));
7571
7572skip_cow:
7573 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7574 inode->i_blkbits;
7575 bh_result->b_size = len;
7576 bh_result->b_bdev = em->bdev;
7577 set_buffer_mapped(bh_result);
7578
7579 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7580 set_buffer_new(bh_result);
7581
7582 /*
7583 * Need to update the i_size under the extent lock so buffered
7584 * readers will get the updated i_size when we unlock.
7585 */
7586 if (!dio_data->overwrite && start + len > i_size_read(inode))
7587 i_size_write(inode, start + len);
7588
7589 WARN_ON(dio_data->reserve < len);
7590 dio_data->reserve -= len;
7591 dio_data->unsubmitted_oe_range_end = start + len;
7592 current->journal_info = dio_data;
7593out:
7594 return ret;
7595}
7596
Josef Bacik4b46fce2010-05-23 11:00:55 -04007597static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7598 struct buffer_head *bh_result, int create)
7599{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007600 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007601 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007602 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307603 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007604 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007605 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007606 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007607 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007608 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007609
Miao Xie172a5042013-02-21 02:48:22 -07007610 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007611 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007612 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007613 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007614
Josef Bacikc3298612012-08-03 16:49:19 -04007615 lockstart = start;
7616 lockend = start + len - 1;
7617
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007618 if (current->journal_info) {
7619 /*
7620 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007621 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007622 * confused.
7623 */
chandan50745b02015-08-28 21:10:13 +05307624 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007625 current->journal_info = NULL;
7626 }
7627
Josef Bacikeb838e72012-07-31 16:28:48 -04007628 /*
7629 * If this errors out it's because we couldn't invalidate pagecache for
7630 * this range and we need to fallback to buffered.
7631 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007632 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7633 create)) {
7634 ret = -ENOTBLK;
7635 goto err;
7636 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007637
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007638 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007639 if (IS_ERR(em)) {
7640 ret = PTR_ERR(em);
7641 goto unlock_err;
7642 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007643
7644 /*
7645 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7646 * io. INLINE is special, and we could probably kludge it in here, but
7647 * it's still buffered so for safety lets just fall back to the generic
7648 * buffered path.
7649 *
7650 * For COMPRESSED we _have_ to read the entire extent in so we can
7651 * decompress it, so there will be buffering required no matter what we
7652 * do, so go ahead and fallback to buffered.
7653 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007654 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007655 * to buffered IO. Don't blame me, this is the price we pay for using
7656 * the generic code.
7657 */
7658 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7659 em->block_start == EXTENT_MAP_INLINE) {
7660 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007661 ret = -ENOTBLK;
7662 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007663 }
7664
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007665 if (create) {
7666 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7667 dio_data, start, len);
7668 if (ret < 0)
7669 goto unlock_err;
7670
7671 /* clear and unlock the entire range */
7672 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7673 unlock_bits, 1, 0, &cached_state);
7674 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007675 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7676 start, len);
7677 /* Can be negative only if we read from a hole */
7678 if (ret < 0) {
7679 ret = 0;
7680 free_extent_map(em);
7681 goto unlock_err;
7682 }
7683 /*
7684 * We need to unlock only the end area that we aren't using.
7685 * The rest is going to be unlocked by the endio routine.
7686 */
7687 lockstart = start + bh_result->b_size;
7688 if (lockstart < lockend) {
7689 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7690 lockend, unlock_bits, 1, 0,
7691 &cached_state);
7692 } else {
7693 free_extent_state(cached_state);
7694 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007695 }
7696
Josef Bacik4b46fce2010-05-23 11:00:55 -04007697 free_extent_map(em);
7698
7699 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007700
7701unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007702 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007703 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007704err:
chandan50745b02015-08-28 21:10:13 +05307705 if (dio_data)
7706 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007707 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007708}
7709
Omar Sandoval58efbc92017-08-22 23:45:59 -07007710static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7711 struct bio *bio,
7712 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007713{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007714 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007715 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007716
Mike Christie37226b22016-06-05 14:31:52 -05007717 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007718
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007719 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007720 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007721 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007722
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007723 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007724
Miao Xie8b110e32014-09-12 18:44:03 +08007725 return ret;
7726}
7727
7728static int btrfs_check_dio_repairable(struct inode *inode,
7729 struct bio *failed_bio,
7730 struct io_failure_record *failrec,
7731 int failed_mirror)
7732{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007734 int num_copies;
7735
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007736 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007737 if (num_copies == 1) {
7738 /*
7739 * we only have a single copy of the data, so don't bother with
7740 * all the retry and error correction code that follows. no
7741 * matter what the error is, it is very likely to persist.
7742 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007743 btrfs_debug(fs_info,
7744 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7745 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007746 return 0;
7747 }
7748
7749 failrec->failed_mirror = failed_mirror;
7750 failrec->this_mirror++;
7751 if (failrec->this_mirror == failed_mirror)
7752 failrec->this_mirror++;
7753
7754 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007755 btrfs_debug(fs_info,
7756 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7757 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007758 return 0;
7759 }
7760
7761 return 1;
7762}
7763
Omar Sandoval58efbc92017-08-22 23:45:59 -07007764static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7765 struct page *page, unsigned int pgoff,
7766 u64 start, u64 end, int failed_mirror,
7767 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007768{
7769 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007770 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7771 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007772 struct bio *bio;
7773 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007774 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007775 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007776 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007777 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007778 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007779
Mike Christie37226b22016-06-05 14:31:52 -05007780 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007781
7782 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7783 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007784 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007785
7786 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7787 failed_mirror);
7788 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007789 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007790 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007791 }
7792
Liu Bo17347ce2017-05-15 15:33:27 -07007793 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007794 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007795 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007796 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007797 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007798
7799 isector = start - btrfs_io_bio(failed_bio)->logical;
7800 isector >>= inode->i_sb->s_blocksize_bits;
7801 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307802 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007803 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007804
7805 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007806 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007807 read_mode, failrec->this_mirror, failrec->in_validation);
7808
Omar Sandoval58efbc92017-08-22 23:45:59 -07007809 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7810 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007811 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007812 bio_put(bio);
7813 }
7814
Omar Sandoval58efbc92017-08-22 23:45:59 -07007815 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007816}
7817
7818struct btrfs_retry_complete {
7819 struct completion done;
7820 struct inode *inode;
7821 u64 start;
7822 int uptodate;
7823};
7824
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007825static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007826{
7827 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007828 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007829 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007830 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007831 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08007832 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007833
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007834 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007835 goto end;
7836
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307837 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007838 io_tree = &BTRFS_I(inode)->io_tree;
7839 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007840 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307841
Miao Xie8b110e32014-09-12 18:44:03 +08007842 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007843 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08007844 bio_for_each_segment_all(bvec, bio, i, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007845 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7846 io_tree, done->start, bvec->bv_page,
7847 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007848end:
7849 complete(&done->done);
7850 bio_put(bio);
7851}
7852
Omar Sandoval58efbc92017-08-22 23:45:59 -07007853static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7854 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007855{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307856 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007857 struct bio_vec bvec;
7858 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007859 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007860 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307861 unsigned int pgoff;
7862 u32 sectorsize;
7863 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007864 blk_status_t ret;
7865 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007866
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307867 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007868 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307869
Miao Xiec1dc0892014-09-12 18:43:56 +08007870 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007871 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007872 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007873
Liu Bo17347ce2017-05-15 15:33:27 -07007874 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7875 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7876 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307877
7878next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007879 done.uptodate = 0;
7880 done.start = start;
7881 init_completion(&done.done);
7882
Liu Bo17347ce2017-05-15 15:33:27 -07007883 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307884 pgoff, start, start + sectorsize - 1,
7885 io_bio->mirror_num,
7886 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007887 if (ret) {
7888 err = ret;
7889 goto next;
7890 }
Miao Xie8b110e32014-09-12 18:44:03 +08007891
David Sterba9c17f6c2017-07-19 19:26:45 +02007892 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007893
7894 if (!done.uptodate) {
7895 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307896 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007897 }
7898
Liu Bo629ebf42017-05-15 17:20:07 -07007899next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307900 start += sectorsize;
7901
Liu Bo97bf5a52017-04-07 13:11:10 -07007902 nr_sectors--;
7903 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307904 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007905 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307906 goto next_block_or_try_again;
7907 }
Miao Xie8b110e32014-09-12 18:44:03 +08007908 }
7909
Liu Bo629ebf42017-05-15 17:20:07 -07007910 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007911}
7912
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007913static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007914{
7915 struct btrfs_retry_complete *done = bio->bi_private;
7916 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007917 struct extent_io_tree *io_tree, *failure_tree;
7918 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007919 struct bio_vec *bvec;
7920 int uptodate;
7921 int ret;
7922 int i;
Ming Lei6dc4f102019-02-15 19:13:19 +08007923 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007924
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007925 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007926 goto end;
7927
7928 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307929
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307930 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007931 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307932
Josef Bacik7870d082017-05-05 11:57:15 -04007933 io_tree = &BTRFS_I(inode)->io_tree;
7934 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7935
David Sterbac09abff2017-07-13 18:10:07 +02007936 ASSERT(!bio_flagged(bio, BIO_CLONED));
Ming Lei6dc4f102019-02-15 19:13:19 +08007937 bio_for_each_segment_all(bvec, bio, i, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04007938 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7939 bvec->bv_offset, done->start,
7940 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007941 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007942 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7943 failure_tree, io_tree, done->start,
7944 bvec->bv_page,
7945 btrfs_ino(BTRFS_I(inode)),
7946 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007947 else
7948 uptodate = 0;
7949 }
7950
7951 done->uptodate = uptodate;
7952end:
7953 complete(&done->done);
7954 bio_put(bio);
7955}
7956
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007957static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7958 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007959{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307960 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007961 struct bio_vec bvec;
7962 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007963 struct btrfs_retry_complete done;
7964 u64 start;
7965 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307966 u32 sectorsize;
7967 int nr_sectors;
7968 unsigned int pgoff;
7969 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07007970 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007971 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007972 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007973
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307974 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007975 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307976
Omar Sandoval58efbc92017-08-22 23:45:59 -07007977 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007978 start = io_bio->logical;
7979 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007980 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007981
Liu Bo17347ce2017-05-15 15:33:27 -07007982 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7983 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307984
Liu Bo17347ce2017-05-15 15:33:27 -07007985 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307986next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07007987 if (uptodate) {
7988 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
7989 ret = __readpage_endio_check(inode, io_bio, csum_pos,
7990 bvec.bv_page, pgoff, start, sectorsize);
7991 if (likely(!ret))
7992 goto next;
7993 }
Miao Xie8b110e32014-09-12 18:44:03 +08007994try_again:
7995 done.uptodate = 0;
7996 done.start = start;
7997 init_completion(&done.done);
7998
Omar Sandoval58efbc92017-08-22 23:45:59 -07007999 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8000 pgoff, start, start + sectorsize - 1,
8001 io_bio->mirror_num, btrfs_retry_endio,
8002 &done);
8003 if (status) {
8004 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008005 goto next;
8006 }
8007
David Sterba9c17f6c2017-07-19 19:26:45 +02008008 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008009
8010 if (!done.uptodate) {
8011 /* We might have another mirror, so try again */
8012 goto try_again;
8013 }
8014next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308015 offset += sectorsize;
8016 start += sectorsize;
8017
8018 ASSERT(nr_sectors);
8019
Liu Bo97bf5a52017-04-07 13:11:10 -07008020 nr_sectors--;
8021 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308022 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008023 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308024 goto next_block;
8025 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008026 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008027
8028 return err;
8029}
8030
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008031static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8032 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008033{
8034 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8035
8036 if (skip_csum) {
8037 if (unlikely(err))
8038 return __btrfs_correct_data_nocsum(inode, io_bio);
8039 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008040 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008041 } else {
8042 return __btrfs_subio_endio_read(inode, io_bio, err);
8043 }
8044}
8045
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008046static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008047{
8048 struct btrfs_dio_private *dip = bio->bi_private;
8049 struct inode *inode = dip->inode;
8050 struct bio *dio_bio;
8051 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008052 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008053
Liu Bo99c4e3b92017-09-15 15:06:51 -06008054 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008055 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008056
Josef Bacik4b46fce2010-05-23 11:00:55 -04008057 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008058 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008059 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008060
Josef Bacik4b46fce2010-05-23 11:00:55 -04008061 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008062
Liu Bo99c4e3b92017-09-15 15:06:51 -06008063 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008064 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008065 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008066 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008067}
8068
Qu Wenruo524272602017-03-08 10:25:52 +08008069static void __endio_write_update_ordered(struct inode *inode,
8070 const u64 offset, const u64 bytes,
8071 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008072{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008073 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008074 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008075 struct btrfs_workqueue *wq;
8076 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008077 u64 ordered_offset = offset;
8078 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008079 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008080
Qu Wenruo524272602017-03-08 10:25:52 +08008081 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8082 wq = fs_info->endio_freespace_worker;
8083 func = btrfs_freespace_write_helper;
8084 } else {
8085 wq = fs_info->endio_write_workers;
8086 func = btrfs_endio_write_helper;
8087 }
8088
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008089 while (ordered_offset < offset + bytes) {
8090 last_offset = ordered_offset;
8091 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8092 &ordered_offset,
8093 ordered_bytes,
8094 uptodate)) {
8095 btrfs_init_work(&ordered->work, func,
8096 finish_ordered_fn,
8097 NULL, NULL);
8098 btrfs_queue_work(wq, &ordered->work);
8099 }
8100 /*
8101 * If btrfs_dec_test_ordered_pending does not find any ordered
8102 * extent in the range, we can exit.
8103 */
8104 if (ordered_offset == last_offset)
8105 return;
8106 /*
8107 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008108 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008109 */
8110 if (ordered_offset < offset + bytes) {
8111 ordered_bytes = offset + bytes - ordered_offset;
8112 ordered = NULL;
8113 }
Chris Mason163cf092010-11-28 19:56:33 -05008114 }
Filipe Manana14543772015-11-24 16:23:54 +00008115}
8116
8117static void btrfs_endio_direct_write(struct bio *bio)
8118{
8119 struct btrfs_dio_private *dip = bio->bi_private;
8120 struct bio *dio_bio = dip->dio_bio;
8121
Qu Wenruo524272602017-03-08 10:25:52 +08008122 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008123 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008124
Josef Bacik4b46fce2010-05-23 11:00:55 -04008125 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008126
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008127 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008128 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008129 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008130}
8131
David Sterbad0ee3932018-03-08 14:35:48 +01008132static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008133 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008134{
Josef Bacikc6100a42017-05-05 11:57:13 -04008135 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008136 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008137 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008138 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008139 return 0;
8140}
8141
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008142static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008143{
8144 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008145 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008146
Miao Xie8b110e32014-09-12 18:44:03 +08008147 if (err)
8148 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008149 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008150 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8151 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008152 (unsigned long long)bio->bi_iter.bi_sector,
8153 bio->bi_iter.bi_size, err);
8154
8155 if (dip->subio_endio)
8156 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008157
8158 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008159 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008160 * We want to perceive the errors flag being set before
8161 * decrementing the reference count. We don't need a barrier
8162 * since atomic operations with a return value are fully
8163 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008164 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008165 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008166 }
8167
8168 /* if there are more bios still pending for this dio, just exit */
8169 if (!atomic_dec_and_test(&dip->pending_bios))
8170 goto out;
8171
Chris Mason9be33952013-05-17 18:30:14 -04008172 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008173 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008174 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008175 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008176 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008177 }
8178out:
8179 bio_put(bio);
8180}
8181
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008182static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008183 struct btrfs_dio_private *dip,
8184 struct bio *bio,
8185 u64 file_offset)
8186{
8187 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8188 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008189 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008190
8191 /*
8192 * We load all the csum data we need when we submit
8193 * the first bio to reduce the csum tree search and
8194 * contention.
8195 */
8196 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008197 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008198 file_offset);
8199 if (ret)
8200 return ret;
8201 }
8202
8203 if (bio == dip->orig_bio)
8204 return 0;
8205
8206 file_offset -= dip->logical_offset;
8207 file_offset >>= inode->i_sb->s_blocksize_bits;
8208 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8209
8210 return 0;
8211}
8212
David Sterbad0ee3932018-03-08 14:35:48 +01008213static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8214 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008215{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008216 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008217 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008218 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008219 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008220
Liu Bo4c274bc2017-11-01 17:19:27 -06008221 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008222 if (async_submit)
8223 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8224
Josef Bacik5fd02042012-05-02 14:00:54 -04008225 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008226 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008227 if (ret)
8228 goto err;
8229 }
Miao Xiee65e1532010-11-22 03:04:43 +00008230
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008231 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008232 goto map;
8233
8234 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008235 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8236 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008237 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008238 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008239 } else if (write) {
8240 /*
8241 * If we aren't doing async submit, calculate the csum of the
8242 * bio now.
8243 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008244 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008245 if (ret)
8246 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008247 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008248 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008249 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008250 if (ret)
8251 goto err;
8252 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008253map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008254 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008255err:
Miao Xiee65e1532010-11-22 03:04:43 +00008256 return ret;
8257}
8258
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008259static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008260{
8261 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008262 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008263 struct bio *bio;
8264 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008265 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008266 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008267 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008268 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008269 u64 submit_len;
8270 int clone_offset = 0;
8271 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308272 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008273 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008274
Kent Overstreet4f024f32013-10-11 15:44:27 -07008275 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008276 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008277 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8278 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008279 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008280 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008281
Liu Bo725130b2017-05-16 09:51:39 -07008282 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008283 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008284 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008285 goto submit;
8286 }
8287
David Woodhouse53b381b2013-01-29 18:40:14 -05008288 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008289 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008290 async_submit = 0;
8291 else
8292 async_submit = 1;
8293
Liu Bo725130b2017-05-16 09:51:39 -07008294 /* bio split */
8295 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008296 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008297 do {
Liu Bo725130b2017-05-16 09:51:39 -07008298 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008299
Liu Bo725130b2017-05-16 09:51:39 -07008300 /*
8301 * This will never fail as it's passing GPF_NOFS and
8302 * the allocation is backed by btrfs_bioset.
8303 */
Liu Boe4770942017-05-16 10:57:14 -07008304 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008305 clone_len);
8306 bio->bi_private = dip;
8307 bio->bi_end_io = btrfs_end_dio_bio;
8308 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008309
Liu Bo725130b2017-05-16 09:51:39 -07008310 ASSERT(submit_len >= clone_len);
8311 submit_len -= clone_len;
8312 if (submit_len == 0)
8313 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008314
Liu Bo725130b2017-05-16 09:51:39 -07008315 /*
8316 * Increase the count before we submit the bio so we know
8317 * the end IO handler won't happen before we increase the
8318 * count. Otherwise, the dip might get freed before we're
8319 * done setting it up.
8320 */
8321 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008322
David Sterbad0ee3932018-03-08 14:35:48 +01008323 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008324 async_submit);
8325 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008326 bio_put(bio);
8327 atomic_dec(&dip->pending_bios);
8328 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008329 }
Liu Bo725130b2017-05-16 09:51:39 -07008330
8331 clone_offset += clone_len;
8332 start_sector += clone_len >> 9;
8333 file_offset += clone_len;
8334
8335 map_length = submit_len;
8336 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8337 start_sector << 9, &map_length, NULL, 0);
8338 if (ret)
8339 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008340 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008341
Josef Bacik02f57c72011-04-06 14:25:44 -04008342submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008343 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008344 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008345 return 0;
8346
8347 bio_put(bio);
8348out_err:
8349 dip->errors = 1;
8350 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008351 * Before atomic variable goto zero, we must make sure dip->errors is
8352 * perceived to be set. This ordering is ensured by the fact that an
8353 * atomic operations with a return value are fully ordered as per
8354 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008355 */
Miao Xiee65e1532010-11-22 03:04:43 +00008356 if (atomic_dec_and_test(&dip->pending_bios))
8357 bio_io_error(dip->orig_bio);
8358
8359 /* bio_end_io() will handle error, so we needn't return it */
8360 return 0;
8361}
8362
Mike Christie8a4c1e42016-06-05 14:31:50 -05008363static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8364 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008365{
Filipe Manana61de7182015-07-01 12:13:10 +01008366 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008367 struct bio *bio = NULL;
8368 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008369 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008370 int ret = 0;
8371
David Sterba8b6c1d52017-06-02 17:48:13 +02008372 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008373
Miao Xiec1dc0892014-09-12 18:43:56 +08008374 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008375 if (!dip) {
8376 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008377 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008378 }
8379
8380 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008381 dip->inode = inode;
8382 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008383 dip->bytes = dio_bio->bi_iter.bi_size;
8384 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008385 bio->bi_private = dip;
8386 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008387 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008388 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008389 io_bio = btrfs_io_bio(bio);
8390 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008391
Miao Xiec1dc0892014-09-12 18:43:56 +08008392 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008393 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008394 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008395 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008396 dip->subio_endio = btrfs_subio_endio_read;
8397 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008398
Filipe Mananaf28a4922015-12-08 19:23:20 +00008399 /*
8400 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8401 * even if we fail to submit a bio, because in such case we do the
8402 * corresponding error handling below and it must not be done a second
8403 * time by btrfs_direct_IO().
8404 */
8405 if (write) {
8406 struct btrfs_dio_data *dio_data = current->journal_info;
8407
8408 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8409 dip->bytes;
8410 dio_data->unsubmitted_oe_range_start =
8411 dio_data->unsubmitted_oe_range_end;
8412 }
8413
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008414 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008415 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008416 return;
Chris Mason9be33952013-05-17 18:30:14 -04008417
David Sterbab3a0dd52018-11-22 17:16:49 +01008418 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008419
Josef Bacik4b46fce2010-05-23 11:00:55 -04008420free_ordered:
8421 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008422 * If we arrived here it means either we failed to submit the dip
8423 * or we either failed to clone the dio_bio or failed to allocate the
8424 * dip. If we cloned the dio_bio and allocated the dip, we can just
8425 * call bio_endio against our io_bio so that we get proper resource
8426 * cleanup if we fail to submit the dip, otherwise, we must do the
8427 * same as btrfs_endio_direct_[write|read] because we can't call these
8428 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008429 */
Liu Bo3892ac92017-04-17 15:00:28 -07008430 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008431 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008432 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008433 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008434 * and all the cleanup and final put for dio_bio (through
8435 * dio_end_io()).
8436 */
8437 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008438 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008439 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008440 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008441 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008442 file_offset,
8443 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008444 false);
Filipe Manana14543772015-11-24 16:23:54 +00008445 else
Filipe Manana61de7182015-07-01 12:13:10 +01008446 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8447 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008448
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008449 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008450 /*
8451 * Releases and cleans up our dio_bio, no need to bio_put()
8452 * nor bio_endio()/bio_io_error() against dio_bio.
8453 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008454 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008455 }
Liu Bo3892ac92017-04-17 15:00:28 -07008456 if (bio)
8457 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008458 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008459}
8460
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008461static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008462 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008463{
8464 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008465 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008466 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008467 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008468
8469 if (offset & blocksize_mask)
8470 goto out;
8471
Al Viro28060d52014-03-22 05:15:17 -04008472 if (iov_iter_alignment(iter) & blocksize_mask)
8473 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008474
Al Viro28060d52014-03-22 05:15:17 -04008475 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008476 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008477 return 0;
8478 /*
8479 * Check to make sure we don't have duplicate iov_base's in this
8480 * iovec, if so return EINVAL, otherwise we'll get csum errors
8481 * when reading back.
8482 */
8483 for (seg = 0; seg < iter->nr_segs; seg++) {
8484 for (i = seg + 1; i < iter->nr_segs; i++) {
8485 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008486 goto out;
8487 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008488 }
8489 retval = 0;
8490out:
8491 return retval;
8492}
Josef Bacikeb838e72012-07-31 16:28:48 -04008493
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008494static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008495{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008496 struct file *file = iocb->ki_filp;
8497 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008498 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308499 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008500 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008501 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008502 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008503 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008504 bool wakeup = true;
8505 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008506 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008507
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008508 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008509 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008510
Jens Axboefe0f07d2015-04-15 17:05:48 -06008511 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008512
Josef Bacik0e267c42013-07-02 10:38:02 -04008513 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008514 * The generic stuff only does filemap_write_and_wait_range, which
8515 * isn't enough if we've written compressed pages to this area, so
8516 * we need to flush the dirty pages again to make absolutely sure
8517 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008518 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008519 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008520 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8521 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008522 filemap_fdatawrite_range(inode->i_mapping, offset,
8523 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008524
Omar Sandoval6f673762015-03-16 04:33:52 -07008525 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008526 /*
8527 * If the write DIO is beyond the EOF, we need update
8528 * the isize, but it is protected by i_mutex. So we can
8529 * not unlock the i_mutex at this case.
8530 */
8531 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008532 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008533 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008534 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008535 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8536 ret = -EAGAIN;
8537 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008538 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008539 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8540 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008541 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008542 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008543
8544 /*
8545 * We need to know how many extents we reserved so that we can
8546 * do the accounting properly if we go over the number we
8547 * originally calculated. Abuse current->journal_info for this.
8548 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008549 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008550 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008551 dio_data.unsubmitted_oe_range_start = (u64)offset;
8552 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308553 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308554 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008555 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8556 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008557 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008558 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8559 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008560 }
8561
Omar Sandoval17f8c842015-03-16 04:33:50 -07008562 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008563 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008564 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008565 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008566 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308567 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008568 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008569 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308570 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008571 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008572 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008573 /*
8574 * On error we might have left some ordered extents
8575 * without submitting corresponding bios for them, so
8576 * cleanup them up to avoid other tasks getting them
8577 * and waiting for them to complete forever.
8578 */
8579 if (dio_data.unsubmitted_oe_range_start <
8580 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008581 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008582 dio_data.unsubmitted_oe_range_start,
8583 dio_data.unsubmitted_oe_range_end -
8584 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008585 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008586 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008587 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008588 offset, count - (size_t)ret, true);
8589 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008590 }
Miao Xie38851cc2013-02-08 07:04:11 +00008591out:
Miao Xie2e60a512013-02-08 07:01:08 +00008592 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008593 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008594 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008595 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008596
Qu Wenruo364ecf32017-02-27 15:10:38 +08008597 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008598 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008599}
8600
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008601#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8602
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008603static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8604 __u64 start, __u64 len)
8605{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008606 int ret;
8607
8608 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8609 if (ret)
8610 return ret;
8611
David Sterba2135fb92017-06-23 04:09:57 +02008612 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008613}
8614
Chris Mason9ebefb182007-06-15 13:50:00 -04008615int btrfs_readpage(struct file *file, struct page *page)
8616{
Chris Masond1310b22008-01-24 16:13:08 -05008617 struct extent_io_tree *tree;
8618 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008619 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008620}
Chris Mason1832a6d2007-12-21 16:27:21 -05008621
Chris Mason39279cc2007-06-12 06:35:45 -04008622static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8623{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008624 struct inode *inode = page->mapping->host;
8625 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008626
8627 if (current->flags & PF_MEMALLOC) {
8628 redirty_page_for_writepage(wbc, page);
8629 unlock_page(page);
8630 return 0;
8631 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008632
8633 /*
8634 * If we are under memory pressure we will call this directly from the
8635 * VM, we need to make sure we have the inode referenced for the ordered
8636 * extent. If not just return like we didn't do anything.
8637 */
8638 if (!igrab(inode)) {
8639 redirty_page_for_writepage(wbc, page);
8640 return AOP_WRITEPAGE_ACTIVATE;
8641 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008642 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008643 btrfs_add_delayed_iput(inode);
8644 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008645}
Chris Mason39279cc2007-06-12 06:35:45 -04008646
Eric Sandeen48a3b632013-04-25 20:41:01 +00008647static int btrfs_writepages(struct address_space *mapping,
8648 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008649{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008650 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008651}
8652
Chris Mason3ab2fb52007-11-08 10:59:22 -05008653static int
8654btrfs_readpages(struct file *file, struct address_space *mapping,
8655 struct list_head *pages, unsigned nr_pages)
8656{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008657 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008658}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008659
Chris Masone6dcd2d2008-07-17 12:53:50 -04008660static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008661{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008662 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008663 if (ret == 1) {
8664 ClearPagePrivate(page);
8665 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008666 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008667 }
8668 return ret;
8669}
Chris Mason39279cc2007-06-12 06:35:45 -04008670
Chris Masone6dcd2d2008-07-17 12:53:50 -04008671static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8672{
Chris Mason98509cf2008-09-11 15:51:43 -04008673 if (PageWriteback(page) || PageDirty(page))
8674 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008675 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008676}
8677
Lukas Czernerd47992f2013-05-21 23:17:23 -04008678static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8679 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008680{
Josef Bacik5fd02042012-05-02 14:00:54 -04008681 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008682 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008683 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008684 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008685 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008686 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308687 u64 start;
8688 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008689 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008690
Chris Mason8b62b722009-09-02 16:53:46 -04008691 /*
8692 * we have the page locked, so new writeback can't start,
8693 * and the dirty bit won't be cleared while we are here.
8694 *
8695 * Wait for IO on this page so that we can safely clear
8696 * the PagePrivate2 bit and do ordered accounting
8697 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008698 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008699
Josef Bacik5fd02042012-05-02 14:00:54 -04008700 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008701 if (offset) {
8702 btrfs_releasepage(page, GFP_NOFS);
8703 return;
8704 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008705
8706 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008707 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308708again:
8709 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008710 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308711 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008712 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308713 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008714 /*
8715 * IO on this page will never be started, so we need
8716 * to account for any ordered extents now
8717 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008718 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308719 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008720 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008721 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008722 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008723 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008724 /*
8725 * whoever cleared the private bit is responsible
8726 * for the finish_ordered_io
8727 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008728 if (TestClearPagePrivate2(page)) {
8729 struct btrfs_ordered_inode_tree *tree;
8730 u64 new_len;
8731
8732 tree = &BTRFS_I(inode)->ordered_tree;
8733
8734 spin_lock_irq(&tree->lock);
8735 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308736 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008737 if (new_len < ordered->truncated_len)
8738 ordered->truncated_len = new_len;
8739 spin_unlock_irq(&tree->lock);
8740
8741 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308742 start,
8743 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008744 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008745 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008746 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008747 if (!inode_evicting) {
8748 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308749 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008750 &cached_state);
8751 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308752
8753 start = end + 1;
8754 if (start < page_end)
8755 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008756 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008757
Qu Wenruob9d0b382015-09-29 10:35:16 +08008758 /*
8759 * Qgroup reserved space handler
8760 * Page here will be either
8761 * 1) Already written to disk
8762 * In this case, its reserved space is released from data rsv map
8763 * and will be freed by delayed_ref handler finally.
8764 * So even we call qgroup_free_data(), it won't decrease reserved
8765 * space.
8766 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008767 * This means the reserved space should be freed here. However,
8768 * if a truncate invalidates the page (by clearing PageDirty)
8769 * and the page is accounted for while allocating extent
8770 * in btrfs_check_data_free_space() we let delayed_ref to
8771 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008772 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008773 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008774 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008775 if (!inode_evicting) {
8776 clear_extent_bit(tree, page_start, page_end,
8777 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008778 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8779 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008780 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008781
8782 __btrfs_releasepage(page, GFP_NOFS);
8783 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008784
Chris Mason4a096752008-07-21 10:29:44 -04008785 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008786 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008787 ClearPagePrivate(page);
8788 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008789 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008790 }
Chris Mason39279cc2007-06-12 06:35:45 -04008791}
8792
Chris Mason9ebefb182007-06-15 13:50:00 -04008793/*
8794 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8795 * called from a page fault handler when a page is first dirtied. Hence we must
8796 * be careful to check for EOF conditions here. We set the page up correctly
8797 * for a written page which means we get ENOSPC checking when writing into
8798 * holes and correct delalloc and unwritten extent mapping on filesystems that
8799 * support these features.
8800 *
8801 * We are not allowed to take the i_mutex here so we have to play games to
8802 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008803 * truncate_setsize() writes the inode size before removing pages, once we have
8804 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008805 * beyond EOF, then the page is guaranteed safe against truncation until we
8806 * unlock the page.
8807 */
Souptick Joardera528a242018-06-06 19:54:44 +05308808vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008809{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008810 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008811 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008812 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008813 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8814 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008815 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008816 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008817 char *kaddr;
8818 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008819 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308820 vm_fault_t ret;
8821 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008822 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308823 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008824 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008825 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308826 u64 end;
8827
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008828 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008829
Jan Karab2b5ef52012-06-12 16:20:45 +02008830 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008831 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008832 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308833 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008834
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308835 /*
8836 * Reserving delalloc space after obtaining the page lock can lead to
8837 * deadlock. For example, if a dirty page is locked by this function
8838 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8839 * dirty page write out, then the btrfs_writepage() function could
8840 * end up waiting indefinitely to get a lock on the page currently
8841 * being processed by btrfs_page_mkwrite() function.
8842 */
Souptick Joardera528a242018-06-06 19:54:44 +05308843 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308844 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308845 if (!ret2) {
8846 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008847 reserved = 1;
8848 }
Souptick Joardera528a242018-06-06 19:54:44 +05308849 if (ret2) {
8850 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008851 if (reserved)
8852 goto out;
8853 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008854 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008855
Nick Piggin56a76f82009-03-31 15:23:23 -07008856 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008857again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008858 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008859 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008860
Chris Mason9ebefb182007-06-15 13:50:00 -04008861 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008862 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008863 /* page got truncated out from underneath us */
8864 goto out_unlock;
8865 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008866 wait_on_page_writeback(page);
8867
David Sterbaff13db42015-12-03 14:30:40 +01008868 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008869 set_page_extent_mapped(page);
8870
Chris Masoneb84ae02008-07-17 13:53:27 -04008871 /*
8872 * we can't set the delalloc bits if there are pending ordered
8873 * extents. Drop our locks and wait for them to finish
8874 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008875 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8876 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008877 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008878 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008879 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008880 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008881 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008882 btrfs_put_ordered_extent(ordered);
8883 goto again;
8884 }
8885
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008886 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008887 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008888 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008889 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308890 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008891 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008892 page_start, PAGE_SIZE - reserved_space,
8893 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308894 }
8895 }
8896
Josef Bacikfbf19082009-10-01 17:10:23 -04008897 /*
Liu Bo54160342016-12-13 12:15:19 -08008898 * page_mkwrite gets called when the page is firstly dirtied after it's
8899 * faulted in, but write(2) could also dirty a page and set delalloc
8900 * bits, thus in this case for space account reason, we still need to
8901 * clear any delalloc bits within this page range since we have to
8902 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008903 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308904 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008905 EXTENT_DIRTY | EXTENT_DELALLOC |
8906 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008907 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008908
Souptick Joardera528a242018-06-06 19:54:44 +05308909 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008910 &cached_state, 0);
Souptick Joardera528a242018-06-06 19:54:44 +05308911 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008912 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008913 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008914 ret = VM_FAULT_SIGBUS;
8915 goto out_unlock;
8916 }
Souptick Joardera528a242018-06-06 19:54:44 +05308917 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008918
8919 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008920 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008921 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008922 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008923 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008924
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008925 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008926 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008927 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008928 flush_dcache_page(page);
8929 kunmap(page);
8930 }
Chris Mason247e7432008-07-17 12:53:51 -04008931 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008932 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008933 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008934
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008935 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008936 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008937 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008938
David Sterbae43bbe52017-12-12 21:43:52 +01008939 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008940
Souptick Joardera528a242018-06-06 19:54:44 +05308941 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08008942 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02008943 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008944 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04008945 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008946 }
Chris Mason717beb92018-06-25 10:03:41 -07008947
8948out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008949 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008950out:
Qu Wenruo43b18592017-12-12 15:34:32 +08008951 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08008952 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008953 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008954out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008955 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008956 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008957 return ret;
8958}
8959
Filipe Manana213e8c52018-02-06 20:40:31 +00008960static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008961{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008962 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008963 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008964 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008965 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008966 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008967 u64 mask = fs_info->sectorsize - 1;
8968 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008969
Filipe Manana213e8c52018-02-06 20:40:31 +00008970 if (!skip_writeback) {
8971 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8972 (u64)-1);
8973 if (ret)
8974 return ret;
8975 }
Chris Mason39279cc2007-06-12 06:35:45 -04008976
Josef Bacikfcb80c22011-05-03 10:40:22 -04008977 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008978 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8979 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008980 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008981 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008982 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008983 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008984 * be free'd up by the truncate operation, but also have some slack
8985 * space reserved in case it uses space during the truncate (thank you
8986 * very much snapshotting).
8987 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008988 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008989 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008990 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008991 * doesn't end up using space reserved for updating the inode. We also
8992 * need to be able to stop the transaction and start a new one, which
8993 * means we need to be able to update the inode several times, and we
8994 * have no idea of knowing how many times that will be, so we can't just
8995 * reserve 1 item for the entirety of the operation, so that has to be
8996 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008997 *
8998 * So that leaves us with
8999 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009000 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04009001 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07009002 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04009003 * updating the inode.
9004 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009005 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009006 if (!rsv)
9007 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009008 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009009 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009010
Josef Bacik907cbce2011-08-08 13:46:15 -04009011 /*
Josef Bacik07127182011-08-19 10:29:59 -04009012 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009013 * 1 for updating the inode.
9014 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009015 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009016 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009017 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009018 goto out;
9019 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009020
Josef Bacik907cbce2011-08-08 13:46:15 -04009021 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009022 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009023 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009024 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009025
Chris Mason5a3f23d2009-03-31 13:27:11 -04009026 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009027 * So if we truncate and then write and fsync we normally would just
9028 * write the extents that changed, which is a problem if we need to
9029 * first truncate that entire inode. So set this flag so we write out
9030 * all of the extents in the inode to the sync log so we're completely
9031 * safe.
9032 */
9033 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009034 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009035
Yan, Zheng80825102009-11-12 09:35:36 +00009036 while (1) {
9037 ret = btrfs_truncate_inode_items(trans, root, inode,
9038 inode->i_size,
9039 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009040 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009041 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00009042 break;
Chris Mason39279cc2007-06-12 06:35:45 -04009043
Yan, Zheng80825102009-11-12 09:35:36 +00009044 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009045 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05009046 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009047
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009048 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009049 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009050
9051 trans = btrfs_start_transaction(root, 2);
9052 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009053 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009054 trans = NULL;
9055 break;
9056 }
9057
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009058 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009059 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009060 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009061 BUG_ON(ret); /* shouldn't happen */
9062 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009063 }
9064
Josef Bacikddfae632017-10-19 14:16:02 -04009065 /*
9066 * We can't call btrfs_truncate_block inside a trans handle as we could
9067 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9068 * we've truncated everything except the last little bit, and can do
9069 * btrfs_truncate_block and then update the disk_i_size.
9070 */
9071 if (ret == NEED_TRUNCATE_BLOCK) {
9072 btrfs_end_transaction(trans);
9073 btrfs_btree_balance_dirty(fs_info);
9074
9075 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9076 if (ret)
9077 goto out;
9078 trans = btrfs_start_transaction(root, 1);
9079 if (IS_ERR(trans)) {
9080 ret = PTR_ERR(trans);
9081 goto out;
9082 }
9083 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9084 }
9085
Chris Mason917c16b2011-11-08 14:49:59 -05009086 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009087 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009088
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009089 trans->block_rsv = &fs_info->trans_block_rsv;
9090 ret2 = btrfs_update_inode(trans, root, inode);
9091 if (ret2 && !ret)
9092 ret = ret2;
9093
9094 ret2 = btrfs_end_transaction(trans);
9095 if (ret2 && !ret)
9096 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009097 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009098 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009099out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009100 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009101
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009102 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009103}
9104
Sven Wegener3b963622008-06-09 21:57:42 -04009105/*
Chris Masond352ac62008-09-29 15:18:18 -04009106 * create a new subvolume directory/inode (helper for the ioctl).
9107 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009108int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009109 struct btrfs_root *new_root,
9110 struct btrfs_root *parent_root,
9111 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009112{
Chris Mason39279cc2007-06-12 06:35:45 -04009113 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009114 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009115 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009116
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009117 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9118 new_dirid, new_dirid,
9119 S_IFDIR | (~current_umask() & S_IRWXUGO),
9120 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009121 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009122 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009123 inode->i_op = &btrfs_dir_inode_operations;
9124 inode->i_fop = &btrfs_dir_file_operations;
9125
Miklos Szeredibfe86842011-10-28 14:13:29 +02009126 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009127 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009128 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009129
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009130 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9131 if (err)
9132 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009133 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009134 new_root->root_key.objectid, err);
9135
Yan, Zheng76dda932009-09-21 16:00:26 -04009136 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009137
Yan, Zheng76dda932009-09-21 16:00:26 -04009138 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009139 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009140}
9141
Chris Mason39279cc2007-06-12 06:35:45 -04009142struct inode *btrfs_alloc_inode(struct super_block *sb)
9143{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009144 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009145 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009146 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009147
David Sterba712e36c2017-10-31 17:08:27 +01009148 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009149 if (!ei)
9150 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009151
9152 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009153 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009154 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009155 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009156 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009157 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009158 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009159 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009160 ei->disk_i_size = 0;
9161 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009162 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009163 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009164 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009165 ei->last_unlink_trans = 0;
Filipe Manana41bd6062018-11-28 14:54:28 +00009166 ei->last_link_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009167 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009168
Josef Bacik9e0baf62011-07-15 15:16:44 +00009169 spin_lock_init(&ei->lock);
9170 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009171 if (sb->s_magic != BTRFS_TEST_MAGIC)
9172 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9173 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009174 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009175 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009176 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009177
Miao Xie16cdcec2011-04-22 18:12:22 +08009178 ei->delayed_node = NULL;
9179
chandan r9cc97d62012-07-04 12:48:07 +05309180 ei->i_otime.tv_sec = 0;
9181 ei->i_otime.tv_nsec = 0;
9182
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009183 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009184 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009185 extent_io_tree_init(&ei->io_tree, inode);
9186 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009187 ei->io_tree.track_uptodate = 1;
9188 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009189 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009190 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009191 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009192 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009193 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009194 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009195 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009196 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009197
9198 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009199}
9200
Josef Bacikaaedb552013-10-11 14:44:09 -04009201#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9202void btrfs_test_destroy_inode(struct inode *inode)
9203{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009204 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009205 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9206}
9207#endif
9208
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009209static void btrfs_i_callback(struct rcu_head *head)
9210{
9211 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009212 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9213}
9214
Chris Mason39279cc2007-06-12 06:35:45 -04009215void btrfs_destroy_inode(struct inode *inode)
9216{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009217 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009218 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009219 struct btrfs_root *root = BTRFS_I(inode)->root;
9220
Al Virob3d9b7a2012-06-09 13:51:19 -04009221 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009222 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009223 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9224 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009225 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009226 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009227 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009228 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009229 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009230
Chris Mason5a3f23d2009-03-31 13:27:11 -04009231 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009232 * This can happen where we create an inode, but somebody else also
9233 * created the same inode and we need to destroy the one we already
9234 * created.
9235 */
9236 if (!root)
9237 goto free;
9238
Chris Masond3977122009-01-05 21:25:51 -05009239 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009240 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9241 if (!ordered)
9242 break;
9243 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009244 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009245 "found ordered extent %llu %llu on inode cleanup",
9246 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009247 btrfs_remove_ordered_extent(inode, ordered);
9248 btrfs_put_ordered_extent(ordered);
9249 btrfs_put_ordered_extent(ordered);
9250 }
9251 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009252 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009253 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009254 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009255free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009256 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009257}
9258
Al Viro45321ac2010-06-07 13:43:19 -04009259int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009260{
9261 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009262
Naohiro Aota6379ef92013-06-06 09:56:34 +00009263 if (root == NULL)
9264 return 1;
9265
Liu Bofa6ac872013-02-20 14:10:23 +00009266 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009267 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009268 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009269 else
Al Viro45321ac2010-06-07 13:43:19 -04009270 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009271}
9272
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009273static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009274{
9275 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9276
9277 inode_init_once(&ei->vfs_inode);
9278}
9279
David Sterbae67c7182018-02-19 17:24:18 +01009280void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009281{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009282 /*
9283 * Make sure all delayed rcu free inodes are flushed before we
9284 * destroy cache.
9285 */
9286 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009287 kmem_cache_destroy(btrfs_inode_cachep);
9288 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009289 kmem_cache_destroy(btrfs_path_cachep);
9290 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009291}
9292
Liu Bof5c29bd2017-11-02 17:21:50 -06009293int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009294{
David Sterba837e1972012-09-07 03:00:48 -06009295 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009296 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009297 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9298 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009299 if (!btrfs_inode_cachep)
9300 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009301
David Sterba837e1972012-09-07 03:00:48 -06009302 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009303 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009304 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009305 if (!btrfs_trans_handle_cachep)
9306 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009307
David Sterba837e1972012-09-07 03:00:48 -06009308 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009309 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009310 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009311 if (!btrfs_path_cachep)
9312 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009313
David Sterba837e1972012-09-07 03:00:48 -06009314 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009315 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009316 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009317 if (!btrfs_free_space_cachep)
9318 goto fail;
9319
Chris Mason39279cc2007-06-12 06:35:45 -04009320 return 0;
9321fail:
9322 btrfs_destroy_cachep();
9323 return -ENOMEM;
9324}
9325
David Howellsa528d352017-01-31 16:46:22 +00009326static int btrfs_getattr(const struct path *path, struct kstat *stat,
9327 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009328{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009329 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009330 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009331 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009332 u32 bi_flags = BTRFS_I(inode)->flags;
9333
9334 stat->result_mask |= STATX_BTIME;
9335 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9336 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9337 if (bi_flags & BTRFS_INODE_APPEND)
9338 stat->attributes |= STATX_ATTR_APPEND;
9339 if (bi_flags & BTRFS_INODE_COMPRESS)
9340 stat->attributes |= STATX_ATTR_COMPRESSED;
9341 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9342 stat->attributes |= STATX_ATTR_IMMUTABLE;
9343 if (bi_flags & BTRFS_INODE_NODUMP)
9344 stat->attributes |= STATX_ATTR_NODUMP;
9345
9346 stat->attributes_mask |= (STATX_ATTR_APPEND |
9347 STATX_ATTR_COMPRESSED |
9348 STATX_ATTR_IMMUTABLE |
9349 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009350
Chris Mason39279cc2007-06-12 06:35:45 -04009351 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009352 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009353
9354 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009355 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009356 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009357 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009358 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009359 return 0;
9360}
9361
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009362static int btrfs_rename_exchange(struct inode *old_dir,
9363 struct dentry *old_dentry,
9364 struct inode *new_dir,
9365 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009366{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009367 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009368 struct btrfs_trans_handle *trans;
9369 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009370 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009371 struct inode *new_inode = new_dentry->d_inode;
9372 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009373 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009374 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009375 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9376 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009377 u64 old_idx = 0;
9378 u64 new_idx = 0;
9379 u64 root_objectid;
9380 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009381 bool root_log_pinned = false;
9382 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009383 struct btrfs_log_ctx ctx_root;
9384 struct btrfs_log_ctx ctx_dest;
9385 bool sync_log_root = false;
9386 bool sync_log_dest = false;
9387 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009388
9389 /* we only allow rename subvolume link between subvolumes */
9390 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9391 return -EXDEV;
9392
Filipe Mananad4682ba2018-06-11 19:24:28 +01009393 btrfs_init_log_ctx(&ctx_root, old_inode);
9394 btrfs_init_log_ctx(&ctx_dest, new_inode);
9395
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009396 /* close the race window with snapshot create/destroy ioctl */
9397 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009398 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009399 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009400 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009401
9402 /*
9403 * We want to reserve the absolute worst case amount of items. So if
9404 * both inodes are subvols and we need to unlink them then that would
9405 * require 4 item modifications, but if they are both normal inodes it
9406 * would require 5 item modifications, so we'll assume their normal
9407 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9408 * should cover the worst case number of items we'll modify.
9409 */
9410 trans = btrfs_start_transaction(root, 12);
9411 if (IS_ERR(trans)) {
9412 ret = PTR_ERR(trans);
9413 goto out_notrans;
9414 }
9415
9416 /*
9417 * We need to find a free sequence number both in the source and
9418 * in the destination directory for the exchange.
9419 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009420 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009421 if (ret)
9422 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009423 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009424 if (ret)
9425 goto out_fail;
9426
9427 BTRFS_I(old_inode)->dir_index = 0ULL;
9428 BTRFS_I(new_inode)->dir_index = 0ULL;
9429
9430 /* Reference for the source. */
9431 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9432 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009433 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009434 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009435 btrfs_pin_log_trans(root);
9436 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009437 ret = btrfs_insert_inode_ref(trans, dest,
9438 new_dentry->d_name.name,
9439 new_dentry->d_name.len,
9440 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009441 btrfs_ino(BTRFS_I(new_dir)),
9442 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009443 if (ret)
9444 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009445 }
9446
9447 /* And now for the dest. */
9448 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9449 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009450 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009451 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009452 btrfs_pin_log_trans(dest);
9453 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009454 ret = btrfs_insert_inode_ref(trans, root,
9455 old_dentry->d_name.name,
9456 old_dentry->d_name.len,
9457 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009458 btrfs_ino(BTRFS_I(old_dir)),
9459 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009460 if (ret)
9461 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009462 }
9463
9464 /* Update inode version and ctime/mtime. */
9465 inode_inc_iversion(old_dir);
9466 inode_inc_iversion(new_dir);
9467 inode_inc_iversion(old_inode);
9468 inode_inc_iversion(new_inode);
9469 old_dir->i_ctime = old_dir->i_mtime = ctime;
9470 new_dir->i_ctime = new_dir->i_mtime = ctime;
9471 old_inode->i_ctime = ctime;
9472 new_inode->i_ctime = ctime;
9473
9474 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009475 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9476 BTRFS_I(old_inode), 1);
9477 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9478 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009479 }
9480
9481 /* src is a subvolume */
9482 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9483 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009484 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009485 old_dentry->d_name.name,
9486 old_dentry->d_name.len);
9487 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009488 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9489 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009490 old_dentry->d_name.name,
9491 old_dentry->d_name.len);
9492 if (!ret)
9493 ret = btrfs_update_inode(trans, root, old_inode);
9494 }
9495 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009496 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009497 goto out_fail;
9498 }
9499
9500 /* dest is a subvolume */
9501 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9502 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009503 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009504 new_dentry->d_name.name,
9505 new_dentry->d_name.len);
9506 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009507 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9508 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009509 new_dentry->d_name.name,
9510 new_dentry->d_name.len);
9511 if (!ret)
9512 ret = btrfs_update_inode(trans, dest, new_inode);
9513 }
9514 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009515 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009516 goto out_fail;
9517 }
9518
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009519 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009520 new_dentry->d_name.name,
9521 new_dentry->d_name.len, 0, old_idx);
9522 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009523 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009524 goto out_fail;
9525 }
9526
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009527 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009528 old_dentry->d_name.name,
9529 old_dentry->d_name.len, 0, new_idx);
9530 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009531 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009532 goto out_fail;
9533 }
9534
9535 if (old_inode->i_nlink == 1)
9536 BTRFS_I(old_inode)->dir_index = old_idx;
9537 if (new_inode->i_nlink == 1)
9538 BTRFS_I(new_inode)->dir_index = new_idx;
9539
Filipe Manana86e8aa02016-05-05 02:02:27 +01009540 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009541 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009542 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9543 BTRFS_I(old_dir), parent,
9544 false, &ctx_root);
9545 if (ret == BTRFS_NEED_LOG_SYNC)
9546 sync_log_root = true;
9547 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9548 commit_transaction = true;
9549 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009550 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009551 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009552 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009553 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009554 if (!commit_transaction) {
9555 parent = old_dentry->d_parent;
9556 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9557 BTRFS_I(new_dir), parent,
9558 false, &ctx_dest);
9559 if (ret == BTRFS_NEED_LOG_SYNC)
9560 sync_log_dest = true;
9561 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9562 commit_transaction = true;
9563 ret = 0;
9564 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009565 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009566 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567 }
9568out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009569 /*
9570 * If we have pinned a log and an error happened, we unpin tasks
9571 * trying to sync the log and force them to fallback to a transaction
9572 * commit if the log currently contains any of the inodes involved in
9573 * this rename operation (to ensure we do not persist a log with an
9574 * inconsistent state for any of these inodes or leading to any
9575 * inconsistencies when replayed). If the transaction was aborted, the
9576 * abortion reason is propagated to userspace when attempting to commit
9577 * the transaction. If the log does not contain any of these inodes, we
9578 * allow the tasks to sync it.
9579 */
9580 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009581 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9582 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9583 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009584 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009585 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009586 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009587
9588 if (root_log_pinned) {
9589 btrfs_end_log_trans(root);
9590 root_log_pinned = false;
9591 }
9592 if (dest_log_pinned) {
9593 btrfs_end_log_trans(dest);
9594 dest_log_pinned = false;
9595 }
9596 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009597 if (!ret && sync_log_root && !commit_transaction) {
9598 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9599 &ctx_root);
9600 if (ret)
9601 commit_transaction = true;
9602 }
9603 if (!ret && sync_log_dest && !commit_transaction) {
9604 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9605 &ctx_dest);
9606 if (ret)
9607 commit_transaction = true;
9608 }
9609 if (commit_transaction) {
9610 ret = btrfs_commit_transaction(trans);
9611 } else {
9612 int ret2;
9613
9614 ret2 = btrfs_end_transaction(trans);
9615 ret = ret ? ret : ret2;
9616 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617out_notrans:
9618 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009619 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009621 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009622
9623 return ret;
9624}
9625
9626static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9627 struct btrfs_root *root,
9628 struct inode *dir,
9629 struct dentry *dentry)
9630{
9631 int ret;
9632 struct inode *inode;
9633 u64 objectid;
9634 u64 index;
9635
9636 ret = btrfs_find_free_ino(root, &objectid);
9637 if (ret)
9638 return ret;
9639
9640 inode = btrfs_new_inode(trans, root, dir,
9641 dentry->d_name.name,
9642 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009643 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009644 objectid,
9645 S_IFCHR | WHITEOUT_MODE,
9646 &index);
9647
9648 if (IS_ERR(inode)) {
9649 ret = PTR_ERR(inode);
9650 return ret;
9651 }
9652
9653 inode->i_op = &btrfs_special_inode_operations;
9654 init_special_inode(inode, inode->i_mode,
9655 WHITEOUT_DEV);
9656
9657 ret = btrfs_init_inode_security(trans, inode, dir,
9658 &dentry->d_name);
9659 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009660 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009661
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009662 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9663 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009665 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009666
9667 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009668out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009669 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009670 if (ret)
9671 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009672 iput(inode);
9673
Filipe Mananac9901612016-05-05 01:41:57 +01009674 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009675}
9676
Chris Mason39279cc2007-06-12 06:35:45 -04009677static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 struct inode *new_dir, struct dentry *new_dentry,
9679 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009680{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009681 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009682 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009683 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009684 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9685 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009686 struct inode *new_inode = d_inode(new_dentry);
9687 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009688 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009689 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009690 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009691 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009692 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009693 struct btrfs_log_ctx ctx;
9694 bool sync_log = false;
9695 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009696
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009697 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009698 return -EPERM;
9699
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009700 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009701 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009702 return -EXDEV;
9703
Li Zefan33345d012011-04-20 10:31:50 +08009704 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009705 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009706 return -ENOTEMPTY;
9707
Chris Mason39279cc2007-06-12 06:35:45 -04009708 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009709 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009710 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009711
9712
9713 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009714 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009715 new_dentry->d_name.name,
9716 new_dentry->d_name.len);
9717
9718 if (ret) {
9719 if (ret == -EEXIST) {
9720 /* we shouldn't get
9721 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309722 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009723 return ret;
9724 }
9725 } else {
9726 /* maybe -EOVERFLOW */
9727 return ret;
9728 }
9729 }
9730 ret = 0;
9731
Chris Mason5a3f23d2009-03-31 13:27:11 -04009732 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009733 * we're using rename to replace one file with another. Start IO on it
9734 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009735 */
Chris Mason8d875f92014-08-12 10:47:42 -07009736 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009737 filemap_flush(old_inode->i_mapping);
9738
Yan, Zheng76dda932009-09-21 16:00:26 -04009739 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009740 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009741 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009742 /*
9743 * We want to reserve the absolute worst case amount of items. So if
9744 * both inodes are subvols and we need to unlink them then that would
9745 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009746 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009747 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9748 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009749 * If our rename has the whiteout flag, we need more 5 units for the
9750 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9751 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009752 */
Filipe Manana5062af32016-05-05 10:26:26 +01009753 trans_num_items = 11;
9754 if (flags & RENAME_WHITEOUT)
9755 trans_num_items += 5;
9756 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009757 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009758 ret = PTR_ERR(trans);
9759 goto out_notrans;
9760 }
Chris Mason5f39d392007-10-15 16:14:19 -04009761
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009762 if (dest != root)
9763 btrfs_record_root_in_trans(trans, dest);
9764
Nikolay Borisov877574e2017-02-20 13:50:33 +02009765 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009766 if (ret)
9767 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009768
Miao Xie67de1172013-12-26 13:07:06 +08009769 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009770 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009771 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009772 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009773 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009774 btrfs_pin_log_trans(root);
9775 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009776 ret = btrfs_insert_inode_ref(trans, dest,
9777 new_dentry->d_name.name,
9778 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009779 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009780 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009781 if (ret)
9782 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009783 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009784
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009785 inode_inc_iversion(old_dir);
9786 inode_inc_iversion(new_dir);
9787 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009788 old_dir->i_ctime = old_dir->i_mtime =
9789 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009790 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009791
Chris Mason12fcfd22009-03-24 10:24:20 -04009792 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009793 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9794 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009795
Li Zefan33345d012011-04-20 10:31:50 +08009796 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009797 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009798 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009799 old_dentry->d_name.name,
9800 old_dentry->d_name.len);
9801 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009802 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9803 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009804 old_dentry->d_name.name,
9805 old_dentry->d_name.len);
9806 if (!ret)
9807 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009808 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009809 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009810 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009811 goto out_fail;
9812 }
Chris Mason39279cc2007-06-12 06:35:45 -04009813
9814 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009815 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009816 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009817 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009818 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9819 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009820 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009821 new_dentry->d_name.name,
9822 new_dentry->d_name.len);
9823 BUG_ON(new_inode->i_nlink == 0);
9824 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009825 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9826 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009827 new_dentry->d_name.name,
9828 new_dentry->d_name.len);
9829 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009830 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009831 ret = btrfs_orphan_add(trans,
9832 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009833 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009834 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009835 goto out_fail;
9836 }
Chris Mason39279cc2007-06-12 06:35:45 -04009837 }
Josef Bacikaec74772008-07-24 12:12:38 -04009838
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009839 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009840 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009841 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009842 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009843 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009844 goto out_fail;
9845 }
Chris Mason39279cc2007-06-12 06:35:45 -04009846
Miao Xie67de1172013-12-26 13:07:06 +08009847 if (old_inode->i_nlink == 1)
9848 BTRFS_I(old_inode)->dir_index = index;
9849
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009850 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009851 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009852
Filipe Mananad4682ba2018-06-11 19:24:28 +01009853 btrfs_init_log_ctx(&ctx, old_inode);
9854 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9855 BTRFS_I(old_dir), parent,
9856 false, &ctx);
9857 if (ret == BTRFS_NEED_LOG_SYNC)
9858 sync_log = true;
9859 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9860 commit_transaction = true;
9861 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009862 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009863 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009864 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009865
9866 if (flags & RENAME_WHITEOUT) {
9867 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9868 old_dentry);
9869
9870 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009871 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009872 goto out_fail;
9873 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009874 }
Chris Mason39279cc2007-06-12 06:35:45 -04009875out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009876 /*
9877 * If we have pinned the log and an error happened, we unpin tasks
9878 * trying to sync the log and force them to fallback to a transaction
9879 * commit if the log currently contains any of the inodes involved in
9880 * this rename operation (to ensure we do not persist a log with an
9881 * inconsistent state for any of these inodes or leading to any
9882 * inconsistencies when replayed). If the transaction was aborted, the
9883 * abortion reason is propagated to userspace when attempting to commit
9884 * the transaction. If the log does not contain any of these inodes, we
9885 * allow the tasks to sync it.
9886 */
9887 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009888 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9889 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9890 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009891 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009892 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009893 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009894
9895 btrfs_end_log_trans(root);
9896 log_pinned = false;
9897 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009898 if (!ret && sync_log) {
9899 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9900 if (ret)
9901 commit_transaction = true;
9902 }
9903 if (commit_transaction) {
9904 ret = btrfs_commit_transaction(trans);
9905 } else {
9906 int ret2;
9907
9908 ret2 = btrfs_end_transaction(trans);
9909 ret = ret ? ret : ret2;
9910 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009911out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009912 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009913 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009914
Chris Mason39279cc2007-06-12 06:35:45 -04009915 return ret;
9916}
9917
Miklos Szeredi80ace852014-07-23 15:15:32 +02009918static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9919 struct inode *new_dir, struct dentry *new_dentry,
9920 unsigned int flags)
9921{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009922 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009923 return -EINVAL;
9924
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009925 if (flags & RENAME_EXCHANGE)
9926 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9927 new_dentry);
9928
9929 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009930}
9931
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009932struct btrfs_delalloc_work {
9933 struct inode *inode;
9934 struct completion completion;
9935 struct list_head list;
9936 struct btrfs_work work;
9937};
9938
Miao Xie8ccf6f192012-10-25 09:28:04 +00009939static void btrfs_run_delalloc_work(struct btrfs_work *work)
9940{
9941 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009942 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009943
9944 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9945 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009946 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009947 filemap_flush(inode->i_mapping);
9948 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9949 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009950 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009951
Nikolay Borisov076da912018-04-23 10:54:16 +03009952 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009953 complete(&delalloc_work->completion);
9954}
9955
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009956static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009957{
9958 struct btrfs_delalloc_work *work;
9959
David Sterba100d5702015-12-08 14:39:32 +01009960 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009961 if (!work)
9962 return NULL;
9963
9964 init_completion(&work->completion);
9965 INIT_LIST_HEAD(&work->list);
9966 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +08009967 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9968 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009969
9970 return work;
9971}
9972
Chris Masond352ac62008-09-29 15:18:18 -04009973/*
9974 * some fairly slow code that needs optimization. This walks the list
9975 * of all the inodes with pending delalloc and forces them to disk.
9976 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009977static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009978{
Chris Masonea8c2812008-08-04 23:17:27 -04009979 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009980 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009981 struct btrfs_delalloc_work *work, *next;
9982 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009983 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009984 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009985
Miao Xie8ccf6f192012-10-25 09:28:04 +00009986 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009987 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009988
Miao Xie573bfb72014-03-06 13:55:03 +08009989 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009990 spin_lock(&root->delalloc_lock);
9991 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009992 while (!list_empty(&splice)) {
9993 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009994 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009995
Miao Xieeb73c1b2013-05-15 07:48:22 +00009996 list_move_tail(&binode->delalloc_inodes,
9997 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009998 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009999 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010000 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010001 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010002 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010003 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010004
Ethan Lien3cd24c62018-11-01 14:49:03 +080010005 if (snapshot)
10006 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10007 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +030010008 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +020010009 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +030010010 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010011 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010012 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010013 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010014 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010015 btrfs_queue_work(root->fs_info->flush_workers,
10016 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010017 ret++;
10018 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010019 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010020 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010021 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010022 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010023 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010024
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010025out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010026 list_for_each_entry_safe(work, next, &works, list) {
10027 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +030010028 wait_for_completion(&work->completion);
10029 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010030 }
10031
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010032 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010033 spin_lock(&root->delalloc_lock);
10034 list_splice_tail(&splice, &root->delalloc_inodes);
10035 spin_unlock(&root->delalloc_lock);
10036 }
Miao Xie573bfb72014-03-06 13:55:03 +080010037 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010038 return ret;
10039}
10040
Ethan Lien3cd24c62018-11-01 14:49:03 +080010041int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010042{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010043 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010044 int ret;
10045
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010046 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010047 return -EROFS;
10048
Ethan Lien3cd24c62018-11-01 14:49:03 +080010049 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010050 if (ret > 0)
10051 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010052 return ret;
10053}
10054
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010055int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010056{
10057 struct btrfs_root *root;
10058 struct list_head splice;
10059 int ret;
10060
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010061 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010062 return -EROFS;
10063
10064 INIT_LIST_HEAD(&splice);
10065
Miao Xie573bfb72014-03-06 13:55:03 +080010066 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010067 spin_lock(&fs_info->delalloc_root_lock);
10068 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010069 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010070 root = list_first_entry(&splice, struct btrfs_root,
10071 delalloc_root);
10072 root = btrfs_grab_fs_root(root);
10073 BUG_ON(!root);
10074 list_move_tail(&root->delalloc_root,
10075 &fs_info->delalloc_roots);
10076 spin_unlock(&fs_info->delalloc_root_lock);
10077
Ethan Lien3cd24c62018-11-01 14:49:03 +080010078 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010079 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010080 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010081 goto out;
10082
Miao Xie6c255e62014-03-06 13:55:01 +080010083 if (nr != -1) {
10084 nr -= ret;
10085 WARN_ON(nr < 0);
10086 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010087 spin_lock(&fs_info->delalloc_root_lock);
10088 }
10089 spin_unlock(&fs_info->delalloc_root_lock);
10090
Miao Xie6c255e62014-03-06 13:55:01 +080010091 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010092out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010093 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010094 spin_lock(&fs_info->delalloc_root_lock);
10095 list_splice_tail(&splice, &fs_info->delalloc_roots);
10096 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010097 }
Miao Xie573bfb72014-03-06 13:55:03 +080010098 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010099 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010100}
10101
Chris Mason39279cc2007-06-12 06:35:45 -040010102static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10103 const char *symname)
10104{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010105 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010106 struct btrfs_trans_handle *trans;
10107 struct btrfs_root *root = BTRFS_I(dir)->root;
10108 struct btrfs_path *path;
10109 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010110 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010111 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010112 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010113 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010114 int name_len;
10115 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010116 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010117 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010118 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010119
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010120 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010121 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010122 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010123
Josef Bacik9ed74f22009-09-11 16:12:44 -040010124 /*
10125 * 2 items for inode item and ref
10126 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010127 * 1 item for updating parent inode item
10128 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010129 * 1 item for xattr if selinux is on
10130 */
Filipe Manana9269d122015-12-31 18:16:29 +000010131 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010132 if (IS_ERR(trans))
10133 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010134
Li Zefan581bb052011-04-20 10:06:11 +080010135 err = btrfs_find_free_ino(root, &objectid);
10136 if (err)
10137 goto out_unlock;
10138
Josef Bacikaec74772008-07-24 12:12:38 -040010139 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010140 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10141 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010142 if (IS_ERR(inode)) {
10143 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010144 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010145 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010146 }
Chris Mason39279cc2007-06-12 06:35:45 -040010147
Casey Schauflerad19db72011-12-15 10:09:07 -050010148 /*
10149 * If the active LSM wants to access the inode during
10150 * d_instantiate it needs these. Smack checks to see
10151 * if the filesystem supports xattrs by looking at the
10152 * ops vector.
10153 */
10154 inode->i_fop = &btrfs_file_operations;
10155 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010156 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010157 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10158
10159 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10160 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010161 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010162
Chris Mason39279cc2007-06-12 06:35:45 -040010163 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010164 if (!path) {
10165 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010166 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010167 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010168 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010169 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010170 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010171 datasize = btrfs_file_extent_calc_inline_size(name_len);
10172 err = btrfs_insert_empty_item(trans, root, path, &key,
10173 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010174 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010175 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010176 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010177 }
Chris Mason5f39d392007-10-15 16:14:19 -040010178 leaf = path->nodes[0];
10179 ei = btrfs_item_ptr(leaf, path->slots[0],
10180 struct btrfs_file_extent_item);
10181 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10182 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010183 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010184 btrfs_set_file_extent_encryption(leaf, ei, 0);
10185 btrfs_set_file_extent_compression(leaf, ei, 0);
10186 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10187 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10188
Chris Mason39279cc2007-06-12 06:35:45 -040010189 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010190 write_extent_buffer(leaf, symname, ptr, name_len);
10191 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010192 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010193
Chris Mason39279cc2007-06-12 06:35:45 -040010194 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010195 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -070010196 inode->i_mapping->a_ops = &btrfs_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010197 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010198 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010199 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010200 /*
10201 * Last step, add directory indexes for our symlink inode. This is the
10202 * last step to avoid extra cleanup of these indexes if an error happens
10203 * elsewhere above.
10204 */
10205 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010206 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10207 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010208 if (err)
10209 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010210
Al Viro1e2e5472018-05-04 08:23:01 -040010211 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010212
10213out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010214 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010215 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010216 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010217 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010218 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010219 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010220 return err;
10221}
Chris Mason16432982008-04-10 10:23:21 -040010222
Josef Bacik0af3d002010-06-21 14:48:16 -040010223static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10224 u64 start, u64 num_bytes, u64 min_size,
10225 loff_t actual_len, u64 *alloc_hint,
10226 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010227{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010228 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010229 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10230 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010231 struct btrfs_root *root = BTRFS_I(inode)->root;
10232 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010233 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010234 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010235 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010236 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010237 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010238 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010239 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010240
Josef Bacik0af3d002010-06-21 14:48:16 -040010241 if (trans)
10242 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010243 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010244 if (own_trans) {
10245 trans = btrfs_start_transaction(root, 3);
10246 if (IS_ERR(trans)) {
10247 ret = PTR_ERR(trans);
10248 break;
10249 }
Yan Zhengd899e052008-10-30 14:25:28 -040010250 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010251
Byongho Leeee221842015-12-15 01:42:10 +090010252 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010253 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010254 /*
10255 * If we are severely fragmented we could end up with really
10256 * small allocations, so if the allocator is returning small
10257 * chunks lets make its job easier by only searching for those
10258 * sized chunks.
10259 */
10260 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010261 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10262 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010263 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010264 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010265 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010266 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010267 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010268 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010269
Josef Bacik0b670dc2015-09-23 17:11:16 -040010270 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010271 ret = insert_reserved_file_extent(trans, inode,
10272 cur_offset, ins.objectid,
10273 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010274 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010275 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010276 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010277 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010278 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010279 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010280 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010281 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010282 break;
10283 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010284
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010285 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010286 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010287
Josef Bacik5dc562c2012-08-17 13:14:17 -040010288 em = alloc_extent_map();
10289 if (!em) {
10290 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10291 &BTRFS_I(inode)->runtime_flags);
10292 goto next;
10293 }
10294
10295 em->start = cur_offset;
10296 em->orig_start = cur_offset;
10297 em->len = ins.offset;
10298 em->block_start = ins.objectid;
10299 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010300 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010301 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010302 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010303 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10304 em->generation = trans->transid;
10305
10306 while (1) {
10307 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010308 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010309 write_unlock(&em_tree->lock);
10310 if (ret != -EEXIST)
10311 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010312 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010313 cur_offset + ins.offset - 1,
10314 0);
10315 }
10316 free_extent_map(em);
10317next:
Yan Zhengd899e052008-10-30 14:25:28 -040010318 num_bytes -= ins.offset;
10319 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010320 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010321
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010322 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010323 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010324 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010325 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010326 (actual_len > inode->i_size) &&
10327 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010328 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010329 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010330 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010331 i_size = cur_offset;
10332 i_size_write(inode, i_size);
10333 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010334 }
10335
Yan Zhengd899e052008-10-30 14:25:28 -040010336 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010337
10338 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010339 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010340 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010341 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010342 break;
10343 }
Yan Zhengd899e052008-10-30 14:25:28 -040010344
Josef Bacik0af3d002010-06-21 14:48:16 -040010345 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010346 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010347 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010348 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010349 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010350 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010351 return ret;
10352}
10353
Josef Bacik0af3d002010-06-21 14:48:16 -040010354int btrfs_prealloc_file_range(struct inode *inode, int mode,
10355 u64 start, u64 num_bytes, u64 min_size,
10356 loff_t actual_len, u64 *alloc_hint)
10357{
10358 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10359 min_size, actual_len, alloc_hint,
10360 NULL);
10361}
10362
10363int btrfs_prealloc_file_range_trans(struct inode *inode,
10364 struct btrfs_trans_handle *trans, int mode,
10365 u64 start, u64 num_bytes, u64 min_size,
10366 loff_t actual_len, u64 *alloc_hint)
10367{
10368 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10369 min_size, actual_len, alloc_hint, trans);
10370}
10371
Chris Masone6dcd2d2008-07-17 12:53:50 -040010372static int btrfs_set_page_dirty(struct page *page)
10373{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010374 return __set_page_dirty_nobuffers(page);
10375}
10376
Al Viro10556cb22011-06-20 19:28:19 -040010377static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010378{
Li Zefanb83cc962010-12-20 16:04:08 +080010379 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010380 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010381
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010382 if (mask & MAY_WRITE &&
10383 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10384 if (btrfs_root_readonly(root))
10385 return -EROFS;
10386 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10387 return -EACCES;
10388 }
Al Viro2830ba72011-06-20 19:16:29 -040010389 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010390}
Chris Mason39279cc2007-06-12 06:35:45 -040010391
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010392static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10393{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010394 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010395 struct btrfs_trans_handle *trans;
10396 struct btrfs_root *root = BTRFS_I(dir)->root;
10397 struct inode *inode = NULL;
10398 u64 objectid;
10399 u64 index;
10400 int ret = 0;
10401
10402 /*
10403 * 5 units required for adding orphan entry
10404 */
10405 trans = btrfs_start_transaction(root, 5);
10406 if (IS_ERR(trans))
10407 return PTR_ERR(trans);
10408
10409 ret = btrfs_find_free_ino(root, &objectid);
10410 if (ret)
10411 goto out;
10412
10413 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010414 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010415 if (IS_ERR(inode)) {
10416 ret = PTR_ERR(inode);
10417 inode = NULL;
10418 goto out;
10419 }
10420
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010421 inode->i_fop = &btrfs_file_operations;
10422 inode->i_op = &btrfs_file_inode_operations;
10423
10424 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010425 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10426
Chris Masonb0d5d102014-09-08 13:08:51 -070010427 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10428 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010429 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010430
10431 ret = btrfs_update_inode(trans, root, inode);
10432 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010433 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010434 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010435 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010436 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010437
Filipe Manana5762b5c2014-08-01 00:10:32 +010010438 /*
10439 * We set number of links to 0 in btrfs_new_inode(), and here we set
10440 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10441 * through:
10442 *
10443 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10444 */
10445 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010446 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010447 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010448 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010449out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010450 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010451 if (ret && inode)
10452 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010453 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010454 return ret;
10455}
10456
David Sterba5cdc84b2018-07-18 20:32:52 +020010457void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010458{
David Sterba5cdc84b2018-07-18 20:32:52 +020010459 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010460 unsigned long index = start >> PAGE_SHIFT;
10461 unsigned long end_index = end >> PAGE_SHIFT;
10462 struct page *page;
10463
10464 while (index <= end_index) {
10465 page = find_get_page(inode->i_mapping, index);
10466 ASSERT(page); /* Pages should be in the extent_io_tree */
10467 set_page_writeback(page);
10468 put_page(page);
10469 index++;
10470 }
10471}
10472
Omar Sandovaled46ff32016-11-03 10:28:14 -070010473#ifdef CONFIG_SWAP
10474/*
10475 * Add an entry indicating a block group or device which is pinned by a
10476 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10477 * negative errno on failure.
10478 */
10479static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10480 bool is_block_group)
10481{
10482 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10483 struct btrfs_swapfile_pin *sp, *entry;
10484 struct rb_node **p;
10485 struct rb_node *parent = NULL;
10486
10487 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10488 if (!sp)
10489 return -ENOMEM;
10490 sp->ptr = ptr;
10491 sp->inode = inode;
10492 sp->is_block_group = is_block_group;
10493
10494 spin_lock(&fs_info->swapfile_pins_lock);
10495 p = &fs_info->swapfile_pins.rb_node;
10496 while (*p) {
10497 parent = *p;
10498 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10499 if (sp->ptr < entry->ptr ||
10500 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10501 p = &(*p)->rb_left;
10502 } else if (sp->ptr > entry->ptr ||
10503 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10504 p = &(*p)->rb_right;
10505 } else {
10506 spin_unlock(&fs_info->swapfile_pins_lock);
10507 kfree(sp);
10508 return 1;
10509 }
10510 }
10511 rb_link_node(&sp->node, parent, p);
10512 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10513 spin_unlock(&fs_info->swapfile_pins_lock);
10514 return 0;
10515}
10516
10517/* Free all of the entries pinned by this swapfile. */
10518static void btrfs_free_swapfile_pins(struct inode *inode)
10519{
10520 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10521 struct btrfs_swapfile_pin *sp;
10522 struct rb_node *node, *next;
10523
10524 spin_lock(&fs_info->swapfile_pins_lock);
10525 node = rb_first(&fs_info->swapfile_pins);
10526 while (node) {
10527 next = rb_next(node);
10528 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10529 if (sp->inode == inode) {
10530 rb_erase(&sp->node, &fs_info->swapfile_pins);
10531 if (sp->is_block_group)
10532 btrfs_put_block_group(sp->ptr);
10533 kfree(sp);
10534 }
10535 node = next;
10536 }
10537 spin_unlock(&fs_info->swapfile_pins_lock);
10538}
10539
10540struct btrfs_swap_info {
10541 u64 start;
10542 u64 block_start;
10543 u64 block_len;
10544 u64 lowest_ppage;
10545 u64 highest_ppage;
10546 unsigned long nr_pages;
10547 int nr_extents;
10548};
10549
10550static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10551 struct btrfs_swap_info *bsi)
10552{
10553 unsigned long nr_pages;
10554 u64 first_ppage, first_ppage_reported, next_ppage;
10555 int ret;
10556
10557 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10558 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10559 PAGE_SIZE) >> PAGE_SHIFT;
10560
10561 if (first_ppage >= next_ppage)
10562 return 0;
10563 nr_pages = next_ppage - first_ppage;
10564
10565 first_ppage_reported = first_ppage;
10566 if (bsi->start == 0)
10567 first_ppage_reported++;
10568 if (bsi->lowest_ppage > first_ppage_reported)
10569 bsi->lowest_ppage = first_ppage_reported;
10570 if (bsi->highest_ppage < (next_ppage - 1))
10571 bsi->highest_ppage = next_ppage - 1;
10572
10573 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10574 if (ret < 0)
10575 return ret;
10576 bsi->nr_extents += ret;
10577 bsi->nr_pages += nr_pages;
10578 return 0;
10579}
10580
10581static void btrfs_swap_deactivate(struct file *file)
10582{
10583 struct inode *inode = file_inode(file);
10584
10585 btrfs_free_swapfile_pins(inode);
10586 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10587}
10588
10589static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10590 sector_t *span)
10591{
10592 struct inode *inode = file_inode(file);
10593 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10594 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10595 struct extent_state *cached_state = NULL;
10596 struct extent_map *em = NULL;
10597 struct btrfs_device *device = NULL;
10598 struct btrfs_swap_info bsi = {
10599 .lowest_ppage = (sector_t)-1ULL,
10600 };
10601 int ret = 0;
10602 u64 isize;
10603 u64 start;
10604
10605 /*
10606 * If the swap file was just created, make sure delalloc is done. If the
10607 * file changes again after this, the user is doing something stupid and
10608 * we don't really care.
10609 */
10610 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10611 if (ret)
10612 return ret;
10613
10614 /*
10615 * The inode is locked, so these flags won't change after we check them.
10616 */
10617 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10618 btrfs_warn(fs_info, "swapfile must not be compressed");
10619 return -EINVAL;
10620 }
10621 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10622 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10623 return -EINVAL;
10624 }
10625 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10626 btrfs_warn(fs_info, "swapfile must not be checksummed");
10627 return -EINVAL;
10628 }
10629
10630 /*
10631 * Balance or device remove/replace/resize can move stuff around from
10632 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10633 * concurrently while we are mapping the swap extents, and
10634 * fs_info->swapfile_pins prevents them from running while the swap file
10635 * is active and moving the extents. Note that this also prevents a
10636 * concurrent device add which isn't actually necessary, but it's not
10637 * really worth the trouble to allow it.
10638 */
10639 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10640 btrfs_warn(fs_info,
10641 "cannot activate swapfile while exclusive operation is running");
10642 return -EBUSY;
10643 }
10644 /*
10645 * Snapshots can create extents which require COW even if NODATACOW is
10646 * set. We use this counter to prevent snapshots. We must increment it
10647 * before walking the extents because we don't want a concurrent
10648 * snapshot to run after we've already checked the extents.
10649 */
10650 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10651
10652 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10653
10654 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10655 start = 0;
10656 while (start < isize) {
10657 u64 logical_block_start, physical_block_start;
10658 struct btrfs_block_group_cache *bg;
10659 u64 len = isize - start;
10660
10661 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10662 if (IS_ERR(em)) {
10663 ret = PTR_ERR(em);
10664 goto out;
10665 }
10666
10667 if (em->block_start == EXTENT_MAP_HOLE) {
10668 btrfs_warn(fs_info, "swapfile must not have holes");
10669 ret = -EINVAL;
10670 goto out;
10671 }
10672 if (em->block_start == EXTENT_MAP_INLINE) {
10673 /*
10674 * It's unlikely we'll ever actually find ourselves
10675 * here, as a file small enough to fit inline won't be
10676 * big enough to store more than the swap header, but in
10677 * case something changes in the future, let's catch it
10678 * here rather than later.
10679 */
10680 btrfs_warn(fs_info, "swapfile must not be inline");
10681 ret = -EINVAL;
10682 goto out;
10683 }
10684 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10685 btrfs_warn(fs_info, "swapfile must not be compressed");
10686 ret = -EINVAL;
10687 goto out;
10688 }
10689
10690 logical_block_start = em->block_start + (start - em->start);
10691 len = min(len, em->len - (start - em->start));
10692 free_extent_map(em);
10693 em = NULL;
10694
10695 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10696 if (ret < 0) {
10697 goto out;
10698 } else if (ret) {
10699 ret = 0;
10700 } else {
10701 btrfs_warn(fs_info,
10702 "swapfile must not be copy-on-write");
10703 ret = -EINVAL;
10704 goto out;
10705 }
10706
10707 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10708 if (IS_ERR(em)) {
10709 ret = PTR_ERR(em);
10710 goto out;
10711 }
10712
10713 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10714 btrfs_warn(fs_info,
10715 "swapfile must have single data profile");
10716 ret = -EINVAL;
10717 goto out;
10718 }
10719
10720 if (device == NULL) {
10721 device = em->map_lookup->stripes[0].dev;
10722 ret = btrfs_add_swapfile_pin(inode, device, false);
10723 if (ret == 1)
10724 ret = 0;
10725 else if (ret)
10726 goto out;
10727 } else if (device != em->map_lookup->stripes[0].dev) {
10728 btrfs_warn(fs_info, "swapfile must be on one device");
10729 ret = -EINVAL;
10730 goto out;
10731 }
10732
10733 physical_block_start = (em->map_lookup->stripes[0].physical +
10734 (logical_block_start - em->start));
10735 len = min(len, em->len - (logical_block_start - em->start));
10736 free_extent_map(em);
10737 em = NULL;
10738
10739 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10740 if (!bg) {
10741 btrfs_warn(fs_info,
10742 "could not find block group containing swapfile");
10743 ret = -EINVAL;
10744 goto out;
10745 }
10746
10747 ret = btrfs_add_swapfile_pin(inode, bg, true);
10748 if (ret) {
10749 btrfs_put_block_group(bg);
10750 if (ret == 1)
10751 ret = 0;
10752 else
10753 goto out;
10754 }
10755
10756 if (bsi.block_len &&
10757 bsi.block_start + bsi.block_len == physical_block_start) {
10758 bsi.block_len += len;
10759 } else {
10760 if (bsi.block_len) {
10761 ret = btrfs_add_swap_extent(sis, &bsi);
10762 if (ret)
10763 goto out;
10764 }
10765 bsi.start = start;
10766 bsi.block_start = physical_block_start;
10767 bsi.block_len = len;
10768 }
10769
10770 start += len;
10771 }
10772
10773 if (bsi.block_len)
10774 ret = btrfs_add_swap_extent(sis, &bsi);
10775
10776out:
10777 if (!IS_ERR_OR_NULL(em))
10778 free_extent_map(em);
10779
10780 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10781
10782 if (ret)
10783 btrfs_swap_deactivate(file);
10784
10785 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10786
10787 if (ret)
10788 return ret;
10789
10790 if (device)
10791 sis->bdev = device->bdev;
10792 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10793 sis->max = bsi.nr_pages;
10794 sis->pages = bsi.nr_pages - 1;
10795 sis->highest_bit = bsi.nr_pages - 1;
10796 return bsi.nr_extents;
10797}
10798#else
10799static void btrfs_swap_deactivate(struct file *file)
10800{
10801}
10802
10803static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10804 sector_t *span)
10805{
10806 return -EOPNOTSUPP;
10807}
10808#endif
10809
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010810static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010811 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010812 .lookup = btrfs_lookup,
10813 .create = btrfs_create,
10814 .unlink = btrfs_unlink,
10815 .link = btrfs_link,
10816 .mkdir = btrfs_mkdir,
10817 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010818 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010819 .symlink = btrfs_symlink,
10820 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010821 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010822 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010823 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010824 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010825 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010826 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010827 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010828};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010829static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010830 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010831 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010832 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010833};
Yan, Zheng76dda932009-09-21 16:00:26 -040010834
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010835static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010836 .llseek = generic_file_llseek,
10837 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010838 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010839 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010840 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010841#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010842 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010843#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010844 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010845 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010846};
10847
David Sterba20e55062015-11-19 11:42:28 +010010848static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010849 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010850 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010851 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10852};
10853
Chris Mason35054392009-01-21 13:11:13 -050010854/*
10855 * btrfs doesn't support the bmap operation because swapfiles
10856 * use bmap to make a mapping of extents in the file. They assume
10857 * these extents won't change over the life of the file and they
10858 * use the bmap result to do IO directly to the drive.
10859 *
10860 * the btrfs bmap call would return logical addresses that aren't
10861 * suitable for IO and they also will change frequently as COW
10862 * operations happen. So, swapfile + btrfs == corruption.
10863 *
10864 * For now we're avoiding this by dropping bmap.
10865 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010866static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010867 .readpage = btrfs_readpage,
10868 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010869 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010870 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010871 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010872 .invalidatepage = btrfs_invalidatepage,
10873 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010874 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010875 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010876 .swap_activate = btrfs_swap_activate,
10877 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010878};
10879
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010880static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010881 .getattr = btrfs_getattr,
10882 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010883 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010884 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010885 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010886 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010887 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010888 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010889};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010890static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010891 .getattr = btrfs_getattr,
10892 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010893 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010894 .listxattr = btrfs_listxattr,
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,
Josef Bacike41f9412012-03-26 09:46:47 -040010897 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010898};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010899static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010900 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010901 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010902 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010903 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010904 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010905 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010906};
Yan, Zheng76dda932009-09-21 16:00:26 -040010907
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010908const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010909 .d_delete = btrfs_dentry_delete,
10910};