blob: c6fc283164b87de58766029b402d5e98214a1bab [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
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003259 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003260 ASSERT(list_empty(&binode->delayed_iput));
3261 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003262 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003263 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3264 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003265}
3266
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003267void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003268{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003269
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003270 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003271 while (!list_empty(&fs_info->delayed_iputs)) {
3272 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003273
David Sterba8089fe62015-11-19 14:15:51 +01003274 inode = list_first_entry(&fs_info->delayed_iputs,
3275 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003276 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003277 spin_unlock(&fs_info->delayed_iput_lock);
3278 iput(&inode->vfs_inode);
3279 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003280 }
David Sterba8089fe62015-11-19 14:15:51 +01003281 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003282}
3283
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003285 * This creates an orphan entry for the given inode in case something goes wrong
3286 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003287 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003288int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003289 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003290{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003291 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003292
Omar Sandoval27919062018-05-11 13:13:37 -07003293 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3294 if (ret && ret != -EEXIST) {
3295 btrfs_abort_transaction(trans, ret);
3296 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003297 }
3298
Yan, Zhengd68fc572010-05-16 10:49:58 -04003299 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003300}
3301
3302/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003303 * We have done the delete so we can go ahead and remove the orphan item for
3304 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003305 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003306static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003307 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003308{
Omar Sandoval27919062018-05-11 13:13:37 -07003309 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003310}
3311
3312/*
3313 * this cleans up any orphans that may be left on the list from the last use
3314 * of this root.
3315 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003316int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003317{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003318 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003319 struct btrfs_path *path;
3320 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003321 struct btrfs_key key, found_key;
3322 struct btrfs_trans_handle *trans;
3323 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003324 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003325 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003326
Yan, Zhengd68fc572010-05-16 10:49:58 -04003327 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003328 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003329
3330 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003331 if (!path) {
3332 ret = -ENOMEM;
3333 goto out;
3334 }
David Sterbae4058b52015-11-27 16:31:35 +01003335 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003336
3337 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003338 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003339 key.offset = (u64)-1;
3340
Josef Bacik7b128762008-07-24 12:17:14 -04003341 while (1) {
3342 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003343 if (ret < 0)
3344 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003345
3346 /*
3347 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003348 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003349 * find the key and see if we have stuff that matches
3350 */
3351 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003352 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003353 if (path->slots[0] == 0)
3354 break;
3355 path->slots[0]--;
3356 }
3357
3358 /* pull out the item */
3359 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003360 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3361
3362 /* make sure the item matches what we want */
3363 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3364 break;
David Sterba962a2982014-06-04 18:41:45 +02003365 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003366 break;
3367
3368 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003369 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003370
3371 /*
3372 * this is where we are basically btrfs_lookup, without the
3373 * crossing root thing. we store the inode number in the
3374 * offset of the orphan item.
3375 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003376
3377 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003378 btrfs_err(fs_info,
3379 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003380 ret = -EINVAL;
3381 goto out;
3382 }
3383
3384 last_objectid = found_key.offset;
3385
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003386 found_key.objectid = found_key.offset;
3387 found_key.type = BTRFS_INODE_ITEM_KEY;
3388 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003389 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303390 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003391 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003392 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003393
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003394 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003395 struct btrfs_root *dead_root;
3396 struct btrfs_fs_info *fs_info = root->fs_info;
3397 int is_dead_root = 0;
3398
3399 /*
3400 * this is an orphan in the tree root. Currently these
3401 * could come from 2 sources:
3402 * a) a snapshot deletion in progress
3403 * b) a free space cache inode
3404 * We need to distinguish those two, as the snapshot
3405 * orphan must not get deleted.
3406 * find_dead_roots already ran before us, so if this
3407 * is a snapshot deletion, we should find the root
3408 * in the dead_roots list
3409 */
3410 spin_lock(&fs_info->trans_lock);
3411 list_for_each_entry(dead_root, &fs_info->dead_roots,
3412 root_list) {
3413 if (dead_root->root_key.objectid ==
3414 found_key.objectid) {
3415 is_dead_root = 1;
3416 break;
3417 }
3418 }
3419 spin_unlock(&fs_info->trans_lock);
3420 if (is_dead_root) {
3421 /* prevent this orphan from being found again */
3422 key.offset = found_key.objectid - 1;
3423 continue;
3424 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003425
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003426 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003427
Josef Bacika8c9e572011-09-21 16:55:59 -04003428 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003429 * If we have an inode with links, there are a couple of
3430 * possibilities. Old kernels (before v3.12) used to create an
3431 * orphan item for truncate indicating that there were possibly
3432 * extent items past i_size that needed to be deleted. In v3.12,
3433 * truncate was changed to update i_size in sync with the extent
3434 * items, but the (useless) orphan item was still created. Since
3435 * v4.18, we don't create the orphan item for truncate at all.
3436 *
3437 * So, this item could mean that we need to do a truncate, but
3438 * only if this filesystem was last used on a pre-v3.12 kernel
3439 * and was not cleanly unmounted. The odds of that are quite
3440 * slim, and it's a pain to do the truncate now, so just delete
3441 * the orphan item.
3442 *
3443 * It's also possible that this orphan item was supposed to be
3444 * deleted but wasn't. The inode number may have been reused,
3445 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003446 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003447 if (ret == -ENOENT || inode->i_nlink) {
3448 if (!ret)
3449 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003450 trans = btrfs_start_transaction(root, 1);
3451 if (IS_ERR(trans)) {
3452 ret = PTR_ERR(trans);
3453 goto out;
3454 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003455 btrfs_debug(fs_info, "auto deleting %Lu",
3456 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003457 ret = btrfs_del_orphan_item(trans, root,
3458 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003459 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003460 if (ret)
3461 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003462 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003463 }
Josef Bacik7b128762008-07-24 12:17:14 -04003464
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003465 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003466
3467 /* this will do delete_inode and everything for us */
3468 iput(inode);
3469 }
Miao Xie3254c872011-11-10 20:45:05 -05003470 /* release the path since we're done with it */
3471 btrfs_release_path(path);
3472
Yan, Zhengd68fc572010-05-16 10:49:58 -04003473 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3474
Omar Sandovala575cee2018-05-11 13:13:38 -07003475 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003476 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003477 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003478 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003479 }
Josef Bacik7b128762008-07-24 12:17:14 -04003480
3481 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003482 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003483
3484out:
3485 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003486 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003487 btrfs_free_path(path);
3488 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003489}
3490
Chris Masond352ac62008-09-29 15:18:18 -04003491/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003492 * very simple check to peek ahead in the leaf looking for xattrs. If we
3493 * don't find any xattrs, we know there can't be any acls.
3494 *
3495 * slot is the slot the inode is in, objectid is the objectid of the inode
3496 */
3497static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003498 int slot, u64 objectid,
3499 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003500{
3501 u32 nritems = btrfs_header_nritems(leaf);
3502 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003503 static u64 xattr_access = 0;
3504 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003505 int scanned = 0;
3506
Josef Bacikf23b5a52013-06-19 10:16:26 -04003507 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003508 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3509 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3510 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3511 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003512 }
3513
Chris Mason46a53cc2009-04-27 11:47:50 -04003514 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003515 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003516 while (slot < nritems) {
3517 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3518
3519 /* we found a different objectid, there must not be acls */
3520 if (found_key.objectid != objectid)
3521 return 0;
3522
3523 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003524 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003525 if (*first_xattr_slot == -1)
3526 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003527 if (found_key.offset == xattr_access ||
3528 found_key.offset == xattr_default)
3529 return 1;
3530 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003531
3532 /*
3533 * we found a key greater than an xattr key, there can't
3534 * be any acls later on
3535 */
3536 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3537 return 0;
3538
3539 slot++;
3540 scanned++;
3541
3542 /*
3543 * it goes inode, inode backrefs, xattrs, extents,
3544 * so if there are a ton of hard links to an inode there can
3545 * be a lot of backrefs. Don't waste time searching too hard,
3546 * this is just an optimization
3547 */
3548 if (scanned >= 8)
3549 break;
3550 }
3551 /* we hit the end of the leaf before we found an xattr or
3552 * something larger than an xattr. We have to assume the inode
3553 * has acls
3554 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003555 if (*first_xattr_slot == -1)
3556 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003557 return 1;
3558}
3559
3560/*
Chris Masond352ac62008-09-29 15:18:18 -04003561 * read an inode from the btree into the in-memory inode
3562 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003563static int btrfs_read_locked_inode(struct inode *inode,
3564 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003565{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003566 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003567 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003568 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003569 struct btrfs_inode_item *inode_item;
3570 struct btrfs_root *root = BTRFS_I(inode)->root;
3571 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003572 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003573 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003574 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003575 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003576 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003577 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003578
3579 ret = btrfs_fill_inode(inode, &rdev);
3580 if (!ret)
3581 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003582
Filipe Manana4222ea72018-10-24 10:13:03 +01003583 if (!path) {
3584 path = btrfs_alloc_path();
3585 if (!path)
3586 return -ENOMEM;
3587 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003588
Chris Mason39279cc2007-06-12 06:35:45 -04003589 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003590
Chris Mason39279cc2007-06-12 06:35:45 -04003591 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003592 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003593 if (path != in_path)
3594 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003595 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003596 }
Chris Mason39279cc2007-06-12 06:35:45 -04003597
Chris Mason5f39d392007-10-15 16:14:19 -04003598 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003599
3600 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003601 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003602
Chris Mason5f39d392007-10-15 16:14:19 -04003603 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3604 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003605 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003606 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003607 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3608 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003609 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003610
David Sterbaa937b972014-12-12 17:39:12 +01003611 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3612 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003613
David Sterbaa937b972014-12-12 17:39:12 +01003614 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3615 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003616
David Sterbaa937b972014-12-12 17:39:12 +01003617 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3618 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003619
chandan r9cc97d62012-07-04 12:48:07 +05303620 BTRFS_I(inode)->i_otime.tv_sec =
3621 btrfs_timespec_sec(leaf, &inode_item->otime);
3622 BTRFS_I(inode)->i_otime.tv_nsec =
3623 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003624
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003625 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003626 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003627 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3628
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003629 inode_set_iversion_queried(inode,
3630 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003631 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003632 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003633 rdev = btrfs_inode_rdev(leaf, inode_item);
3634
Josef Bacikaec74772008-07-24 12:12:38 -04003635 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003636 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003637
3638cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003639 /*
3640 * If we were modified in the current generation and evicted from memory
3641 * and then re-read we need to do a full sync since we don't have any
3642 * idea about which extents were modified before we were evicted from
3643 * cache.
3644 *
3645 * This is required for both inode re-read from disk and delayed inode
3646 * in delayed_nodes_tree.
3647 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003648 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003649 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3650 &BTRFS_I(inode)->runtime_flags);
3651
Filipe Mananabde6c242015-07-24 00:00:19 +01003652 /*
3653 * We don't persist the id of the transaction where an unlink operation
3654 * against the inode was last made. So here we assume the inode might
3655 * have been evicted, and therefore the exact value of last_unlink_trans
3656 * lost, and set it to last_trans to avoid metadata inconsistencies
3657 * between the inode and its parent if the inode is fsync'ed and the log
3658 * replayed. For example, in the scenario:
3659 *
3660 * touch mydir/foo
3661 * ln mydir/foo mydir/bar
3662 * sync
3663 * unlink mydir/bar
3664 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3665 * xfs_io -c fsync mydir/foo
3666 * <power failure>
3667 * mount fs, triggers fsync log replay
3668 *
3669 * We must make sure that when we fsync our inode foo we also log its
3670 * parent inode, otherwise after log replay the parent still has the
3671 * dentry with the "bar" name but our inode foo has a link count of 1
3672 * and doesn't have an inode ref with the name "bar" anymore.
3673 *
3674 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003675 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003676 * transaction commits on fsync if our inode is a directory, or if our
3677 * inode is not a directory, logging its parent unnecessarily.
3678 */
3679 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
Filipe Manana41bd6062018-11-28 14:54:28 +00003680 /*
3681 * Similar reasoning for last_link_trans, needs to be set otherwise
3682 * for a case like the following:
3683 *
3684 * mkdir A
3685 * touch foo
3686 * ln foo A/bar
3687 * echo 2 > /proc/sys/vm/drop_caches
3688 * fsync foo
3689 * <power failure>
3690 *
3691 * Would result in link bar and directory A not existing after the power
3692 * failure.
3693 */
3694 BTRFS_I(inode)->last_link_trans = BTRFS_I(inode)->last_trans;
Filipe Mananabde6c242015-07-24 00:00:19 +01003695
Miao Xie67de1172013-12-26 13:07:06 +08003696 path->slots[0]++;
3697 if (inode->i_nlink != 1 ||
3698 path->slots[0] >= btrfs_header_nritems(leaf))
3699 goto cache_acl;
3700
3701 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003702 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003703 goto cache_acl;
3704
3705 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3706 if (location.type == BTRFS_INODE_REF_KEY) {
3707 struct btrfs_inode_ref *ref;
3708
3709 ref = (struct btrfs_inode_ref *)ptr;
3710 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3711 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3712 struct btrfs_inode_extref *extref;
3713
3714 extref = (struct btrfs_inode_extref *)ptr;
3715 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3716 extref);
3717 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003718cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003719 /*
3720 * try to precache a NULL acl entry for files that don't have
3721 * any xattrs or acls
3722 */
Li Zefan33345d012011-04-20 10:31:50 +08003723 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003724 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003725 if (first_xattr_slot != -1) {
3726 path->slots[0] = first_xattr_slot;
3727 ret = btrfs_load_inode_props(inode, path);
3728 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003729 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003730 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003731 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003732 root->root_key.objectid, ret);
3733 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003734 if (path != in_path)
3735 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003736
Al Viro72c04902009-06-24 16:58:48 -04003737 if (!maybe_acls)
3738 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003739
Chris Mason39279cc2007-06-12 06:35:45 -04003740 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003741 case S_IFREG:
3742 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003743 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003744 inode->i_fop = &btrfs_file_operations;
3745 inode->i_op = &btrfs_file_inode_operations;
3746 break;
3747 case S_IFDIR:
3748 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003749 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003750 break;
3751 case S_IFLNK:
3752 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003753 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003754 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003755 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003756 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003757 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003758 init_special_inode(inode, inode->i_mode, rdev);
3759 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003760 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003761
David Sterba7b6a2212018-03-26 18:40:21 +02003762 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003763 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003764}
3765
Chris Masond352ac62008-09-29 15:18:18 -04003766/*
3767 * given a leaf and an inode, copy the inode fields into the leaf
3768 */
Chris Masone02119d2008-09-05 16:13:11 -04003769static void fill_inode_item(struct btrfs_trans_handle *trans,
3770 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003771 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003772 struct inode *inode)
3773{
Liu Bo51fab692012-12-27 09:01:21 +00003774 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003775
Liu Bo51fab692012-12-27 09:01:21 +00003776 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003777
Liu Bo51fab692012-12-27 09:01:21 +00003778 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3779 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3780 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3781 &token);
3782 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3783 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003784
David Sterbaa937b972014-12-12 17:39:12 +01003785 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003786 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003787 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003788 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003789
David Sterbaa937b972014-12-12 17:39:12 +01003790 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003791 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003792 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003793 inode->i_mtime.tv_nsec, &token);
3794
David Sterbaa937b972014-12-12 17:39:12 +01003795 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003796 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003797 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003798 inode->i_ctime.tv_nsec, &token);
3799
chandan r9cc97d62012-07-04 12:48:07 +05303800 btrfs_set_token_timespec_sec(leaf, &item->otime,
3801 BTRFS_I(inode)->i_otime.tv_sec, &token);
3802 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3803 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3804
Liu Bo51fab692012-12-27 09:01:21 +00003805 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3806 &token);
3807 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3808 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003809 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3810 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003811 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3812 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3813 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3814 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003815}
3816
Chris Masond352ac62008-09-29 15:18:18 -04003817/*
3818 * copy everything in the in-memory inode into the btree.
3819 */
Chris Mason21151332011-11-10 20:39:08 -05003820static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003821 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003822{
3823 struct btrfs_inode_item *inode_item;
3824 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003825 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003826 int ret;
3827
3828 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003829 if (!path)
3830 return -ENOMEM;
3831
Chris Masonb9473432009-03-13 11:00:37 -04003832 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003833 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3834 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003835 if (ret) {
3836 if (ret > 0)
3837 ret = -ENOENT;
3838 goto failed;
3839 }
3840
Chris Mason5f39d392007-10-15 16:14:19 -04003841 leaf = path->nodes[0];
3842 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003843 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003844
Chris Masone02119d2008-09-05 16:13:11 -04003845 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003846 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003847 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003848 ret = 0;
3849failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003850 btrfs_free_path(path);
3851 return ret;
3852}
3853
Chris Masond352ac62008-09-29 15:18:18 -04003854/*
Chris Mason21151332011-11-10 20:39:08 -05003855 * copy everything in the in-memory inode into the btree.
3856 */
3857noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3858 struct btrfs_root *root, struct inode *inode)
3859{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003860 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003861 int ret;
3862
3863 /*
3864 * If the inode is a free space inode, we can deadlock during commit
3865 * if we put it into the delayed code.
3866 *
3867 * The data relocation inode should also be directly updated
3868 * without delay
3869 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003870 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003871 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003872 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003873 btrfs_update_root_times(trans, root);
3874
Chris Mason21151332011-11-10 20:39:08 -05003875 ret = btrfs_delayed_update_inode(trans, root, inode);
3876 if (!ret)
3877 btrfs_set_inode_last_trans(trans, inode);
3878 return ret;
3879 }
3880
3881 return btrfs_update_inode_item(trans, root, inode);
3882}
3883
Josef Bacikbe6aef62012-10-22 15:43:12 -04003884noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3885 struct btrfs_root *root,
3886 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003887{
3888 int ret;
3889
3890 ret = btrfs_update_inode(trans, root, inode);
3891 if (ret == -ENOSPC)
3892 return btrfs_update_inode_item(trans, root, inode);
3893 return ret;
3894}
3895
3896/*
Chris Masond352ac62008-09-29 15:18:18 -04003897 * unlink helper that gets used here in inode.c and in the tree logging
3898 * recovery code. It remove a link in a directory with a given name, and
3899 * also drops the back refs in the inode to the directory
3900 */
Al Viro92986792011-03-04 17:14:37 +00003901static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3902 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003903 struct btrfs_inode *dir,
3904 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003905 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003906{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003907 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003909 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003910 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003911 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003912 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003913 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003914 u64 ino = btrfs_ino(inode);
3915 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003916
3917 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003918 if (!path) {
3919 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003920 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003921 }
3922
Chris Masonb9473432009-03-13 11:00:37 -04003923 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003924 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003925 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003926 if (IS_ERR_OR_NULL(di)) {
3927 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003928 goto err;
3929 }
Chris Mason5f39d392007-10-15 16:14:19 -04003930 leaf = path->nodes[0];
3931 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003932 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003933 if (ret)
3934 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003935 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003936
Miao Xie67de1172013-12-26 13:07:06 +08003937 /*
3938 * If we don't have dir index, we have to get it by looking up
3939 * the inode ref, since we get the inode ref, remove it directly,
3940 * it is unnecessary to do delayed deletion.
3941 *
3942 * But if we have dir index, needn't search inode ref to get it.
3943 * Since the inode ref is close to the inode item, it is better
3944 * that we delay to delete it, and just do this deletion when
3945 * we update the inode item.
3946 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003947 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003948 ret = btrfs_delayed_delete_inode_ref(inode);
3949 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003950 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003951 goto skip_backref;
3952 }
3953 }
3954
Li Zefan33345d012011-04-20 10:31:50 +08003955 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3956 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003957 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003958 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003959 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003960 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003961 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003962 goto err;
3963 }
Miao Xie67de1172013-12-26 13:07:06 +08003964skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003965 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003966 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003967 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003968 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003969 }
Chris Mason39279cc2007-06-12 06:35:45 -04003970
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003971 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3972 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003973 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003974 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003975 goto err;
3976 }
Chris Masone02119d2008-09-05 16:13:11 -04003977
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003978 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3979 index);
Chris Mason6418c962010-10-30 07:34:24 -04003980 if (ret == -ENOENT)
3981 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003982 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003983 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003984err:
3985 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003986 if (ret)
3987 goto out;
3988
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003989 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003990 inode_inc_iversion(&inode->vfs_inode);
3991 inode_inc_iversion(&dir->vfs_inode);
3992 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3993 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3994 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003995out:
Chris Mason39279cc2007-06-12 06:35:45 -04003996 return ret;
3997}
3998
Al Viro92986792011-03-04 17:14:37 +00003999int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4000 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004001 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004002 const char *name, int name_len)
4003{
4004 int ret;
4005 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4006 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004007 drop_nlink(&inode->vfs_inode);
4008 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004009 }
4010 return ret;
4011}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004012
4013/*
4014 * helper to start transaction for unlink and rmdir.
4015 *
Josef Bacikd52be812013-05-29 14:54:47 -04004016 * unlink and rmdir are special in btrfs, they do not always free space, so
4017 * if we cannot make our reservations the normal way try and see if there is
4018 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4019 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020 */
Josef Bacikd52be812013-05-29 14:54:47 -04004021static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004022{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004023 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024
Josef Bacike70bea52011-10-11 14:18:24 -04004025 /*
4026 * 1 for the possible orphan item
4027 * 1 for the dir item
4028 * 1 for the dir index
4029 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004030 * 1 for the inode
4031 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004032 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004033}
4034
Chris Mason39279cc2007-06-12 06:35:45 -04004035static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4036{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004037 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004038 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004039 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004040 int ret;
4041
Josef Bacikd52be812013-05-29 14:54:47 -04004042 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004043 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004044 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004045
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004046 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4047 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004048
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004049 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4050 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4051 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004052 if (ret)
4053 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004054
Yan, Zhenga22285a2010-05-16 10:48:46 -04004055 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004056 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004057 if (ret)
4058 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004059 }
Josef Bacik7b128762008-07-24 12:17:14 -04004060
Tsutomu Itohb5324022011-07-19 07:27:20 +00004061out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004062 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004063 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004064 return ret;
4065}
4066
Misono Tomohirof60a2362018-04-18 11:34:52 +09004067static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Lu Fengqi401b3b12018-08-01 11:32:30 +08004068 struct inode *dir, u64 objectid,
4069 const char *name, int name_len)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004070{
Lu Fengqi401b3b12018-08-01 11:32:30 +08004071 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004072 struct btrfs_path *path;
4073 struct extent_buffer *leaf;
4074 struct btrfs_dir_item *di;
4075 struct btrfs_key key;
4076 u64 index;
4077 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004078 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004079
4080 path = btrfs_alloc_path();
4081 if (!path)
4082 return -ENOMEM;
4083
Li Zefan33345d012011-04-20 10:31:50 +08004084 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004085 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004086 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08004087 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004088 goto out;
4089 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004090
4091 leaf = path->nodes[0];
4092 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4093 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4094 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004095 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004096 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004097 goto out;
4098 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004099 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100
Lu Fengqi3ee1c552018-08-01 11:32:28 +08004101 ret = btrfs_del_root_ref(trans, objectid, root->root_key.objectid,
4102 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004103 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004104 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004105 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004106 goto out;
4107 }
Li Zefan33345d012011-04-20 10:31:50 +08004108 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004109 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004110 if (IS_ERR_OR_NULL(di)) {
4111 if (!di)
4112 ret = -ENOENT;
4113 else
4114 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004115 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116 goto out;
4117 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004118
4119 leaf = path->nodes[0];
4120 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004121 index = key.offset;
4122 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004123 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004124
Lu Fengqi9add2942018-08-01 11:32:26 +08004125 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004126 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004127 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004128 goto out;
4129 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004130
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004131 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004132 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004133 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004134 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004136 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004137out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004138 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004139 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004140}
4141
Misono Tomohiroec42f162018-04-18 11:34:13 +09004142/*
4143 * Helper to check if the subvolume references other subvolumes or if it's
4144 * default.
4145 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004146static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004147{
4148 struct btrfs_fs_info *fs_info = root->fs_info;
4149 struct btrfs_path *path;
4150 struct btrfs_dir_item *di;
4151 struct btrfs_key key;
4152 u64 dir_id;
4153 int ret;
4154
4155 path = btrfs_alloc_path();
4156 if (!path)
4157 return -ENOMEM;
4158
4159 /* Make sure this root isn't set as the default subvol */
4160 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4161 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4162 dir_id, "default", 7, 0);
4163 if (di && !IS_ERR(di)) {
4164 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4165 if (key.objectid == root->root_key.objectid) {
4166 ret = -EPERM;
4167 btrfs_err(fs_info,
4168 "deleting default subvolume %llu is not allowed",
4169 key.objectid);
4170 goto out;
4171 }
4172 btrfs_release_path(path);
4173 }
4174
4175 key.objectid = root->root_key.objectid;
4176 key.type = BTRFS_ROOT_REF_KEY;
4177 key.offset = (u64)-1;
4178
4179 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4180 if (ret < 0)
4181 goto out;
4182 BUG_ON(ret == 0);
4183
4184 ret = 0;
4185 if (path->slots[0] > 0) {
4186 path->slots[0]--;
4187 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4188 if (key.objectid == root->root_key.objectid &&
4189 key.type == BTRFS_ROOT_REF_KEY)
4190 ret = -ENOTEMPTY;
4191 }
4192out:
4193 btrfs_free_path(path);
4194 return ret;
4195}
4196
Nikolay Borisov20a68002018-04-27 14:36:24 +03004197/* Delete all dentries for inodes belonging to the root */
4198static void btrfs_prune_dentries(struct btrfs_root *root)
4199{
4200 struct btrfs_fs_info *fs_info = root->fs_info;
4201 struct rb_node *node;
4202 struct rb_node *prev;
4203 struct btrfs_inode *entry;
4204 struct inode *inode;
4205 u64 objectid = 0;
4206
4207 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4208 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4209
4210 spin_lock(&root->inode_lock);
4211again:
4212 node = root->inode_tree.rb_node;
4213 prev = NULL;
4214 while (node) {
4215 prev = node;
4216 entry = rb_entry(node, struct btrfs_inode, rb_node);
4217
David Sterba37508512018-06-29 10:56:40 +02004218 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004219 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004220 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004221 node = node->rb_right;
4222 else
4223 break;
4224 }
4225 if (!node) {
4226 while (prev) {
4227 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004228 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004229 node = prev;
4230 break;
4231 }
4232 prev = rb_next(prev);
4233 }
4234 }
4235 while (node) {
4236 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004237 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004238 inode = igrab(&entry->vfs_inode);
4239 if (inode) {
4240 spin_unlock(&root->inode_lock);
4241 if (atomic_read(&inode->i_count) > 1)
4242 d_prune_aliases(inode);
4243 /*
4244 * btrfs_drop_inode will have it removed from the inode
4245 * cache when its usage count hits zero.
4246 */
4247 iput(inode);
4248 cond_resched();
4249 spin_lock(&root->inode_lock);
4250 goto again;
4251 }
4252
4253 if (cond_resched_lock(&root->inode_lock))
4254 goto again;
4255
4256 node = rb_next(node);
4257 }
4258 spin_unlock(&root->inode_lock);
4259}
4260
Misono Tomohirof60a2362018-04-18 11:34:52 +09004261int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4262{
4263 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4264 struct btrfs_root *root = BTRFS_I(dir)->root;
4265 struct inode *inode = d_inode(dentry);
4266 struct btrfs_root *dest = BTRFS_I(inode)->root;
4267 struct btrfs_trans_handle *trans;
4268 struct btrfs_block_rsv block_rsv;
4269 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004270 int ret;
4271 int err;
4272
4273 /*
4274 * Don't allow to delete a subvolume with send in progress. This is
4275 * inside the inode lock so the error handling that has to drop the bit
4276 * again is not run concurrently.
4277 */
4278 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004279 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004280 spin_unlock(&dest->root_item_lock);
4281 btrfs_warn(fs_info,
4282 "attempt to delete subvolume %llu during send",
4283 dest->root_key.objectid);
4284 return -EPERM;
4285 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004286 root_flags = btrfs_root_flags(&dest->root_item);
4287 btrfs_set_root_flags(&dest->root_item,
4288 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4289 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004290
4291 down_write(&fs_info->subvol_sem);
4292
4293 err = may_destroy_subvol(dest);
4294 if (err)
4295 goto out_up_write;
4296
4297 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4298 /*
4299 * One for dir inode,
4300 * two for dir entries,
4301 * two for root ref/backref.
4302 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004303 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004304 if (err)
4305 goto out_up_write;
4306
4307 trans = btrfs_start_transaction(root, 0);
4308 if (IS_ERR(trans)) {
4309 err = PTR_ERR(trans);
4310 goto out_release;
4311 }
4312 trans->block_rsv = &block_rsv;
4313 trans->bytes_reserved = block_rsv.size;
4314
4315 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4316
Lu Fengqi401b3b12018-08-01 11:32:30 +08004317 ret = btrfs_unlink_subvol(trans, dir, dest->root_key.objectid,
4318 dentry->d_name.name, dentry->d_name.len);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004319 if (ret) {
4320 err = ret;
4321 btrfs_abort_transaction(trans, ret);
4322 goto out_end_trans;
4323 }
4324
4325 btrfs_record_root_in_trans(trans, dest);
4326
4327 memset(&dest->root_item.drop_progress, 0,
4328 sizeof(dest->root_item.drop_progress));
4329 dest->root_item.drop_level = 0;
4330 btrfs_set_root_refs(&dest->root_item, 0);
4331
4332 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4333 ret = btrfs_insert_orphan_item(trans,
4334 fs_info->tree_root,
4335 dest->root_key.objectid);
4336 if (ret) {
4337 btrfs_abort_transaction(trans, ret);
4338 err = ret;
4339 goto out_end_trans;
4340 }
4341 }
4342
Lu Fengqid1957792018-05-29 15:01:54 +08004343 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004344 BTRFS_UUID_KEY_SUBVOL,
4345 dest->root_key.objectid);
4346 if (ret && ret != -ENOENT) {
4347 btrfs_abort_transaction(trans, ret);
4348 err = ret;
4349 goto out_end_trans;
4350 }
4351 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004352 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004353 dest->root_item.received_uuid,
4354 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4355 dest->root_key.objectid);
4356 if (ret && ret != -ENOENT) {
4357 btrfs_abort_transaction(trans, ret);
4358 err = ret;
4359 goto out_end_trans;
4360 }
4361 }
4362
4363out_end_trans:
4364 trans->block_rsv = NULL;
4365 trans->bytes_reserved = 0;
4366 ret = btrfs_end_transaction(trans);
4367 if (ret && !err)
4368 err = ret;
4369 inode->i_flags |= S_DEAD;
4370out_release:
4371 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4372out_up_write:
4373 up_write(&fs_info->subvol_sem);
4374 if (err) {
4375 spin_lock(&dest->root_item_lock);
4376 root_flags = btrfs_root_flags(&dest->root_item);
4377 btrfs_set_root_flags(&dest->root_item,
4378 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4379 spin_unlock(&dest->root_item_lock);
4380 } else {
4381 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004382 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004383 ASSERT(dest->send_in_progress == 0);
4384
4385 /* the last ref */
4386 if (dest->ino_cache_inode) {
4387 iput(dest->ino_cache_inode);
4388 dest->ino_cache_inode = NULL;
4389 }
4390 }
4391
4392 return err;
4393}
4394
Chris Mason39279cc2007-06-12 06:35:45 -04004395static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4396{
David Howells2b0143b2015-03-17 22:25:59 +00004397 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004398 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004399 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004400 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004401 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004402
David Sterbab3ae2442012-09-13 16:04:34 -06004403 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004404 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004405 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004406 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004407
Josef Bacikd52be812013-05-29 14:54:47 -04004408 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004409 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004410 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004411
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004412 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Lu Fengqi401b3b12018-08-01 11:32:30 +08004413 err = btrfs_unlink_subvol(trans, dir,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004414 BTRFS_I(inode)->location.objectid,
4415 dentry->d_name.name,
4416 dentry->d_name.len);
4417 goto out;
4418 }
4419
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004420 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004421 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004422 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004423
Filipe Manana44f714d2016-06-06 16:11:13 +01004424 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4425
Chris Mason39279cc2007-06-12 06:35:45 -04004426 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004427 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4428 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4429 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004430 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004431 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004432 /*
4433 * Propagate the last_unlink_trans value of the deleted dir to
4434 * its parent directory. This is to prevent an unrecoverable
4435 * log tree in the case we do something like this:
4436 * 1) create dir foo
4437 * 2) create snapshot under dir foo
4438 * 3) delete the snapshot
4439 * 4) rmdir foo
4440 * 5) mkdir foo
4441 * 6) fsync foo or some file inside foo
4442 */
4443 if (last_unlink_trans >= trans->transid)
4444 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4445 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004446out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004447 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004448 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004449
Chris Mason39279cc2007-06-12 06:35:45 -04004450 return err;
4451}
4452
Josef Bacikddfae632017-10-19 14:16:02 -04004453/*
4454 * Return this if we need to call truncate_block for the last bit of the
4455 * truncate.
4456 */
4457#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004458
Chris Mason323ac952008-10-01 19:05:46 -04004459/*
Chris Mason39279cc2007-06-12 06:35:45 -04004460 * this can truncate away extent items, csum items and directory items.
4461 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004462 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004463 *
4464 * csum items that cross the new i_size are truncated to the new size
4465 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004466 *
4467 * min_type is the minimum key type to truncate down to. If set to 0, this
4468 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004469 */
Yan, Zheng80825102009-11-12 09:35:36 +00004470int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4471 struct btrfs_root *root,
4472 struct inode *inode,
4473 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004474{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004475 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004476 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004477 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004478 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004479 struct btrfs_key key;
4480 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004481 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004482 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004483 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004484 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004485 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004486 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004487 int found_extent;
4488 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004489 int pending_del_nr = 0;
4490 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004491 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004492 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004493 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004494 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004495 bool be_nice = false;
4496 bool should_throttle = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004497
4498 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004499
Chris Mason28ed1342014-12-17 09:41:04 -08004500 /*
4501 * for non-free space inodes and ref cows, we want to back off from
4502 * time to time
4503 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004504 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004505 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004506 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004507
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004508 path = btrfs_alloc_path();
4509 if (!path)
4510 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004511 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004512
Josef Bacik5dc562c2012-08-17 13:14:17 -04004513 /*
4514 * We want to drop from the next block forward in case this new size is
4515 * not block aligned since we will be keeping the last block of the
4516 * extent just the way it is.
4517 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004518 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004519 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004520 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004521 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004522 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004523
Miao Xie16cdcec2011-04-22 18:12:22 +08004524 /*
4525 * This function is also used to drop the items in the log tree before
4526 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004527 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004528 * items.
4529 */
4530 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004531 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004532
Li Zefan33345d012011-04-20 10:31:50 +08004533 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004534 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004535 key.type = (u8)-1;
4536
Chris Mason85e21ba2008-01-29 15:11:36 -05004537search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004538 /*
4539 * with a 16K leaf size and 128MB extents, you can actually queue
4540 * up a huge file in a single leaf. Most of the time that
4541 * bytes_deleted is > 0, it will be huge by the time we get here
4542 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004543 if (be_nice && bytes_deleted > SZ_32M &&
4544 btrfs_should_end_transaction(trans)) {
4545 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004546 goto out;
4547 }
Chris Masond3977122009-01-05 21:25:51 -05004548
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004549 path->leave_spinning = 1;
4550 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4551 if (ret < 0)
4552 goto out;
4553
Chris Mason85e21ba2008-01-29 15:11:36 -05004554 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004555 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004556 /* there are no items in the tree for us to truncate, we're
4557 * done
4558 */
Yan, Zheng80825102009-11-12 09:35:36 +00004559 if (path->slots[0] == 0)
4560 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004561 path->slots[0]--;
4562 }
4563
Chris Masond3977122009-01-05 21:25:51 -05004564 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004565 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004566 leaf = path->nodes[0];
4567 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004568 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004569
Li Zefan33345d012011-04-20 10:31:50 +08004570 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004571 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004572
Chris Mason85e21ba2008-01-29 15:11:36 -05004573 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004574 break;
4575
Chris Mason5f39d392007-10-15 16:14:19 -04004576 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004577 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004578 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004579 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004580 extent_type = btrfs_file_extent_type(leaf, fi);
4581 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004582 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004583 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004584
4585 trace_btrfs_truncate_show_fi_regular(
4586 BTRFS_I(inode), leaf, fi,
4587 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004588 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004589 item_end += btrfs_file_extent_ram_bytes(leaf,
4590 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004591
4592 trace_btrfs_truncate_show_fi_inline(
4593 BTRFS_I(inode), leaf, fi, path->slots[0],
4594 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004595 }
Yan008630c2007-11-07 13:31:09 -05004596 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004597 }
Yan, Zheng80825102009-11-12 09:35:36 +00004598 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004599 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004600 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004601 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004602 break;
4603 if (found_key.offset >= new_size)
4604 del_item = 1;
4605 else
4606 del_item = 0;
4607 }
Chris Mason39279cc2007-06-12 06:35:45 -04004608 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004609 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004610 if (found_type != BTRFS_EXTENT_DATA_KEY)
4611 goto delete;
4612
4613 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004614 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004615 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004616 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004617 u64 orig_num_bytes =
4618 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004619 extent_num_bytes = ALIGN(new_size -
4620 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004621 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004622 btrfs_set_file_extent_num_bytes(leaf, fi,
4623 extent_num_bytes);
4624 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004625 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004626 if (test_bit(BTRFS_ROOT_REF_COWS,
4627 &root->state) &&
4628 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004629 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004630 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004631 } else {
Chris Masondb945352007-10-15 16:15:53 -04004632 extent_num_bytes =
4633 btrfs_file_extent_disk_num_bytes(leaf,
4634 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004635 extent_offset = found_key.offset -
4636 btrfs_file_extent_offset(leaf, fi);
4637
Chris Mason39279cc2007-06-12 06:35:45 -04004638 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004639 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004640 if (extent_start != 0) {
4641 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004642 if (test_bit(BTRFS_ROOT_REF_COWS,
4643 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004644 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004645 }
4646 }
Chris Mason90692182008-02-08 13:49:28 -05004647 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004648 /*
4649 * we can't truncate inline items that have had
4650 * special encodings
4651 */
4652 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004653 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004654 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4655 btrfs_file_extent_compression(leaf, fi) == 0) {
4656 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004657
Josef Bacikddfae632017-10-19 14:16:02 -04004658 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4659 size = btrfs_file_extent_calc_inline_size(size);
4660 btrfs_truncate_item(root->fs_info, path, size, 1);
4661 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004662 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004663 * We have to bail so the last_size is set to
4664 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004665 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004666 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004667 break;
Chris Mason90692182008-02-08 13:49:28 -05004668 }
Josef Bacikddfae632017-10-19 14:16:02 -04004669
4670 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4671 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004672 }
Chris Mason179e29e2007-11-01 11:28:41 -04004673delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004674 if (del_item)
4675 last_size = found_key.offset;
4676 else
4677 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004678 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004679 if (!pending_del_nr) {
4680 /* no pending yet, add ourselves */
4681 pending_del_slot = path->slots[0];
4682 pending_del_nr = 1;
4683 } else if (pending_del_nr &&
4684 path->slots[0] + 1 == pending_del_slot) {
4685 /* hop on the pending chunk */
4686 pending_del_nr++;
4687 pending_del_slot = path->slots[0];
4688 } else {
Chris Masond3977122009-01-05 21:25:51 -05004689 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004690 }
Chris Mason39279cc2007-06-12 06:35:45 -04004691 } else {
4692 break;
4693 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004694 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004695
Miao Xie27cdeb72014-04-02 19:51:05 +08004696 if (found_extent &&
4697 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004698 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004699 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004700 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004701 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004702 extent_num_bytes, 0,
4703 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004704 ino, extent_offset);
Omar Sandoval05522102018-05-11 13:13:31 -07004705 if (ret) {
4706 btrfs_abort_transaction(trans, ret);
4707 break;
4708 }
Chris Mason28f75a02015-02-04 06:59:29 -08004709 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004710 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004711 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004712 }
Chris Mason39279cc2007-06-12 06:35:45 -04004713 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004714
Yan, Zheng80825102009-11-12 09:35:36 +00004715 if (found_type == BTRFS_INODE_ITEM_KEY)
4716 break;
4717
4718 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004719 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004720 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004721 if (pending_del_nr) {
4722 ret = btrfs_del_items(trans, root, path,
4723 pending_del_slot,
4724 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004725 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004726 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004727 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004728 }
Yan, Zheng80825102009-11-12 09:35:36 +00004729 pending_del_nr = 0;
4730 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004731 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004732
Chris Mason28f75a02015-02-04 06:59:29 -08004733 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004734 * We can generate a lot of delayed refs, so we need to
4735 * throttle every once and a while and make sure we're
4736 * adding enough space to keep up with the work we are
4737 * generating. Since we hold a transaction here we
4738 * can't flush, and we don't want to FLUSH_LIMIT because
4739 * we could have generated too many delayed refs to
4740 * actually allocate, so just bail if we're short and
4741 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004742 */
Josef Bacik28bad212018-12-03 10:20:38 -05004743 if (should_throttle) {
4744 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4745 BTRFS_RESERVE_NO_FLUSH);
4746 if (ret) {
4747 ret = -EAGAIN;
4748 break;
4749 }
Chris Mason28f75a02015-02-04 06:59:29 -08004750 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004751 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004752 } else {
4753 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004754 }
Chris Mason39279cc2007-06-12 06:35:45 -04004755 }
Yan, Zheng80825102009-11-12 09:35:36 +00004756out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004757 if (ret >= 0 && pending_del_nr) {
4758 int err;
4759
4760 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004761 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004762 if (err) {
4763 btrfs_abort_transaction(trans, err);
4764 ret = err;
4765 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004766 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004767 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4768 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004769 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004770 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004771 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004772 }
Chris Mason28ed1342014-12-17 09:41:04 -08004773
Chris Mason39279cc2007-06-12 06:35:45 -04004774 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004775 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004776}
4777
Chris Masona52d9a82007-08-27 16:49:44 -04004778/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304779 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004780 * @inode - inode that we're zeroing
4781 * @from - the offset to start zeroing
4782 * @len - the length to zero, 0 to zero the entire range respective to the
4783 * offset
4784 * @front - zero up to the offset instead of from the offset on
4785 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304786 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004787 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004788 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304789int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004790 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004791{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004792 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004793 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004794 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4795 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004796 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004797 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004798 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004799 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004800 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304801 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004802 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004803 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004804 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304805 u64 block_start;
4806 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004807
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004808 if (IS_ALIGNED(offset, blocksize) &&
4809 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004810 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304811
Josef Bacik8b62f872017-10-19 14:15:55 -04004812 block_start = round_down(from, blocksize);
4813 block_end = block_start + blocksize - 1;
4814
Qu Wenruo364ecf32017-02-27 15:10:38 +08004815 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004816 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004817 if (ret)
4818 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004819
Chris Mason211c17f2008-05-15 09:13:45 -04004820again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004821 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004822 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004823 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004824 block_start, blocksize, true);
4825 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004826 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004827 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004828 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004829
Chris Masona52d9a82007-08-27 16:49:44 -04004830 if (!PageUptodate(page)) {
4831 ret = btrfs_readpage(NULL, page);
4832 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004833 if (page->mapping != mapping) {
4834 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004835 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004836 goto again;
4837 }
Chris Masona52d9a82007-08-27 16:49:44 -04004838 if (!PageUptodate(page)) {
4839 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004840 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004841 }
4842 }
Chris Mason211c17f2008-05-15 09:13:45 -04004843 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004844
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304845 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004846 set_page_extent_mapped(page);
4847
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304848 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004849 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304850 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004851 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004852 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004853 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004854 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004855 btrfs_put_ordered_extent(ordered);
4856 goto again;
4857 }
4858
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304859 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004860 EXTENT_DIRTY | EXTENT_DELALLOC |
4861 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004862 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004863
Filipe Mananae3b8a482017-11-04 00:16:59 +00004864 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004865 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004866 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304867 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004868 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004869 goto out_unlock;
4870 }
4871
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304872 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004873 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304874 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004875 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004876 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304877 memset(kaddr + (block_start - page_offset(page)),
4878 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004879 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304880 memset(kaddr + (block_start - page_offset(page)) + offset,
4881 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004882 flush_dcache_page(page);
4883 kunmap(page);
4884 }
Chris Mason247e7432008-07-17 12:53:51 -04004885 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004886 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004887 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004888
Chris Mason89642222008-07-24 09:41:53 -04004889out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004890 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004891 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004892 blocksize, true);
4893 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004894 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004895 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004896out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004897 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004898 return ret;
4899}
4900
Josef Bacik16e75492013-10-22 12:18:51 -04004901static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4902 u64 offset, u64 len)
4903{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004904 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004905 struct btrfs_trans_handle *trans;
4906 int ret;
4907
4908 /*
4909 * Still need to make sure the inode looks like it's been updated so
4910 * that any holes get logged if we fsync.
4911 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004912 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4913 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004914 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4915 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4916 return 0;
4917 }
4918
4919 /*
4920 * 1 - for the one we're dropping
4921 * 1 - for the one we're adding
4922 * 1 - for updating the inode.
4923 */
4924 trans = btrfs_start_transaction(root, 3);
4925 if (IS_ERR(trans))
4926 return PTR_ERR(trans);
4927
4928 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4929 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004930 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004931 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004932 return ret;
4933 }
4934
David Sterbaf85b7372017-01-20 14:54:07 +01004935 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4936 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004937 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004938 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004939 else
4940 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004941 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004942 return ret;
4943}
4944
Josef Bacik695a0d02011-03-04 15:46:53 -05004945/*
4946 * This function puts in dummy file extents for the area we're creating a hole
4947 * for. So if we are truncating this file to a larger size we need to insert
4948 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4949 * the range between oldsize and size
4950 */
Josef Bacika41ad392011-01-31 15:30:16 -05004951int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004952{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004953 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004954 struct btrfs_root *root = BTRFS_I(inode)->root;
4955 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004956 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004957 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004958 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004959 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4960 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004961 u64 last_byte;
4962 u64 cur_offset;
4963 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004964 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004965
Josef Bacika71754f2013-06-17 17:14:39 -04004966 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304967 * If our size started in the middle of a block we need to zero out the
4968 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004969 * expose stale data.
4970 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304971 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004972 if (err)
4973 return err;
4974
Yan Zheng9036c102008-10-30 14:19:41 -04004975 if (size <= hole_start)
4976 return 0;
4977
Yan Zheng9036c102008-10-30 14:19:41 -04004978 while (1) {
4979 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004980
David Sterbaff13db42015-12-03 14:30:40 +01004981 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004982 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004983 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004984 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004985 if (!ordered)
4986 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004987 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004988 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004989 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004990 btrfs_put_ordered_extent(ordered);
4991 }
4992
Yan Zheng9036c102008-10-30 14:19:41 -04004993 cur_offset = hole_start;
4994 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004995 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004996 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004997 if (IS_ERR(em)) {
4998 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004999 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005000 break;
5001 }
Yan Zheng9036c102008-10-30 14:19:41 -04005002 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005003 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00005004 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04005005 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04005006 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005007
Josef Bacik16e75492013-10-22 12:18:51 -04005008 err = maybe_insert_hole(root, inode, cur_offset,
5009 hole_size);
5010 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05005011 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005012 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005013 cur_offset + hole_size - 1, 0);
5014 hole_em = alloc_extent_map();
5015 if (!hole_em) {
5016 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5017 &BTRFS_I(inode)->runtime_flags);
5018 goto next;
5019 }
5020 hole_em->start = cur_offset;
5021 hole_em->len = hole_size;
5022 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005023
Josef Bacik5dc562c2012-08-17 13:14:17 -04005024 hole_em->block_start = EXTENT_MAP_HOLE;
5025 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005026 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005027 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005028 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005029 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005030 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005031
5032 while (1) {
5033 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005034 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005035 write_unlock(&em_tree->lock);
5036 if (err != -EEXIST)
5037 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005038 btrfs_drop_extent_cache(BTRFS_I(inode),
5039 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005040 cur_offset +
5041 hole_size - 1, 0);
5042 }
5043 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005044 }
Josef Bacik16e75492013-10-22 12:18:51 -04005045next:
Yan Zheng9036c102008-10-30 14:19:41 -04005046 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005047 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005048 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005049 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005050 break;
5051 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005052 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005053 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005054 return err;
5055}
5056
Eric Sandeen3972f262013-01-12 02:57:22 +00005057static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005058{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005059 struct btrfs_root *root = BTRFS_I(inode)->root;
5060 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005061 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005062 loff_t newsize = attr->ia_size;
5063 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005064 int ret;
5065
Eric Sandeen3972f262013-01-12 02:57:22 +00005066 /*
5067 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5068 * special case where we need to update the times despite not having
5069 * these flags set. For all other operations the VFS set these flags
5070 * explicitly if it wants a timestamp update.
5071 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005072 if (newsize != oldsize) {
5073 inode_inc_iversion(inode);
5074 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5075 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005076 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005077 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005078
Josef Bacika41ad392011-01-31 15:30:16 -05005079 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005080 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005081 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005082 * This is to ensure the snapshot captures a fully consistent
5083 * state of this file - if the snapshot captures this expanding
5084 * truncation, it must capture all writes that happened before
5085 * this truncation.
5086 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005087 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005088 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005089 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005090 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005091 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005092 }
Yan, Zheng80825102009-11-12 09:35:36 +00005093
Miao Xief4a2f4c2011-12-14 20:12:01 -05005094 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005095 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005096 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005097 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005098 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005099
5100 i_size_write(inode, newsize);
5101 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305102 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005103 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005104 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005105 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005106 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005107
Josef Bacika41ad392011-01-31 15:30:16 -05005108 /*
5109 * We're truncating a file that used to have good data down to
5110 * zero. Make sure it gets into the ordered flush list so that
5111 * any new writes get down to disk quickly.
5112 */
5113 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005114 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5115 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005116
Josef Bacika41ad392011-01-31 15:30:16 -05005117 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005118
Andrea Gelmini52042d82018-11-28 12:05:13 +01005119 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005120 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005121 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005122 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005123
Filipe Manana213e8c52018-02-06 20:40:31 +00005124 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005125 if (ret && inode->i_nlink) {
5126 int err;
5127
5128 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005129 * Truncate failed, so fix up the in-memory size. We
5130 * adjusted disk_i_size down as we removed extents, so
5131 * wait for disk_i_size to be stable and then update the
5132 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005133 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005134 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005135 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005136 return err;
5137 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005138 }
Yan, Zheng80825102009-11-12 09:35:36 +00005139 }
5140
Josef Bacika41ad392011-01-31 15:30:16 -05005141 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005142}
5143
Chris Mason39279cc2007-06-12 06:35:45 -04005144static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5145{
David Howells2b0143b2015-03-17 22:25:59 +00005146 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005147 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005148 int err;
5149
Li Zefanb83cc962010-12-20 16:04:08 +08005150 if (btrfs_root_readonly(root))
5151 return -EROFS;
5152
Jan Kara31051c82016-05-26 16:55:18 +02005153 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005154 if (err)
5155 return err;
5156
Chris Mason5a3f23d2009-03-31 13:27:11 -04005157 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005158 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005159 if (err)
5160 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005161 }
Yan Zheng9036c102008-10-30 14:19:41 -04005162
Christoph Hellwig10257742010-06-04 11:30:02 +02005163 if (attr->ia_valid) {
5164 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005165 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005166 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005167
Josef Bacik22c44fe2011-11-30 10:45:38 -05005168 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005169 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005170 }
5171
Chris Mason39279cc2007-06-12 06:35:45 -04005172 return err;
5173}
Chris Mason61295eb2008-01-14 16:24:38 -05005174
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005175/*
5176 * While truncating the inode pages during eviction, we get the VFS calling
5177 * btrfs_invalidatepage() against each page of the inode. This is slow because
5178 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5179 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5180 * extent_state structures over and over, wasting lots of time.
5181 *
5182 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5183 * those expensive operations on a per page basis and do only the ordered io
5184 * finishing, while we release here the extent_map and extent_state structures,
5185 * without the excessive merging and splitting.
5186 */
5187static void evict_inode_truncate_pages(struct inode *inode)
5188{
5189 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5190 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5191 struct rb_node *node;
5192
5193 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005194 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005195
5196 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005197 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005198 struct extent_map *em;
5199
Liu Bo07e1ce02018-08-23 03:51:52 +08005200 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005201 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005202 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5203 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005204 remove_extent_mapping(map_tree, em);
5205 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005206 if (need_resched()) {
5207 write_unlock(&map_tree->lock);
5208 cond_resched();
5209 write_lock(&map_tree->lock);
5210 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005211 }
5212 write_unlock(&map_tree->lock);
5213
Filipe Manana6ca07092015-05-26 00:55:42 +01005214 /*
5215 * Keep looping until we have no more ranges in the io tree.
5216 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005217 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5218 * still in progress (unlocked the pages in the bio but did not yet
5219 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005220 * ranges can still be locked and eviction started because before
5221 * submitting those bios, which are executed by a separate task (work
5222 * queue kthread), inode references (inode->i_count) were not taken
5223 * (which would be dropped in the end io callback of each bio).
5224 * Therefore here we effectively end up waiting for those bios and
5225 * anyone else holding locked ranges without having bumped the inode's
5226 * reference count - if we don't do it, when they access the inode's
5227 * io_tree to unlock a range it may be too late, leading to an
5228 * use-after-free issue.
5229 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005230 spin_lock(&io_tree->lock);
5231 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5232 struct extent_state *state;
5233 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005234 u64 start;
5235 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005236 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005237
5238 node = rb_first(&io_tree->state);
5239 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005240 start = state->start;
5241 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005242 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005243 spin_unlock(&io_tree->lock);
5244
David Sterbaff13db42015-12-03 14:30:40 +01005245 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005246
5247 /*
5248 * If still has DELALLOC flag, the extent didn't reach disk,
5249 * and its reserved space won't be freed by delayed_ref.
5250 * So we need to free its reserved space here.
5251 * (Refer to comment in btrfs_invalidatepage, case 2)
5252 *
5253 * Note, end is the bytenr of last byte, so we need + 1 here.
5254 */
Filipe Manana421f0922018-10-12 13:02:48 +01005255 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005256 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005257
Filipe Manana6ca07092015-05-26 00:55:42 +01005258 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005259 EXTENT_LOCKED | EXTENT_DIRTY |
5260 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005261 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005262
Filipe Manana7064dd52014-08-08 02:47:05 +01005263 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005264 spin_lock(&io_tree->lock);
5265 }
5266 spin_unlock(&io_tree->lock);
5267}
5268
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005269static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005270 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005271{
5272 struct btrfs_fs_info *fs_info = root->fs_info;
5273 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5274 int failures = 0;
5275
5276 for (;;) {
5277 struct btrfs_trans_handle *trans;
5278 int ret;
5279
Josef Bacikad80cf52018-09-28 07:18:19 -04005280 ret = btrfs_block_rsv_refill(root, rsv, rsv->size,
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005281 BTRFS_RESERVE_FLUSH_LIMIT);
5282
5283 if (ret && ++failures > 2) {
5284 btrfs_warn(fs_info,
5285 "could not allocate space for a delete; will truncate on mount");
5286 return ERR_PTR(-ENOSPC);
5287 }
5288
5289 trans = btrfs_join_transaction(root);
5290 if (IS_ERR(trans) || !ret)
5291 return trans;
5292
5293 /*
5294 * Try to steal from the global reserve if there is space for
5295 * it.
5296 */
Josef Bacik644036122018-12-03 10:20:36 -05005297 if (!btrfs_check_space_for_delayed_refs(fs_info) &&
5298 !btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0))
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005299 return trans;
5300
5301 /* If not, commit and try again. */
5302 ret = btrfs_commit_transaction(trans);
5303 if (ret)
5304 return ERR_PTR(ret);
5305 }
5306}
5307
Al Virobd555972010-06-07 11:35:40 -04005308void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005309{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005310 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005311 struct btrfs_trans_handle *trans;
5312 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005313 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005314 int ret;
5315
liubo1abe9b82011-03-24 11:18:59 +00005316 trace_btrfs_inode_evict(inode);
5317
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005318 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005319 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005320 return;
5321 }
5322
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005323 evict_inode_truncate_pages(inode);
5324
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005325 if (inode->i_nlink &&
5326 ((btrfs_root_refs(&root->root_item) != 0 &&
5327 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005328 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005329 goto no_delete;
5330
Omar Sandoval27919062018-05-11 13:13:37 -07005331 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005332 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005333
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005334 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005335
Omar Sandoval7b40b692018-05-11 13:13:33 -07005336 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005337 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005338
Yan, Zheng76dda932009-09-21 16:00:26 -04005339 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005340 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5341 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005342 goto no_delete;
5343 }
5344
Nikolay Borisovaa790212017-01-10 20:35:40 +02005345 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005346 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005347 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005348
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005349 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005350 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005351 goto no_delete;
Josef Bacikad80cf52018-09-28 07:18:19 -04005352 rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005353 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005354
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005355 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005356
Yan, Zheng80825102009-11-12 09:35:36 +00005357 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005358 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005359 if (IS_ERR(trans))
5360 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005361
5362 trans->block_rsv = rsv;
5363
Yan, Zhengd68fc572010-05-16 10:49:58 -04005364 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005365 trans->block_rsv = &fs_info->trans_block_rsv;
5366 btrfs_end_transaction(trans);
5367 btrfs_btree_balance_dirty(fs_info);
5368 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5369 goto free_rsv;
5370 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005371 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005372 }
5373
Josef Bacik4ef31a42013-08-13 14:10:08 -04005374 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005375 * Errors here aren't a big deal, it just means we leave orphan items in
5376 * the tree. They will be cleaned up on the next mount. If the inode
5377 * number gets reused, cleanup deletes the orphan item without doing
5378 * anything, and unlink reuses the existing orphan item.
5379 *
5380 * If it turns out that we are dropping too many of these, we might want
5381 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005382 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005383 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005384 if (!IS_ERR(trans)) {
5385 trans->block_rsv = rsv;
5386 btrfs_orphan_del(trans, BTRFS_I(inode));
5387 trans->block_rsv = &fs_info->trans_block_rsv;
5388 btrfs_end_transaction(trans);
5389 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005390
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005391 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005392 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005393 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005394
Omar Sandoval27919062018-05-11 13:13:37 -07005395free_rsv:
5396 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005397no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005398 /*
5399 * If we didn't successfully delete, the orphan item will still be in
5400 * the tree and we'll retry on the next mount. Again, we might also want
5401 * to retry these periodically in the future.
5402 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005403 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005404 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005405}
5406
5407/*
5408 * this returns the key found in the dir entry in the location pointer.
Su Yue005d6712018-03-05 17:13:37 +08005409 * If no dir entries were found, returns -ENOENT.
5410 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005411 */
5412static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5413 struct btrfs_key *location)
5414{
5415 const char *name = dentry->d_name.name;
5416 int namelen = dentry->d_name.len;
5417 struct btrfs_dir_item *di;
5418 struct btrfs_path *path;
5419 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005420 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005421
5422 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005423 if (!path)
5424 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005425
David Sterbaf85b7372017-01-20 14:54:07 +01005426 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5427 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005428 if (IS_ERR_OR_NULL(di)) {
5429 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005430 goto out;
5431 }
Chris Masond3977122009-01-05 21:25:51 -05005432
Chris Mason5f39d392007-10-15 16:14:19 -04005433 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005434 if (location->type != BTRFS_INODE_ITEM_KEY &&
5435 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005436 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005437 btrfs_warn(root->fs_info,
5438"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5439 __func__, name, btrfs_ino(BTRFS_I(dir)),
5440 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005441 }
Chris Mason39279cc2007-06-12 06:35:45 -04005442out:
Chris Mason39279cc2007-06-12 06:35:45 -04005443 btrfs_free_path(path);
5444 return ret;
5445}
5446
5447/*
5448 * when we hit a tree root in a directory, the btrfs part of the inode
5449 * needs to be changed to reflect the root directory of the tree root. This
5450 * is kind of like crossing a mount point.
5451 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005452static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005453 struct inode *dir,
5454 struct dentry *dentry,
5455 struct btrfs_key *location,
5456 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005457{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005458 struct btrfs_path *path;
5459 struct btrfs_root *new_root;
5460 struct btrfs_root_ref *ref;
5461 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005462 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005463 int ret;
5464 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005465
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005466 path = btrfs_alloc_path();
5467 if (!path) {
5468 err = -ENOMEM;
5469 goto out;
5470 }
Chris Mason39279cc2007-06-12 06:35:45 -04005471
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005472 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005473 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5474 key.type = BTRFS_ROOT_REF_KEY;
5475 key.offset = location->objectid;
5476
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005477 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005478 if (ret) {
5479 if (ret < 0)
5480 err = ret;
5481 goto out;
5482 }
Chris Mason39279cc2007-06-12 06:35:45 -04005483
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005484 leaf = path->nodes[0];
5485 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005486 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005487 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5488 goto out;
5489
5490 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5491 (unsigned long)(ref + 1),
5492 dentry->d_name.len);
5493 if (ret)
5494 goto out;
5495
David Sterbab3b4aa72011-04-21 01:20:15 +02005496 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005497
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005498 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005499 if (IS_ERR(new_root)) {
5500 err = PTR_ERR(new_root);
5501 goto out;
5502 }
5503
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005504 *sub_root = new_root;
5505 location->objectid = btrfs_root_dirid(&new_root->root_item);
5506 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005507 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005508 err = 0;
5509out:
5510 btrfs_free_path(path);
5511 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005512}
5513
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005514static void inode_tree_add(struct inode *inode)
5515{
5516 struct btrfs_root *root = BTRFS_I(inode)->root;
5517 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005518 struct rb_node **p;
5519 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005520 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005521 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005522
Al Viro1d3382cb2010-10-23 15:19:20 -04005523 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005524 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005525 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005526 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005527 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005528 while (*p) {
5529 parent = *p;
5530 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5531
David Sterba37508512018-06-29 10:56:40 +02005532 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005533 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005534 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005535 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005536 else {
5537 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005538 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005539 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005540 RB_CLEAR_NODE(parent);
5541 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005542 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005543 }
5544 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005545 rb_link_node(new, parent, p);
5546 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005547 spin_unlock(&root->inode_lock);
5548}
5549
5550static void inode_tree_del(struct inode *inode)
5551{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005552 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005553 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005554 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005555
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005556 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005557 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005558 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005560 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005562 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005563
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005564 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005565 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005566 spin_lock(&root->inode_lock);
5567 empty = RB_EMPTY_ROOT(&root->inode_tree);
5568 spin_unlock(&root->inode_lock);
5569 if (empty)
5570 btrfs_add_dead_root(root);
5571 }
5572}
5573
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005574
Chris Masone02119d2008-09-05 16:13:11 -04005575static int btrfs_init_locked_inode(struct inode *inode, void *p)
5576{
5577 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005578 inode->i_ino = args->location->objectid;
5579 memcpy(&BTRFS_I(inode)->location, args->location,
5580 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005581 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005582 return 0;
5583}
5584
5585static int btrfs_find_actor(struct inode *inode, void *opaque)
5586{
5587 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005588 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005589 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005590}
5591
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005592static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005593 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005594 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005595{
5596 struct inode *inode;
5597 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005598 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005599
Chris Mason90d3e592014-01-09 17:28:00 -08005600 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005601 args.root = root;
5602
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005603 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005604 btrfs_init_locked_inode,
5605 (void *)&args);
5606 return inode;
5607}
5608
Balaji Rao1a54ef82008-07-21 02:01:04 +05305609/* Get an inode object given its location and corresponding root.
5610 * Returns in *is_new if the inode was read from disk
5611 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005612struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5613 struct btrfs_root *root, int *new,
5614 struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305615{
5616 struct inode *inode;
5617
Chris Mason90d3e592014-01-09 17:28:00 -08005618 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305619 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005620 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305621
5622 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005623 int ret;
5624
Filipe Manana4222ea72018-10-24 10:13:03 +01005625 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005626 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005627 inode_tree_add(inode);
5628 unlock_new_inode(inode);
5629 if (new)
5630 *new = 1;
5631 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005632 iget_failed(inode);
5633 /*
5634 * ret > 0 can come from btrfs_search_slot called by
5635 * btrfs_read_locked_inode, this means the inode item
5636 * was not found.
5637 */
5638 if (ret > 0)
5639 ret = -ENOENT;
5640 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005641 }
5642 }
5643
Balaji Rao1a54ef82008-07-21 02:01:04 +05305644 return inode;
5645}
5646
Filipe Manana4222ea72018-10-24 10:13:03 +01005647struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5648 struct btrfs_root *root, int *new)
5649{
5650 return btrfs_iget_path(s, location, root, new, NULL);
5651}
5652
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005653static struct inode *new_simple_dir(struct super_block *s,
5654 struct btrfs_key *key,
5655 struct btrfs_root *root)
5656{
5657 struct inode *inode = new_inode(s);
5658
5659 if (!inode)
5660 return ERR_PTR(-ENOMEM);
5661
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005662 BTRFS_I(inode)->root = root;
5663 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005664 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005665
5666 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005667 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005668 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005669 inode->i_fop = &simple_dir_operations;
5670 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005671 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305672 inode->i_atime = inode->i_mtime;
5673 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005674 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005675
5676 return inode;
5677}
5678
Chris Mason3de45862008-11-17 21:02:50 -05005679struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005680{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005681 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005682 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005683 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005684 struct btrfs_root *sub_root = root;
5685 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005686 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005687 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005688
5689 if (dentry->d_name.len > BTRFS_NAME_LEN)
5690 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005691
Jeff Layton39e3c952012-11-28 11:30:53 -05005692 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005693 if (ret < 0)
5694 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005695
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005696 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005697 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005698 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005699 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005700
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005701 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005702 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005703 &location, &sub_root);
5704 if (ret < 0) {
5705 if (ret != -ENOENT)
5706 inode = ERR_PTR(ret);
5707 else
5708 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5709 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005710 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005711 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005712 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005713
Julia Lawall34d19ba2011-01-24 19:55:19 +00005714 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005715 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005716 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005717 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005718 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005719 if (ret) {
5720 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005721 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005722 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005723 }
5724
Chris Mason3de45862008-11-17 21:02:50 -05005725 return inode;
5726}
5727
Nick Pigginfe15ce42011-01-07 17:49:23 +11005728static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005729{
5730 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005731 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005732
Li Zefan848cce02012-02-21 17:04:28 +08005733 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005734 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005735
Li Zefan848cce02012-02-21 17:04:28 +08005736 if (inode) {
5737 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005738 if (btrfs_root_refs(&root->root_item) == 0)
5739 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005740
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005741 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005742 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005743 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005744 return 0;
5745}
5746
Chris Mason3de45862008-11-17 21:02:50 -05005747static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005748 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005749{
Al Viro3837d202018-10-10 16:38:27 -04005750 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005751
Al Viro3837d202018-10-10 16:38:27 -04005752 if (inode == ERR_PTR(-ENOENT))
5753 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005754 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005755}
5756
Miao Xie16cdcec2011-04-22 18:12:22 +08005757unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005758 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5759};
5760
Josef Bacik23b5ec72017-07-24 15:14:25 -04005761/*
5762 * All this infrastructure exists because dir_emit can fault, and we are holding
5763 * the tree lock when doing readdir. For now just allocate a buffer and copy
5764 * our information into that, and then dir_emit from the buffer. This is
5765 * similar to what NFS does, only we don't keep the buffer around in pagecache
5766 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5767 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5768 * tree lock.
5769 */
5770static int btrfs_opendir(struct inode *inode, struct file *file)
5771{
5772 struct btrfs_file_private *private;
5773
5774 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5775 if (!private)
5776 return -ENOMEM;
5777 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5778 if (!private->filldir_buf) {
5779 kfree(private);
5780 return -ENOMEM;
5781 }
5782 file->private_data = private;
5783 return 0;
5784}
5785
5786struct dir_entry {
5787 u64 ino;
5788 u64 offset;
5789 unsigned type;
5790 int name_len;
5791};
5792
5793static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5794{
5795 while (entries--) {
5796 struct dir_entry *entry = addr;
5797 char *name = (char *)(entry + 1);
5798
David Sterba92d32172018-04-16 21:10:14 +02005799 ctx->pos = get_unaligned(&entry->offset);
5800 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5801 get_unaligned(&entry->ino),
5802 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005803 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005804 addr += sizeof(struct dir_entry) +
5805 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005806 ctx->pos++;
5807 }
5808 return 0;
5809}
5810
Al Viro9cdda8d2013-05-22 16:48:09 -04005811static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005812{
Al Viro9cdda8d2013-05-22 16:48:09 -04005813 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005814 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005815 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005816 struct btrfs_dir_item *di;
5817 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005818 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005819 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005820 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005821 struct list_head ins_list;
5822 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005823 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005824 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005825 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005826 char *name_ptr;
5827 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005828 int entries = 0;
5829 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005830 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005831 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005832
Al Viro9cdda8d2013-05-22 16:48:09 -04005833 if (!dir_emit_dots(file, ctx))
5834 return 0;
5835
David Woodhouse49593bf2008-08-17 17:08:36 +01005836 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005837 if (!path)
5838 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005839
Josef Bacik23b5ec72017-07-24 15:14:25 -04005840 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005841 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005842
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005843 INIT_LIST_HEAD(&ins_list);
5844 INIT_LIST_HEAD(&del_list);
5845 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005846
Josef Bacik23b5ec72017-07-24 15:14:25 -04005847again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005848 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005849 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005850 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005851
Chris Mason39279cc2007-06-12 06:35:45 -04005852 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5853 if (ret < 0)
5854 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005855
5856 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005857 struct dir_entry *entry;
5858
Chris Mason5f39d392007-10-15 16:14:19 -04005859 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005860 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005861 if (slot >= btrfs_header_nritems(leaf)) {
5862 ret = btrfs_next_leaf(root, path);
5863 if (ret < 0)
5864 goto err;
5865 else if (ret > 0)
5866 break;
5867 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005868 }
Chris Mason3de45862008-11-17 21:02:50 -05005869
Chris Mason5f39d392007-10-15 16:14:19 -04005870 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5871
5872 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005873 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005874 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005875 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005876 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005877 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005878 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005879 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005880 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005881 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005882 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5883 PAGE_SIZE) {
5884 btrfs_release_path(path);
5885 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5886 if (ret)
5887 goto nopos;
5888 addr = private->filldir_buf;
5889 entries = 0;
5890 total_len = 0;
5891 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005892 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005893
5894 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005895 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005896 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005897 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5898 name_len);
David Sterba92d32172018-04-16 21:10:14 +02005899 put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
5900 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005901 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005902 put_unaligned(location.objectid, &entry->ino);
5903 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005904 entries++;
5905 addr += sizeof(struct dir_entry) + name_len;
5906 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005907next:
5908 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005909 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005910 btrfs_release_path(path);
5911
5912 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5913 if (ret)
5914 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005915
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005916 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005917 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005918 goto nopos;
5919
Zach Browndb62efb2013-07-11 16:19:42 -07005920 /*
5921 * Stop new entries from being returned after we return the last
5922 * entry.
5923 *
5924 * New directory entries are assigned a strictly increasing
5925 * offset. This means that new entries created during readdir
5926 * are *guaranteed* to be seen in the future by that readdir.
5927 * This has broken buggy programs which operate on names as
5928 * they're returned by readdir. Until we re-use freed offsets
5929 * we have this hack to stop new entries from being returned
5930 * under the assumption that they'll never reach this huge
5931 * offset.
5932 *
5933 * This is being careful not to overflow 32bit loff_t unless the
5934 * last entry requires it because doing so has broken 32bit apps
5935 * in the past.
5936 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005937 if (ctx->pos >= INT_MAX)
5938 ctx->pos = LLONG_MAX;
5939 else
5940 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005941nopos:
5942 ret = 0;
5943err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005944 if (put)
5945 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005946 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005947 return ret;
5948}
5949
Chris Mason39279cc2007-06-12 06:35:45 -04005950/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005951 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005952 * inode changes. But, it is most likely to find the inode in cache.
5953 * FIXME, needs more benchmarking...there are no reasons other than performance
5954 * to keep or drop this code.
5955 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005956static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005957{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005959 struct btrfs_root *root = BTRFS_I(inode)->root;
5960 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005961 int ret;
5962
Josef Bacik72ac3c02012-05-23 14:13:11 -04005963 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005964 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005965
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005966 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005967 if (IS_ERR(trans))
5968 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005969
5970 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005971 if (ret && ret == -ENOSPC) {
5972 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005973 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005974 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005975 if (IS_ERR(trans))
5976 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005977
Chris Mason94b60442010-05-26 11:02:00 -04005978 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005979 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005980 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005981 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005982 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005983
5984 return ret;
5985}
5986
5987/*
5988 * This is a copy of file_update_time. We need this so we can return error on
5989 * ENOSPC for updating the inode in the case of file write and mmap writes.
5990 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005991static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005992 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005993{
Alexander Block2bc5565282012-06-15 09:49:33 +02005994 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005995 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005996
5997 if (btrfs_root_readonly(root))
5998 return -EROFS;
5999
Josef Bacike41f9412012-03-26 09:46:47 -04006000 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006001 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006002 if (flags & S_CTIME)
6003 inode->i_ctime = *now;
6004 if (flags & S_MTIME)
6005 inode->i_mtime = *now;
6006 if (flags & S_ATIME)
6007 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006008 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006009}
6010
Chris Masond352ac62008-09-29 15:18:18 -04006011/*
6012 * find the highest existing sequence number in a directory
6013 * and then set the in-memory index_cnt variable to reflect
6014 * free sequence numbers
6015 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006016static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006017{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006018 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006019 struct btrfs_key key, found_key;
6020 struct btrfs_path *path;
6021 struct extent_buffer *leaf;
6022 int ret;
6023
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006024 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006025 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006026 key.offset = (u64)-1;
6027
6028 path = btrfs_alloc_path();
6029 if (!path)
6030 return -ENOMEM;
6031
6032 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6033 if (ret < 0)
6034 goto out;
6035 /* FIXME: we should be able to handle this */
6036 if (ret == 0)
6037 goto out;
6038 ret = 0;
6039
6040 /*
6041 * MAGIC NUMBER EXPLANATION:
6042 * since we search a directory based on f_pos we have to start at 2
6043 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6044 * else has to start at 2
6045 */
6046 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006047 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006048 goto out;
6049 }
6050
6051 path->slots[0]--;
6052
6053 leaf = path->nodes[0];
6054 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6055
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006056 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006057 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006058 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006059 goto out;
6060 }
6061
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006062 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006063out:
6064 btrfs_free_path(path);
6065 return ret;
6066}
6067
Chris Masond352ac62008-09-29 15:18:18 -04006068/*
6069 * helper to find a free sequence number in a given directory. This current
6070 * code is very simple, later versions will do smarter things in the btree
6071 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006072int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006073{
6074 int ret = 0;
6075
Nikolay Borisov877574e2017-02-20 13:50:33 +02006076 if (dir->index_cnt == (u64)-1) {
6077 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006078 if (ret) {
6079 ret = btrfs_set_inode_index_count(dir);
6080 if (ret)
6081 return ret;
6082 }
Josef Bacikaec74772008-07-24 12:12:38 -04006083 }
6084
Nikolay Borisov877574e2017-02-20 13:50:33 +02006085 *index = dir->index_cnt;
6086 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006087
6088 return ret;
6089}
6090
Chris Masonb0d5d102014-09-08 13:08:51 -07006091static int btrfs_insert_inode_locked(struct inode *inode)
6092{
6093 struct btrfs_iget_args args;
6094 args.location = &BTRFS_I(inode)->location;
6095 args.root = BTRFS_I(inode)->root;
6096
6097 return insert_inode_locked4(inode,
6098 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6099 btrfs_find_actor, &args);
6100}
6101
Anand Jain19aee8d2017-07-18 17:37:05 +08006102/*
6103 * Inherit flags from the parent inode.
6104 *
6105 * Currently only the compression flags and the cow flags are inherited.
6106 */
6107static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6108{
6109 unsigned int flags;
6110
6111 if (!dir)
6112 return;
6113
6114 flags = BTRFS_I(dir)->flags;
6115
6116 if (flags & BTRFS_INODE_NOCOMPRESS) {
6117 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6118 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6119 } else if (flags & BTRFS_INODE_COMPRESS) {
6120 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6121 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6122 }
6123
6124 if (flags & BTRFS_INODE_NODATACOW) {
6125 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6126 if (S_ISREG(inode->i_mode))
6127 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6128 }
6129
David Sterba7b6a2212018-03-26 18:40:21 +02006130 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006131}
6132
Chris Mason39279cc2007-06-12 06:35:45 -04006133static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6134 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006135 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006136 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006137 u64 ref_objectid, u64 objectid,
6138 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006139{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006140 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006141 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006142 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006143 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006144 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006145 struct btrfs_inode_ref *ref;
6146 struct btrfs_key key[2];
6147 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006148 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006149 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006150 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006151
Chris Mason5f39d392007-10-15 16:14:19 -04006152 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006153 if (!path)
6154 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006155
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006156 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006157 if (!inode) {
6158 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006159 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006160 }
Chris Mason39279cc2007-06-12 06:35:45 -04006161
Li Zefan581bb052011-04-20 10:06:11 +08006162 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006163 * O_TMPFILE, set link count to 0, so that after this point,
6164 * we fill in an inode item with the correct link count.
6165 */
6166 if (!name)
6167 set_nlink(inode, 0);
6168
6169 /*
Li Zefan581bb052011-04-20 10:06:11 +08006170 * we have to initialize this early, so we can reclaim the inode
6171 * number if we fail afterwards in this function.
6172 */
6173 inode->i_ino = objectid;
6174
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006175 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006176 trace_btrfs_inode_request(dir);
6177
Nikolay Borisov877574e2017-02-20 13:50:33 +02006178 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006179 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006180 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006181 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006182 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006183 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006184 } else if (dir) {
6185 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006186 }
6187 /*
6188 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006189 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006190 * number
6191 */
6192 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006193 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006194 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006195 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006196 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006197
Josef Bacik5dc562c2012-08-17 13:14:17 -04006198 /*
6199 * We could have gotten an inode number from somebody who was fsynced
6200 * and then removed in this same transaction, so let's just set full
6201 * sync since it will be a full sync anyway and this will blow away the
6202 * old info in the log.
6203 */
6204 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6205
Chris Mason9c583092008-01-29 15:15:18 -05006206 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006207 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006208 key[0].offset = 0;
6209
Chris Mason9c583092008-01-29 15:15:18 -05006210 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006211
6212 if (name) {
6213 /*
6214 * Start new inodes with an inode_ref. This is slightly more
6215 * efficient for small numbers of hard links since they will
6216 * be packed into one item. Extended refs will kick in if we
6217 * add more hard links than can fit in the ref item.
6218 */
6219 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006220 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006221 key[1].offset = ref_objectid;
6222
6223 sizes[1] = name_len + sizeof(*ref);
6224 }
Chris Mason9c583092008-01-29 15:15:18 -05006225
Chris Masonb0d5d102014-09-08 13:08:51 -07006226 location = &BTRFS_I(inode)->location;
6227 location->objectid = objectid;
6228 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006229 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006230
6231 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006232 if (ret < 0) {
6233 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006234 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006235 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006236
Chris Masonb9473432009-03-13 11:00:37 -04006237 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006238 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006239 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006240 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006241
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006242 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006243 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306244
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006245 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306246 inode->i_atime = inode->i_mtime;
6247 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006248 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306249
Chris Mason5f39d392007-10-15 16:14:19 -04006250 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6251 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006252 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006253 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006254 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006255
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006256 if (name) {
6257 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6258 struct btrfs_inode_ref);
6259 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6260 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6261 ptr = (unsigned long)(ref + 1);
6262 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6263 }
Chris Mason9c583092008-01-29 15:15:18 -05006264
Chris Mason5f39d392007-10-15 16:14:19 -04006265 btrfs_mark_buffer_dirty(path->nodes[0]);
6266 btrfs_free_path(path);
6267
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006268 btrfs_inherit_iflags(inode, dir);
6269
Al Viro569254b2011-07-24 17:08:40 -04006270 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006271 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006272 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006273 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006274 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6275 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006276 }
6277
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006278 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006279
6280 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006281 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006282
Alexander Block8ea05e32012-07-25 17:35:53 +02006283 btrfs_update_root_times(trans, root);
6284
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006285 ret = btrfs_inode_inherit_props(trans, inode, dir);
6286 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006287 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006288 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006289 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006290
Chris Mason39279cc2007-06-12 06:35:45 -04006291 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006292
6293fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006294 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006295fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006296 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006297 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006298 btrfs_free_path(path);
6299 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006300}
6301
6302static inline u8 btrfs_inode_type(struct inode *inode)
6303{
6304 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6305}
6306
Chris Masond352ac62008-09-29 15:18:18 -04006307/*
6308 * utility function to add 'inode' into 'parent_inode' with
6309 * a give name and a given sequence number.
6310 * if 'add_backref' is true, also insert a backref from the
6311 * inode to the parent directory.
6312 */
Chris Masone02119d2008-09-05 16:13:11 -04006313int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006314 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006315 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006316{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006317 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006318 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006319 struct btrfs_root *root = parent_inode->root;
6320 u64 ino = btrfs_ino(inode);
6321 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006322
Li Zefan33345d012011-04-20 10:31:50 +08006323 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006324 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006325 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006326 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006327 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006328 key.offset = 0;
6329 }
Chris Mason39279cc2007-06-12 06:35:45 -04006330
Li Zefan33345d012011-04-20 10:31:50 +08006331 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006332 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006333 root->root_key.objectid, parent_ino,
6334 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006335 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006336 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6337 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006338 }
6339
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006340 /* Nothing to clean up yet */
6341 if (ret)
6342 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006343
Lu Fengqi684572d2018-08-04 21:10:57 +08006344 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006345 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006346 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006347 goto fail_dir_item;
6348 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006349 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006350 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006351 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006352
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006353 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006354 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006355 inode_inc_iversion(&parent_inode->vfs_inode);
6356 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6357 current_time(&parent_inode->vfs_inode);
6358 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006359 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006360 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006361 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006362
6363fail_dir_item:
6364 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6365 u64 local_index;
6366 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006367 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006368 root->root_key.objectid, parent_ino,
6369 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006370 if (err)
6371 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006372 } else if (add_backref) {
6373 u64 local_index;
6374 int err;
6375
6376 err = btrfs_del_inode_ref(trans, root, name, name_len,
6377 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006378 if (err)
6379 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006380 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006381
6382 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006383 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006384}
6385
6386static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006387 struct btrfs_inode *dir, struct dentry *dentry,
6388 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006389{
Josef Bacika1b075d2010-11-19 20:36:11 +00006390 int err = btrfs_add_link(trans, dir, inode,
6391 dentry->d_name.name, dentry->d_name.len,
6392 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006393 if (err > 0)
6394 err = -EEXIST;
6395 return err;
6396}
6397
Josef Bacik618e21d2007-07-11 10:18:17 -04006398static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006399 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006400{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006401 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006402 struct btrfs_trans_handle *trans;
6403 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006404 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006405 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006406 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006407 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006408
Josef Bacik9ed74f22009-09-11 16:12:44 -04006409 /*
6410 * 2 for inode item and ref
6411 * 2 for dir items
6412 * 1 for xattr if selinux is on
6413 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006414 trans = btrfs_start_transaction(root, 5);
6415 if (IS_ERR(trans))
6416 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006417
Li Zefan581bb052011-04-20 10:06:11 +08006418 err = btrfs_find_free_ino(root, &objectid);
6419 if (err)
6420 goto out_unlock;
6421
Josef Bacikaec74772008-07-24 12:12:38 -04006422 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006423 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6424 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006425 if (IS_ERR(inode)) {
6426 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006427 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006428 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006429 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006430
Casey Schauflerad19db72011-12-15 10:09:07 -05006431 /*
6432 * If the active LSM wants to access the inode during
6433 * d_instantiate it needs these. Smack checks to see
6434 * if the filesystem supports xattrs by looking at the
6435 * ops vector.
6436 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006437 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006438 init_special_inode(inode, inode->i_mode, rdev);
6439
6440 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006441 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006442 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006443
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006444 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6445 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006446 if (err)
6447 goto out_unlock;
6448
6449 btrfs_update_inode(trans, root, inode);
6450 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006451
Josef Bacik618e21d2007-07-11 10:18:17 -04006452out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006453 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006454 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006455 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006456 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006457 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006458 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006459 return err;
6460}
6461
Chris Mason39279cc2007-06-12 06:35:45 -04006462static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006463 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006464{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006465 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006466 struct btrfs_trans_handle *trans;
6467 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006468 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006469 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006470 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006471 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006472
Josef Bacik9ed74f22009-09-11 16:12:44 -04006473 /*
6474 * 2 for inode item and ref
6475 * 2 for dir items
6476 * 1 for xattr if selinux is on
6477 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006478 trans = btrfs_start_transaction(root, 5);
6479 if (IS_ERR(trans))
6480 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006481
Li Zefan581bb052011-04-20 10:06:11 +08006482 err = btrfs_find_free_ino(root, &objectid);
6483 if (err)
6484 goto out_unlock;
6485
Josef Bacikaec74772008-07-24 12:12:38 -04006486 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006487 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6488 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006489 if (IS_ERR(inode)) {
6490 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006491 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006492 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006493 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006494 /*
6495 * If the active LSM wants to access the inode during
6496 * d_instantiate it needs these. Smack checks to see
6497 * if the filesystem supports xattrs by looking at the
6498 * ops vector.
6499 */
6500 inode->i_fop = &btrfs_file_operations;
6501 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006502 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006503
6504 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6505 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006506 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006507
6508 err = btrfs_update_inode(trans, root, inode);
6509 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006510 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006511
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006512 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6513 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006514 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006515 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006516
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006517 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006518 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006519
Chris Mason39279cc2007-06-12 06:35:45 -04006520out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006521 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006522 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006523 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006524 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006525 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006526 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006527 return err;
6528}
6529
6530static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6531 struct dentry *dentry)
6532{
Filipe Manana271dba452016-01-05 16:24:05 +00006533 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006534 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006535 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006536 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006537 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006538 int err;
6539 int drop_inode = 0;
6540
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006541 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006542 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006543 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006544
Mark Fashehf1863732012-08-08 11:32:27 -07006545 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006546 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006547
Nikolay Borisov877574e2017-02-20 13:50:33 +02006548 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006549 if (err)
6550 goto fail;
6551
Yan, Zhenga22285a2010-05-16 10:48:46 -04006552 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006553 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006554 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006555 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006556 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006557 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006558 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006559 if (IS_ERR(trans)) {
6560 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006561 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006562 goto fail;
6563 }
Chris Mason5f39d392007-10-15 16:14:19 -04006564
Miao Xie67de1172013-12-26 13:07:06 +08006565 /* There are several dir indexes for this inode, clear the cache. */
6566 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006567 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006568 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006569 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006570 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006571 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006572
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006573 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6574 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006575
Yan, Zhenga5719522009-09-24 09:17:31 -04006576 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006577 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006578 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006579 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006580 int ret;
6581
Yan, Zhenga5719522009-09-24 09:17:31 -04006582 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006583 if (err)
6584 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006585 if (inode->i_nlink == 1) {
6586 /*
6587 * If new hard link count is 1, it's a file created
6588 * with open(2) O_TMPFILE flag.
6589 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006590 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006591 if (err)
6592 goto fail;
6593 }
Filipe Manana41bd6062018-11-28 14:54:28 +00006594 BTRFS_I(inode)->last_link_trans = trans->transid;
Al Viro08c422c2011-12-23 07:58:13 -05006595 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006596 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6597 true, NULL);
6598 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6599 err = btrfs_commit_transaction(trans);
6600 trans = NULL;
6601 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006602 }
Chris Mason39279cc2007-06-12 06:35:45 -04006603
Chris Mason1832a6d2007-12-21 16:27:21 -05006604fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006605 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006606 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006607 if (drop_inode) {
6608 inode_dec_link_count(inode);
6609 iput(inode);
6610 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006611 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006612 return err;
6613}
6614
Al Viro18bb1db2011-07-26 01:41:39 -04006615static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006616{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006617 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006618 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006619 struct btrfs_trans_handle *trans;
6620 struct btrfs_root *root = BTRFS_I(dir)->root;
6621 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006622 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006623 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006624
Josef Bacik9ed74f22009-09-11 16:12:44 -04006625 /*
6626 * 2 items for inode and ref
6627 * 2 items for dir items
6628 * 1 for xattr if selinux is on
6629 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006630 trans = btrfs_start_transaction(root, 5);
6631 if (IS_ERR(trans))
6632 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006633
Li Zefan581bb052011-04-20 10:06:11 +08006634 err = btrfs_find_free_ino(root, &objectid);
6635 if (err)
6636 goto out_fail;
6637
Josef Bacikaec74772008-07-24 12:12:38 -04006638 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006639 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6640 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006641 if (IS_ERR(inode)) {
6642 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006643 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006644 goto out_fail;
6645 }
Chris Mason5f39d392007-10-15 16:14:19 -04006646
Chris Masonb0d5d102014-09-08 13:08:51 -07006647 /* these must be set before we unlock the inode */
6648 inode->i_op = &btrfs_dir_inode_operations;
6649 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006650
Eric Paris2a7dba32011-02-01 11:05:39 -05006651 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006652 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006653 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006654
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006655 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006656 err = btrfs_update_inode(trans, root, inode);
6657 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006658 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006659
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006660 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6661 dentry->d_name.name,
6662 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006663 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006664 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006665
Al Viro1e2e5472018-05-04 08:23:01 -04006666 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006667
6668out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006669 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006670 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006671 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006672 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006673 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006674 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006675 return err;
6676}
6677
Chris Masonc8b97812008-10-29 14:49:59 -04006678static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006679 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006680 size_t pg_offset, u64 extent_offset,
6681 struct btrfs_file_extent_item *item)
6682{
6683 int ret;
6684 struct extent_buffer *leaf = path->nodes[0];
6685 char *tmp;
6686 size_t max_size;
6687 unsigned long inline_size;
6688 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006689 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006690
6691 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006692 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006693 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6694 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006695 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006696 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006697 if (!tmp)
6698 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006699 ptr = btrfs_file_extent_inline_start(item);
6700
6701 read_extent_buffer(leaf, tmp, ptr, inline_size);
6702
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006703 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006704 ret = btrfs_decompress(compress_type, tmp, page,
6705 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006706
6707 /*
6708 * decompression code contains a memset to fill in any space between the end
6709 * of the uncompressed data and the end of max_size in case the decompressed
6710 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6711 * the end of an inline extent and the beginning of the next block, so we
6712 * cover that region here.
6713 */
6714
6715 if (max_size + pg_offset < PAGE_SIZE) {
6716 char *map = kmap(page);
6717 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6718 kunmap(page);
6719 }
Chris Masonc8b97812008-10-29 14:49:59 -04006720 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006721 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006722}
6723
Chris Masond352ac62008-09-29 15:18:18 -04006724/*
6725 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006726 * the ugly parts come from merging extents from the disk with the in-ram
6727 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006728 * where the in-ram extents might be locked pending data=ordered completion.
6729 *
6730 * This also copies inline extents directly into the page.
6731 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006732struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Liu Bode2c6612018-08-25 13:47:59 +08006733 struct page *page,
6734 size_t pg_offset, u64 start, u64 len,
6735 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006736{
David Sterba3ffbd682018-06-29 10:56:42 +02006737 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006738 int ret;
6739 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006740 u64 extent_start = 0;
6741 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006742 u64 objectid = btrfs_ino(inode);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006743 u8 extent_type;
Chris Masonf4219502008-07-22 11:18:09 -04006744 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006745 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006746 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006747 struct extent_buffer *leaf;
6748 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006749 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006750 struct extent_map_tree *em_tree = &inode->extent_tree;
6751 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006752 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006753
Chris Mason890871b2009-09-02 16:24:52 -04006754 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006755 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006756 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006757 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006758 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006759
Chris Masona52d9a82007-08-27 16:49:44 -04006760 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006761 if (em->start > start || em->start + em->len <= start)
6762 free_extent_map(em);
6763 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006764 free_extent_map(em);
6765 else
6766 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006767 }
David Sterba172ddd62011-04-21 00:48:27 +02006768 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006769 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006770 err = -ENOMEM;
6771 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006772 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006773 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006774 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006775 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006776 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006777 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006778
Liu Bobee6ec82018-08-17 05:05:28 +08006779 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006780 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006781 err = -ENOMEM;
6782 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006783 }
6784
Liu Bobee6ec82018-08-17 05:05:28 +08006785 /* Chances are we'll be called again, so go ahead and do readahead */
6786 path->reada = READA_FORWARD;
6787
Liu Boe49aabd2018-08-25 13:47:09 +08006788 /*
6789 * Unless we're going to uncompress the inline extent, no sleep would
6790 * happen.
6791 */
6792 path->leave_spinning = 1;
6793
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006794 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006795 if (ret < 0) {
6796 err = ret;
6797 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006798 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006799 if (path->slots[0] == 0)
6800 goto not_found;
6801 path->slots[0]--;
6802 }
6803
Chris Mason5f39d392007-10-15 16:14:19 -04006804 leaf = path->nodes[0];
6805 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006806 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006807 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006808 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006809 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006810 /*
6811 * If we backup past the first extent we want to move forward
6812 * and see if there is an extent in front of us, otherwise we'll
6813 * say there is a hole for our whole search range which can
6814 * cause problems.
6815 */
6816 extent_end = start;
6817 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006818 }
6819
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006820 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006821 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006822 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6823 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006824 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006825 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006826
6827 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6828 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006829 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04006830 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006831
6832 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006833 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006834 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006835
6836 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6837 path->slots[0],
6838 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006839 }
Josef Bacik25a50342013-10-14 12:08:38 -04006840next:
Yan Zheng9036c102008-10-30 14:19:41 -04006841 if (start >= extent_end) {
6842 path->slots[0]++;
6843 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6844 ret = btrfs_next_leaf(root, path);
6845 if (ret < 0) {
6846 err = ret;
6847 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006848 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006849 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006850 }
Yan Zheng9036c102008-10-30 14:19:41 -04006851 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006852 }
Yan Zheng9036c102008-10-30 14:19:41 -04006853 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6854 if (found_key.objectid != objectid ||
6855 found_key.type != BTRFS_EXTENT_DATA_KEY)
6856 goto not_found;
6857 if (start + len <= found_key.offset)
6858 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006859 if (start > found_key.offset)
6860 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006861 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006862 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006863 em->len = found_key.offset - start;
6864 goto not_found_em;
6865 }
6866
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006867 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006868 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006869
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006870 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6871 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006872 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006873 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006874 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006875 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006876 size_t size;
6877 size_t extent_offset;
6878 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006879
Filipe Manana7ffbb592014-06-09 03:48:05 +01006880 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006881 goto out;
Yan689f9342007-10-29 11:41:07 -04006882
Qu Wenruoe41ca582018-06-06 15:41:49 +08006883 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006884 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006885 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6886 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006887 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006888 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006889 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006890 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006891 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006892
6893 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006894 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006895 if (btrfs_file_extent_compression(leaf, item) !=
6896 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006897 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006898 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006899 if (ret) {
6900 err = ret;
6901 goto out;
6902 }
Chris Masonc8b97812008-10-29 14:49:59 -04006903 } else {
6904 map = kmap(page);
6905 read_extent_buffer(leaf, map + pg_offset, ptr,
6906 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006907 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006908 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006909 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006910 copy_size);
6911 }
Chris Masonc8b97812008-10-29 14:49:59 -04006912 kunmap(page);
6913 }
Chris Mason179e29e2007-11-01 11:28:41 -04006914 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006915 }
Chris Masond1310b22008-01-24 16:13:08 -05006916 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006917 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006918 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006919 }
6920not_found:
6921 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006922 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006923 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006924not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006925 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006926insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006927 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006928 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006929 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006930 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6931 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006932 err = -EIO;
6933 goto out;
6934 }
Chris Masond1310b22008-01-24 16:13:08 -05006935
6936 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006937 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006938 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006939 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006940out:
Liu Boc6414282018-08-23 07:36:17 +08006941 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006942
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006943 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006944
Chris Masona52d9a82007-08-27 16:49:44 -04006945 if (err) {
6946 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006947 return ERR_PTR(err);
6948 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006949 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006950 return em;
6951}
6952
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006953struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006954 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006955{
6956 struct extent_map *em;
6957 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006958 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006959 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006960 u64 delalloc_len;
6961 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006962 int err = 0;
6963
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006964 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Chris Masonec29ed52011-02-23 16:23:20 -05006965 if (IS_ERR(em))
6966 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006967 /*
6968 * If our em maps to:
6969 * - a hole or
6970 * - a pre-alloc extent,
6971 * there might actually be delalloc bytes behind it.
6972 */
6973 if (em->block_start != EXTENT_MAP_HOLE &&
6974 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6975 return em;
6976 else
6977 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006978
6979 /* check to see if we've wrapped (len == -1 or similar) */
6980 end = start + len;
6981 if (end < start)
6982 end = (u64)-1;
6983 else
6984 end -= 1;
6985
6986 em = NULL;
6987
6988 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006989 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006990 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006991 delalloc_end = delalloc_start + delalloc_len;
6992 if (delalloc_end < delalloc_start)
6993 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006994
6995 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006996 * We didn't find anything useful, return the original results from
6997 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006998 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006999 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05007000 em = hole_em;
7001 hole_em = NULL;
7002 goto out;
7003 }
7004
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007005 /*
7006 * Adjust the delalloc_start to make sure it doesn't go backwards from
7007 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05007008 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007009 delalloc_start = max(start, delalloc_start);
7010 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007011
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007012 if (delalloc_len > 0) {
7013 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02007014 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007015 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05007016
David Sterba172ddd62011-04-21 00:48:27 +02007017 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007018 if (!em) {
7019 err = -ENOMEM;
7020 goto out;
7021 }
Chris Masonec29ed52011-02-23 16:23:20 -05007022 em->bdev = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007023
7024 ASSERT(hole_em);
7025 /*
7026 * When btrfs_get_extent can't find anything it returns one
7027 * huge hole
7028 *
7029 * Make sure what it found really fits our range, and adjust to
7030 * make sure it is based on the start from the caller
7031 */
7032 if (hole_end <= start || hole_em->start > end) {
7033 free_extent_map(hole_em);
7034 hole_em = NULL;
7035 } else {
7036 hole_start = max(hole_em->start, start);
7037 hole_len = hole_end - hole_start;
7038 }
7039
7040 if (hole_em && delalloc_start > hole_start) {
7041 /*
7042 * Our hole starts before our delalloc, so we have to
7043 * return just the parts of the hole that go until the
7044 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05007045 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007046 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007047 em->start = hole_start;
7048 em->orig_start = hole_start;
7049 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007050 * Don't adjust block start at all, it is fixed at
7051 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007052 */
7053 em->block_start = hole_em->block_start;
7054 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007055 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7056 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007057 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007058 /*
7059 * Hole is out of passed range or it starts after
7060 * delalloc range
7061 */
7062 em->start = delalloc_start;
7063 em->len = delalloc_len;
7064 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007065 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007066 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007067 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007068 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007069 return hole_em;
7070 }
7071out:
7072
7073 free_extent_map(hole_em);
7074 if (err) {
7075 free_extent_map(em);
7076 return ERR_PTR(err);
7077 }
7078 return em;
7079}
7080
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007081static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7082 const u64 start,
7083 const u64 len,
7084 const u64 orig_start,
7085 const u64 block_start,
7086 const u64 block_len,
7087 const u64 orig_block_len,
7088 const u64 ram_bytes,
7089 const int type)
7090{
7091 struct extent_map *em = NULL;
7092 int ret;
7093
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007094 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007095 em = create_io_em(inode, start, len, orig_start,
7096 block_start, block_len, orig_block_len,
7097 ram_bytes,
7098 BTRFS_COMPRESS_NONE, /* compress_type */
7099 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007100 if (IS_ERR(em))
7101 goto out;
7102 }
7103 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7104 len, block_len, type);
7105 if (ret) {
7106 if (em) {
7107 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007108 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007109 start + len - 1, 0);
7110 }
7111 em = ERR_PTR(ret);
7112 }
7113 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007114
7115 return em;
7116}
7117
Josef Bacik4b46fce2010-05-23 11:00:55 -04007118static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7119 u64 start, u64 len)
7120{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007121 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007122 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007123 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007124 struct btrfs_key ins;
7125 u64 alloc_hint;
7126 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007127
Josef Bacik4b46fce2010-05-23 11:00:55 -04007128 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007129 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007130 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007131 if (ret)
7132 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007133
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007134 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7135 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007136 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007137 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007138 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007139 btrfs_free_reserved_extent(fs_info, ins.objectid,
7140 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007141
Josef Bacik4b46fce2010-05-23 11:00:55 -04007142 return em;
7143}
7144
Chris Mason46bfbb52010-05-26 11:04:10 -04007145/*
7146 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7147 * block must be cow'd
7148 */
Josef Bacik00361582013-08-14 14:02:47 -04007149noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007150 u64 *orig_start, u64 *orig_block_len,
7151 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007152{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007153 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007154 struct btrfs_path *path;
7155 int ret;
7156 struct extent_buffer *leaf;
7157 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007158 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007159 struct btrfs_file_extent_item *fi;
7160 struct btrfs_key key;
7161 u64 disk_bytenr;
7162 u64 backref_offset;
7163 u64 extent_end;
7164 u64 num_bytes;
7165 int slot;
7166 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007167 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007168
Chris Mason46bfbb52010-05-26 11:04:10 -04007169 path = btrfs_alloc_path();
7170 if (!path)
7171 return -ENOMEM;
7172
David Sterbaf85b7372017-01-20 14:54:07 +01007173 ret = btrfs_lookup_file_extent(NULL, root, path,
7174 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007175 if (ret < 0)
7176 goto out;
7177
7178 slot = path->slots[0];
7179 if (ret == 1) {
7180 if (slot == 0) {
7181 /* can't find the item, must cow */
7182 ret = 0;
7183 goto out;
7184 }
7185 slot--;
7186 }
7187 ret = 0;
7188 leaf = path->nodes[0];
7189 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007190 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007191 key.type != BTRFS_EXTENT_DATA_KEY) {
7192 /* not our file or wrong item type, must cow */
7193 goto out;
7194 }
7195
7196 if (key.offset > offset) {
7197 /* Wrong offset, must cow */
7198 goto out;
7199 }
7200
7201 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7202 found_type = btrfs_file_extent_type(leaf, fi);
7203 if (found_type != BTRFS_FILE_EXTENT_REG &&
7204 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7205 /* not a regular extent, must cow */
7206 goto out;
7207 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007208
7209 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7210 goto out;
7211
Miao Xiee77751a2013-12-27 21:11:50 +08007212 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7213 if (extent_end <= offset)
7214 goto out;
7215
Chris Mason46bfbb52010-05-26 11:04:10 -04007216 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007217 if (disk_bytenr == 0)
7218 goto out;
7219
7220 if (btrfs_file_extent_compression(leaf, fi) ||
7221 btrfs_file_extent_encryption(leaf, fi) ||
7222 btrfs_file_extent_other_encoding(leaf, fi))
7223 goto out;
7224
Ethan Lien78d42952018-05-17 14:58:29 +08007225 /*
7226 * Do the same check as in btrfs_cross_ref_exist but without the
7227 * unnecessary search.
7228 */
7229 if (btrfs_file_extent_generation(leaf, fi) <=
7230 btrfs_root_last_snapshot(&root->root_item))
7231 goto out;
7232
Chris Mason46bfbb52010-05-26 11:04:10 -04007233 backref_offset = btrfs_file_extent_offset(leaf, fi);
7234
Josef Bacik7ee9e442013-06-21 16:37:03 -04007235 if (orig_start) {
7236 *orig_start = key.offset - backref_offset;
7237 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7238 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7239 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007240
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007241 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007242 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007243
7244 num_bytes = min(offset + *len, extent_end) - offset;
7245 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7246 u64 range_end;
7247
Jeff Mahoneyda170662016-06-15 09:22:56 -04007248 range_end = round_up(offset + num_bytes,
7249 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007250 ret = test_range_bit(io_tree, offset, range_end,
7251 EXTENT_DELALLOC, 0, NULL);
7252 if (ret) {
7253 ret = -EAGAIN;
7254 goto out;
7255 }
7256 }
7257
Josef Bacik1bda19e2013-10-18 12:10:36 -04007258 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007259
7260 /*
7261 * look for other files referencing this extent, if we
7262 * find any we must cow
7263 */
Josef Bacik00361582013-08-14 14:02:47 -04007264
Liu Boe4c3b2d2017-01-30 12:25:28 -08007265 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007266 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007267 if (ret) {
7268 ret = 0;
7269 goto out;
7270 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007271
7272 /*
7273 * adjust disk_bytenr and num_bytes to cover just the bytes
7274 * in this extent we are about to write. If there
7275 * are any csums in that range we have to cow in order
7276 * to keep the csums correct
7277 */
7278 disk_bytenr += backref_offset;
7279 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007280 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7281 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007282 /*
7283 * all of the above have passed, it is safe to overwrite this extent
7284 * without cow
7285 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007286 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007287 ret = 1;
7288out:
7289 btrfs_free_path(path);
7290 return ret;
7291}
7292
Josef Bacikeb838e72012-07-31 16:28:48 -04007293static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7294 struct extent_state **cached_state, int writing)
7295{
7296 struct btrfs_ordered_extent *ordered;
7297 int ret = 0;
7298
7299 while (1) {
7300 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007301 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007302 /*
7303 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007304 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007305 * extents in this range.
7306 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007307 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007308 lockend - lockstart + 1);
7309
7310 /*
7311 * We need to make sure there are no buffered pages in this
7312 * range either, we could have raced between the invalidate in
7313 * generic_file_direct_write and locking the extent. The
7314 * invalidate needs to happen so that reads after a write do not
7315 * get stale data.
7316 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007317 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007318 (!writing || !filemap_range_has_page(inode->i_mapping,
7319 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007320 break;
7321
7322 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007323 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007324
7325 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007326 /*
7327 * If we are doing a DIO read and the ordered extent we
7328 * found is for a buffered write, we can not wait for it
7329 * to complete and retry, because if we do so we can
7330 * deadlock with concurrent buffered writes on page
7331 * locks. This happens only if our DIO read covers more
7332 * than one extent map, if at this point has already
7333 * created an ordered extent for a previous extent map
7334 * and locked its range in the inode's io tree, and a
7335 * concurrent write against that previous extent map's
7336 * range and this range started (we unlock the ranges
7337 * in the io tree only when the bios complete and
7338 * buffered writes always lock pages before attempting
7339 * to lock range in the io tree).
7340 */
7341 if (writing ||
7342 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7343 btrfs_start_ordered_extent(inode, ordered, 1);
7344 else
7345 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007346 btrfs_put_ordered_extent(ordered);
7347 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007348 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007349 * We could trigger writeback for this range (and wait
7350 * for it to complete) and then invalidate the pages for
7351 * this range (through invalidate_inode_pages2_range()),
7352 * but that can lead us to a deadlock with a concurrent
7353 * call to readpages() (a buffered read or a defrag call
7354 * triggered a readahead) on a page lock due to an
7355 * ordered dio extent we created before but did not have
7356 * yet a corresponding bio submitted (whence it can not
7357 * complete), which makes readpages() wait for that
7358 * ordered extent to complete while holding a lock on
7359 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007360 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007361 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007362 }
7363
Filipe Mananaade77022016-02-18 14:28:55 +00007364 if (ret)
7365 break;
7366
Josef Bacikeb838e72012-07-31 16:28:48 -04007367 cond_resched();
7368 }
7369
7370 return ret;
7371}
7372
Liu Bo6f9994d2017-01-31 07:50:22 -08007373/* The callers of this must take lock_extent() */
7374static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7375 u64 orig_start, u64 block_start,
7376 u64 block_len, u64 orig_block_len,
7377 u64 ram_bytes, int compress_type,
7378 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007379{
7380 struct extent_map_tree *em_tree;
7381 struct extent_map *em;
7382 struct btrfs_root *root = BTRFS_I(inode)->root;
7383 int ret;
7384
Liu Bo6f9994d2017-01-31 07:50:22 -08007385 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7386 type == BTRFS_ORDERED_COMPRESSED ||
7387 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007388 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007389
Josef Bacik69ffb542012-09-11 15:40:07 -04007390 em_tree = &BTRFS_I(inode)->extent_tree;
7391 em = alloc_extent_map();
7392 if (!em)
7393 return ERR_PTR(-ENOMEM);
7394
7395 em->start = start;
7396 em->orig_start = orig_start;
7397 em->len = len;
7398 em->block_len = block_len;
7399 em->block_start = block_start;
7400 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007401 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007402 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007403 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007404 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007405 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007406 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007407 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007408 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7409 em->compress_type = compress_type;
7410 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007411
7412 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007413 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007414 em->start + em->len - 1, 0);
7415 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007416 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007417 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007418 /*
7419 * The caller has taken lock_extent(), who could race with us
7420 * to add em?
7421 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007422 } while (ret == -EEXIST);
7423
7424 if (ret) {
7425 free_extent_map(em);
7426 return ERR_PTR(ret);
7427 }
7428
Liu Bo6f9994d2017-01-31 07:50:22 -08007429 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007430 return em;
7431}
7432
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007433
7434static int btrfs_get_blocks_direct_read(struct extent_map *em,
7435 struct buffer_head *bh_result,
7436 struct inode *inode,
7437 u64 start, u64 len)
7438{
7439 if (em->block_start == EXTENT_MAP_HOLE ||
7440 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7441 return -ENOENT;
7442
7443 len = min(len, em->len - (start - em->start));
7444
7445 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7446 inode->i_blkbits;
7447 bh_result->b_size = len;
7448 bh_result->b_bdev = em->bdev;
7449 set_buffer_mapped(bh_result);
7450
7451 return 0;
7452}
7453
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007454static int btrfs_get_blocks_direct_write(struct extent_map **map,
7455 struct buffer_head *bh_result,
7456 struct inode *inode,
7457 struct btrfs_dio_data *dio_data,
7458 u64 start, u64 len)
7459{
7460 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7461 struct extent_map *em = *map;
7462 int ret = 0;
7463
7464 /*
7465 * We don't allocate a new extent in the following cases
7466 *
7467 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7468 * existing extent.
7469 * 2) The extent is marked as PREALLOC. We're good to go here and can
7470 * just use the extent.
7471 *
7472 */
7473 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7474 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7475 em->block_start != EXTENT_MAP_HOLE)) {
7476 int type;
7477 u64 block_start, orig_start, orig_block_len, ram_bytes;
7478
7479 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7480 type = BTRFS_ORDERED_PREALLOC;
7481 else
7482 type = BTRFS_ORDERED_NOCOW;
7483 len = min(len, em->len - (start - em->start));
7484 block_start = em->block_start + (start - em->start);
7485
7486 if (can_nocow_extent(inode, start, &len, &orig_start,
7487 &orig_block_len, &ram_bytes) == 1 &&
7488 btrfs_inc_nocow_writers(fs_info, block_start)) {
7489 struct extent_map *em2;
7490
7491 em2 = btrfs_create_dio_extent(inode, start, len,
7492 orig_start, block_start,
7493 len, orig_block_len,
7494 ram_bytes, type);
7495 btrfs_dec_nocow_writers(fs_info, block_start);
7496 if (type == BTRFS_ORDERED_PREALLOC) {
7497 free_extent_map(em);
7498 *map = em = em2;
7499 }
7500
7501 if (em2 && IS_ERR(em2)) {
7502 ret = PTR_ERR(em2);
7503 goto out;
7504 }
7505 /*
7506 * For inode marked NODATACOW or extent marked PREALLOC,
7507 * use the existing or preallocated extent, so does not
7508 * need to adjust btrfs_space_info's bytes_may_use.
7509 */
7510 btrfs_free_reserved_data_space_noquota(inode, start,
7511 len);
7512 goto skip_cow;
7513 }
7514 }
7515
7516 /* this will cow the extent */
7517 len = bh_result->b_size;
7518 free_extent_map(em);
7519 *map = em = btrfs_new_extent_direct(inode, start, len);
7520 if (IS_ERR(em)) {
7521 ret = PTR_ERR(em);
7522 goto out;
7523 }
7524
7525 len = min(len, em->len - (start - em->start));
7526
7527skip_cow:
7528 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7529 inode->i_blkbits;
7530 bh_result->b_size = len;
7531 bh_result->b_bdev = em->bdev;
7532 set_buffer_mapped(bh_result);
7533
7534 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7535 set_buffer_new(bh_result);
7536
7537 /*
7538 * Need to update the i_size under the extent lock so buffered
7539 * readers will get the updated i_size when we unlock.
7540 */
7541 if (!dio_data->overwrite && start + len > i_size_read(inode))
7542 i_size_write(inode, start + len);
7543
7544 WARN_ON(dio_data->reserve < len);
7545 dio_data->reserve -= len;
7546 dio_data->unsubmitted_oe_range_end = start + len;
7547 current->journal_info = dio_data;
7548out:
7549 return ret;
7550}
7551
Josef Bacik4b46fce2010-05-23 11:00:55 -04007552static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7553 struct buffer_head *bh_result, int create)
7554{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007555 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007556 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007557 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307558 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007559 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007560 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007561 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007562 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007563 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007564
Miao Xie172a5042013-02-21 02:48:22 -07007565 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007566 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007567 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007568 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007569
Josef Bacikc3298612012-08-03 16:49:19 -04007570 lockstart = start;
7571 lockend = start + len - 1;
7572
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007573 if (current->journal_info) {
7574 /*
7575 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007576 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007577 * confused.
7578 */
chandan50745b02015-08-28 21:10:13 +05307579 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007580 current->journal_info = NULL;
7581 }
7582
Josef Bacikeb838e72012-07-31 16:28:48 -04007583 /*
7584 * If this errors out it's because we couldn't invalidate pagecache for
7585 * this range and we need to fallback to buffered.
7586 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007587 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7588 create)) {
7589 ret = -ENOTBLK;
7590 goto err;
7591 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007592
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007593 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007594 if (IS_ERR(em)) {
7595 ret = PTR_ERR(em);
7596 goto unlock_err;
7597 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007598
7599 /*
7600 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7601 * io. INLINE is special, and we could probably kludge it in here, but
7602 * it's still buffered so for safety lets just fall back to the generic
7603 * buffered path.
7604 *
7605 * For COMPRESSED we _have_ to read the entire extent in so we can
7606 * decompress it, so there will be buffering required no matter what we
7607 * do, so go ahead and fallback to buffered.
7608 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007609 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007610 * to buffered IO. Don't blame me, this is the price we pay for using
7611 * the generic code.
7612 */
7613 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7614 em->block_start == EXTENT_MAP_INLINE) {
7615 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007616 ret = -ENOTBLK;
7617 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007618 }
7619
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007620 if (create) {
7621 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7622 dio_data, start, len);
7623 if (ret < 0)
7624 goto unlock_err;
7625
7626 /* clear and unlock the entire range */
7627 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7628 unlock_bits, 1, 0, &cached_state);
7629 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007630 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7631 start, len);
7632 /* Can be negative only if we read from a hole */
7633 if (ret < 0) {
7634 ret = 0;
7635 free_extent_map(em);
7636 goto unlock_err;
7637 }
7638 /*
7639 * We need to unlock only the end area that we aren't using.
7640 * The rest is going to be unlocked by the endio routine.
7641 */
7642 lockstart = start + bh_result->b_size;
7643 if (lockstart < lockend) {
7644 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7645 lockend, unlock_bits, 1, 0,
7646 &cached_state);
7647 } else {
7648 free_extent_state(cached_state);
7649 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007650 }
7651
Josef Bacik4b46fce2010-05-23 11:00:55 -04007652 free_extent_map(em);
7653
7654 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007655
7656unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007657 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007658 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007659err:
chandan50745b02015-08-28 21:10:13 +05307660 if (dio_data)
7661 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007662 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007663}
7664
Omar Sandoval58efbc92017-08-22 23:45:59 -07007665static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7666 struct bio *bio,
7667 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007668{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007669 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007670 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007671
Mike Christie37226b22016-06-05 14:31:52 -05007672 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007673
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007674 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007675 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007676 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007677
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007678 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007679
Miao Xie8b110e32014-09-12 18:44:03 +08007680 return ret;
7681}
7682
7683static int btrfs_check_dio_repairable(struct inode *inode,
7684 struct bio *failed_bio,
7685 struct io_failure_record *failrec,
7686 int failed_mirror)
7687{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007688 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007689 int num_copies;
7690
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007691 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007692 if (num_copies == 1) {
7693 /*
7694 * we only have a single copy of the data, so don't bother with
7695 * all the retry and error correction code that follows. no
7696 * matter what the error is, it is very likely to persist.
7697 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007698 btrfs_debug(fs_info,
7699 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7700 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007701 return 0;
7702 }
7703
7704 failrec->failed_mirror = failed_mirror;
7705 failrec->this_mirror++;
7706 if (failrec->this_mirror == failed_mirror)
7707 failrec->this_mirror++;
7708
7709 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007710 btrfs_debug(fs_info,
7711 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7712 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007713 return 0;
7714 }
7715
7716 return 1;
7717}
7718
Omar Sandoval58efbc92017-08-22 23:45:59 -07007719static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7720 struct page *page, unsigned int pgoff,
7721 u64 start, u64 end, int failed_mirror,
7722 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007723{
7724 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007725 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7726 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007727 struct bio *bio;
7728 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007729 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007730 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007731 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007732 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007733 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007734
Mike Christie37226b22016-06-05 14:31:52 -05007735 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007736
7737 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7738 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007739 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007740
7741 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7742 failed_mirror);
7743 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007744 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007745 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007746 }
7747
Liu Bo17347ce2017-05-15 15:33:27 -07007748 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007749 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007750 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007751 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007752 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007753
7754 isector = start - btrfs_io_bio(failed_bio)->logical;
7755 isector >>= inode->i_sb->s_blocksize_bits;
7756 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307757 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007758 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007759
7760 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007761 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007762 read_mode, failrec->this_mirror, failrec->in_validation);
7763
Omar Sandoval58efbc92017-08-22 23:45:59 -07007764 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7765 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007766 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007767 bio_put(bio);
7768 }
7769
Omar Sandoval58efbc92017-08-22 23:45:59 -07007770 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007771}
7772
7773struct btrfs_retry_complete {
7774 struct completion done;
7775 struct inode *inode;
7776 u64 start;
7777 int uptodate;
7778};
7779
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007780static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007781{
7782 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007783 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007784 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007785 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007786 int i;
7787
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007788 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007789 goto end;
7790
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307791 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007792 io_tree = &BTRFS_I(inode)->io_tree;
7793 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007794 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307795
Miao Xie8b110e32014-09-12 18:44:03 +08007796 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007797 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007798 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007799 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7800 io_tree, done->start, bvec->bv_page,
7801 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007802end:
7803 complete(&done->done);
7804 bio_put(bio);
7805}
7806
Omar Sandoval58efbc92017-08-22 23:45:59 -07007807static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7808 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007809{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307810 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007811 struct bio_vec bvec;
7812 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007813 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007814 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307815 unsigned int pgoff;
7816 u32 sectorsize;
7817 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007818 blk_status_t ret;
7819 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007820
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307821 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007822 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307823
Miao Xiec1dc0892014-09-12 18:43:56 +08007824 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007825 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007826 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007827
Liu Bo17347ce2017-05-15 15:33:27 -07007828 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7829 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7830 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307831
7832next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007833 done.uptodate = 0;
7834 done.start = start;
7835 init_completion(&done.done);
7836
Liu Bo17347ce2017-05-15 15:33:27 -07007837 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307838 pgoff, start, start + sectorsize - 1,
7839 io_bio->mirror_num,
7840 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007841 if (ret) {
7842 err = ret;
7843 goto next;
7844 }
Miao Xie8b110e32014-09-12 18:44:03 +08007845
David Sterba9c17f6c2017-07-19 19:26:45 +02007846 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007847
7848 if (!done.uptodate) {
7849 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307850 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007851 }
7852
Liu Bo629ebf42017-05-15 17:20:07 -07007853next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307854 start += sectorsize;
7855
Liu Bo97bf5a52017-04-07 13:11:10 -07007856 nr_sectors--;
7857 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307858 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007859 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307860 goto next_block_or_try_again;
7861 }
Miao Xie8b110e32014-09-12 18:44:03 +08007862 }
7863
Liu Bo629ebf42017-05-15 17:20:07 -07007864 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007865}
7866
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007867static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007868{
7869 struct btrfs_retry_complete *done = bio->bi_private;
7870 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007871 struct extent_io_tree *io_tree, *failure_tree;
7872 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007873 struct bio_vec *bvec;
7874 int uptodate;
7875 int ret;
7876 int i;
7877
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007878 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007879 goto end;
7880
7881 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307882
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307883 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007884 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307885
Josef Bacik7870d082017-05-05 11:57:15 -04007886 io_tree = &BTRFS_I(inode)->io_tree;
7887 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7888
David Sterbac09abff2017-07-13 18:10:07 +02007889 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007890 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04007891 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7892 bvec->bv_offset, done->start,
7893 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007894 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007895 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7896 failure_tree, io_tree, done->start,
7897 bvec->bv_page,
7898 btrfs_ino(BTRFS_I(inode)),
7899 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007900 else
7901 uptodate = 0;
7902 }
7903
7904 done->uptodate = uptodate;
7905end:
7906 complete(&done->done);
7907 bio_put(bio);
7908}
7909
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007910static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7911 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007912{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307913 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007914 struct bio_vec bvec;
7915 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007916 struct btrfs_retry_complete done;
7917 u64 start;
7918 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307919 u32 sectorsize;
7920 int nr_sectors;
7921 unsigned int pgoff;
7922 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07007923 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007924 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007925 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007926
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307927 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007928 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307929
Omar Sandoval58efbc92017-08-22 23:45:59 -07007930 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007931 start = io_bio->logical;
7932 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007933 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007934
Liu Bo17347ce2017-05-15 15:33:27 -07007935 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7936 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307937
Liu Bo17347ce2017-05-15 15:33:27 -07007938 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307939next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07007940 if (uptodate) {
7941 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
7942 ret = __readpage_endio_check(inode, io_bio, csum_pos,
7943 bvec.bv_page, pgoff, start, sectorsize);
7944 if (likely(!ret))
7945 goto next;
7946 }
Miao Xie8b110e32014-09-12 18:44:03 +08007947try_again:
7948 done.uptodate = 0;
7949 done.start = start;
7950 init_completion(&done.done);
7951
Omar Sandoval58efbc92017-08-22 23:45:59 -07007952 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
7953 pgoff, start, start + sectorsize - 1,
7954 io_bio->mirror_num, btrfs_retry_endio,
7955 &done);
7956 if (status) {
7957 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08007958 goto next;
7959 }
7960
David Sterba9c17f6c2017-07-19 19:26:45 +02007961 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007962
7963 if (!done.uptodate) {
7964 /* We might have another mirror, so try again */
7965 goto try_again;
7966 }
7967next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307968 offset += sectorsize;
7969 start += sectorsize;
7970
7971 ASSERT(nr_sectors);
7972
Liu Bo97bf5a52017-04-07 13:11:10 -07007973 nr_sectors--;
7974 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307975 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007976 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307977 goto next_block;
7978 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007979 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007980
7981 return err;
7982}
7983
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007984static blk_status_t btrfs_subio_endio_read(struct inode *inode,
7985 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007986{
7987 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7988
7989 if (skip_csum) {
7990 if (unlikely(err))
7991 return __btrfs_correct_data_nocsum(inode, io_bio);
7992 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07007993 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007994 } else {
7995 return __btrfs_subio_endio_read(inode, io_bio, err);
7996 }
7997}
7998
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007999static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008000{
8001 struct btrfs_dio_private *dip = bio->bi_private;
8002 struct inode *inode = dip->inode;
8003 struct bio *dio_bio;
8004 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008005 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008006
Liu Bo99c4e3b92017-09-15 15:06:51 -06008007 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008008 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008009
Josef Bacik4b46fce2010-05-23 11:00:55 -04008010 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008011 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008012 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008013
Josef Bacik4b46fce2010-05-23 11:00:55 -04008014 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008015
Liu Bo99c4e3b92017-09-15 15:06:51 -06008016 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008017 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01008018 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008019 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008020}
8021
Qu Wenruo524272602017-03-08 10:25:52 +08008022static void __endio_write_update_ordered(struct inode *inode,
8023 const u64 offset, const u64 bytes,
8024 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008025{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008026 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008027 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008028 struct btrfs_workqueue *wq;
8029 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008030 u64 ordered_offset = offset;
8031 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008032 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008033
Qu Wenruo524272602017-03-08 10:25:52 +08008034 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8035 wq = fs_info->endio_freespace_worker;
8036 func = btrfs_freespace_write_helper;
8037 } else {
8038 wq = fs_info->endio_write_workers;
8039 func = btrfs_endio_write_helper;
8040 }
8041
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008042 while (ordered_offset < offset + bytes) {
8043 last_offset = ordered_offset;
8044 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8045 &ordered_offset,
8046 ordered_bytes,
8047 uptodate)) {
8048 btrfs_init_work(&ordered->work, func,
8049 finish_ordered_fn,
8050 NULL, NULL);
8051 btrfs_queue_work(wq, &ordered->work);
8052 }
8053 /*
8054 * If btrfs_dec_test_ordered_pending does not find any ordered
8055 * extent in the range, we can exit.
8056 */
8057 if (ordered_offset == last_offset)
8058 return;
8059 /*
8060 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01008061 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03008062 */
8063 if (ordered_offset < offset + bytes) {
8064 ordered_bytes = offset + bytes - ordered_offset;
8065 ordered = NULL;
8066 }
Chris Mason163cf092010-11-28 19:56:33 -05008067 }
Filipe Manana14543772015-11-24 16:23:54 +00008068}
8069
8070static void btrfs_endio_direct_write(struct bio *bio)
8071{
8072 struct btrfs_dio_private *dip = bio->bi_private;
8073 struct bio *dio_bio = dip->dio_bio;
8074
Qu Wenruo524272602017-03-08 10:25:52 +08008075 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008076 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008077
Josef Bacik4b46fce2010-05-23 11:00:55 -04008078 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008079
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008080 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008081 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008082 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008083}
8084
David Sterbad0ee3932018-03-08 14:35:48 +01008085static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008086 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008087{
Josef Bacikc6100a42017-05-05 11:57:13 -04008088 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008089 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008090 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008091 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008092 return 0;
8093}
8094
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008095static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008096{
8097 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008098 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008099
Miao Xie8b110e32014-09-12 18:44:03 +08008100 if (err)
8101 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008102 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008103 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8104 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008105 (unsigned long long)bio->bi_iter.bi_sector,
8106 bio->bi_iter.bi_size, err);
8107
8108 if (dip->subio_endio)
8109 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008110
8111 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008112 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008113 * We want to perceive the errors flag being set before
8114 * decrementing the reference count. We don't need a barrier
8115 * since atomic operations with a return value are fully
8116 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008117 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008118 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008119 }
8120
8121 /* if there are more bios still pending for this dio, just exit */
8122 if (!atomic_dec_and_test(&dip->pending_bios))
8123 goto out;
8124
Chris Mason9be33952013-05-17 18:30:14 -04008125 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008126 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008127 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008128 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008129 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008130 }
8131out:
8132 bio_put(bio);
8133}
8134
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008135static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008136 struct btrfs_dio_private *dip,
8137 struct bio *bio,
8138 u64 file_offset)
8139{
8140 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8141 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008142 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008143
8144 /*
8145 * We load all the csum data we need when we submit
8146 * the first bio to reduce the csum tree search and
8147 * contention.
8148 */
8149 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008150 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008151 file_offset);
8152 if (ret)
8153 return ret;
8154 }
8155
8156 if (bio == dip->orig_bio)
8157 return 0;
8158
8159 file_offset -= dip->logical_offset;
8160 file_offset >>= inode->i_sb->s_blocksize_bits;
8161 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8162
8163 return 0;
8164}
8165
David Sterbad0ee3932018-03-08 14:35:48 +01008166static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8167 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008168{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008169 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008170 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008171 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008172 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008173
Liu Bo4c274bc2017-11-01 17:19:27 -06008174 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008175 if (async_submit)
8176 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8177
Josef Bacik5fd02042012-05-02 14:00:54 -04008178 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008179 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008180 if (ret)
8181 goto err;
8182 }
Miao Xiee65e1532010-11-22 03:04:43 +00008183
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008184 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008185 goto map;
8186
8187 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008188 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8189 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02008190 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00008191 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008192 } else if (write) {
8193 /*
8194 * If we aren't doing async submit, calculate the csum of the
8195 * bio now.
8196 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008197 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008198 if (ret)
8199 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008200 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008201 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008202 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008203 if (ret)
8204 goto err;
8205 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008206map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008207 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008208err:
Miao Xiee65e1532010-11-22 03:04:43 +00008209 return ret;
8210}
8211
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008212static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008213{
8214 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008215 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008216 struct bio *bio;
8217 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008218 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008219 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008220 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008221 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008222 u64 submit_len;
8223 int clone_offset = 0;
8224 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308225 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008226 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008227
Kent Overstreet4f024f32013-10-11 15:44:27 -07008228 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008229 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008230 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8231 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008232 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008233 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008234
Liu Bo725130b2017-05-16 09:51:39 -07008235 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008236 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008237 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008238 goto submit;
8239 }
8240
David Woodhouse53b381b2013-01-29 18:40:14 -05008241 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008242 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008243 async_submit = 0;
8244 else
8245 async_submit = 1;
8246
Liu Bo725130b2017-05-16 09:51:39 -07008247 /* bio split */
8248 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008249 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008250 do {
Liu Bo725130b2017-05-16 09:51:39 -07008251 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008252
Liu Bo725130b2017-05-16 09:51:39 -07008253 /*
8254 * This will never fail as it's passing GPF_NOFS and
8255 * the allocation is backed by btrfs_bioset.
8256 */
Liu Boe4770942017-05-16 10:57:14 -07008257 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008258 clone_len);
8259 bio->bi_private = dip;
8260 bio->bi_end_io = btrfs_end_dio_bio;
8261 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008262
Liu Bo725130b2017-05-16 09:51:39 -07008263 ASSERT(submit_len >= clone_len);
8264 submit_len -= clone_len;
8265 if (submit_len == 0)
8266 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008267
Liu Bo725130b2017-05-16 09:51:39 -07008268 /*
8269 * Increase the count before we submit the bio so we know
8270 * the end IO handler won't happen before we increase the
8271 * count. Otherwise, the dip might get freed before we're
8272 * done setting it up.
8273 */
8274 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008275
David Sterbad0ee3932018-03-08 14:35:48 +01008276 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008277 async_submit);
8278 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008279 bio_put(bio);
8280 atomic_dec(&dip->pending_bios);
8281 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008282 }
Liu Bo725130b2017-05-16 09:51:39 -07008283
8284 clone_offset += clone_len;
8285 start_sector += clone_len >> 9;
8286 file_offset += clone_len;
8287
8288 map_length = submit_len;
8289 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8290 start_sector << 9, &map_length, NULL, 0);
8291 if (ret)
8292 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008293 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008294
Josef Bacik02f57c72011-04-06 14:25:44 -04008295submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008296 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008297 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008298 return 0;
8299
8300 bio_put(bio);
8301out_err:
8302 dip->errors = 1;
8303 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008304 * Before atomic variable goto zero, we must make sure dip->errors is
8305 * perceived to be set. This ordering is ensured by the fact that an
8306 * atomic operations with a return value are fully ordered as per
8307 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008308 */
Miao Xiee65e1532010-11-22 03:04:43 +00008309 if (atomic_dec_and_test(&dip->pending_bios))
8310 bio_io_error(dip->orig_bio);
8311
8312 /* bio_end_io() will handle error, so we needn't return it */
8313 return 0;
8314}
8315
Mike Christie8a4c1e42016-06-05 14:31:50 -05008316static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8317 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008318{
Filipe Manana61de7182015-07-01 12:13:10 +01008319 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008320 struct bio *bio = NULL;
8321 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008322 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008323 int ret = 0;
8324
David Sterba8b6c1d52017-06-02 17:48:13 +02008325 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008326
Miao Xiec1dc0892014-09-12 18:43:56 +08008327 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008328 if (!dip) {
8329 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008330 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008331 }
8332
8333 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008334 dip->inode = inode;
8335 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008336 dip->bytes = dio_bio->bi_iter.bi_size;
8337 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008338 bio->bi_private = dip;
8339 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008340 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008341 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008342 io_bio = btrfs_io_bio(bio);
8343 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008344
Miao Xiec1dc0892014-09-12 18:43:56 +08008345 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008346 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008347 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008348 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008349 dip->subio_endio = btrfs_subio_endio_read;
8350 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008351
Filipe Mananaf28a4922015-12-08 19:23:20 +00008352 /*
8353 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8354 * even if we fail to submit a bio, because in such case we do the
8355 * corresponding error handling below and it must not be done a second
8356 * time by btrfs_direct_IO().
8357 */
8358 if (write) {
8359 struct btrfs_dio_data *dio_data = current->journal_info;
8360
8361 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8362 dip->bytes;
8363 dio_data->unsubmitted_oe_range_start =
8364 dio_data->unsubmitted_oe_range_end;
8365 }
8366
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008367 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008368 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008369 return;
Chris Mason9be33952013-05-17 18:30:14 -04008370
David Sterbab3a0dd52018-11-22 17:16:49 +01008371 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008372
Josef Bacik4b46fce2010-05-23 11:00:55 -04008373free_ordered:
8374 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008375 * If we arrived here it means either we failed to submit the dip
8376 * or we either failed to clone the dio_bio or failed to allocate the
8377 * dip. If we cloned the dio_bio and allocated the dip, we can just
8378 * call bio_endio against our io_bio so that we get proper resource
8379 * cleanup if we fail to submit the dip, otherwise, we must do the
8380 * same as btrfs_endio_direct_[write|read] because we can't call these
8381 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008382 */
Liu Bo3892ac92017-04-17 15:00:28 -07008383 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008384 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008385 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008386 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008387 * and all the cleanup and final put for dio_bio (through
8388 * dio_end_io()).
8389 */
8390 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008391 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008392 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008393 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008394 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008395 file_offset,
8396 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008397 false);
Filipe Manana14543772015-11-24 16:23:54 +00008398 else
Filipe Manana61de7182015-07-01 12:13:10 +01008399 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8400 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008401
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008402 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008403 /*
8404 * Releases and cleans up our dio_bio, no need to bio_put()
8405 * nor bio_endio()/bio_io_error() against dio_bio.
8406 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008407 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008408 }
Liu Bo3892ac92017-04-17 15:00:28 -07008409 if (bio)
8410 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008411 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008412}
8413
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008414static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008415 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008416{
8417 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008418 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008419 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008420 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008421
8422 if (offset & blocksize_mask)
8423 goto out;
8424
Al Viro28060d52014-03-22 05:15:17 -04008425 if (iov_iter_alignment(iter) & blocksize_mask)
8426 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008427
Al Viro28060d52014-03-22 05:15:17 -04008428 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008429 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008430 return 0;
8431 /*
8432 * Check to make sure we don't have duplicate iov_base's in this
8433 * iovec, if so return EINVAL, otherwise we'll get csum errors
8434 * when reading back.
8435 */
8436 for (seg = 0; seg < iter->nr_segs; seg++) {
8437 for (i = seg + 1; i < iter->nr_segs; i++) {
8438 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008439 goto out;
8440 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008441 }
8442 retval = 0;
8443out:
8444 return retval;
8445}
Josef Bacikeb838e72012-07-31 16:28:48 -04008446
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008447static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008448{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008449 struct file *file = iocb->ki_filp;
8450 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008451 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308452 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008453 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008454 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008455 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008456 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008457 bool wakeup = true;
8458 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008459 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008460
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008461 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008462 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008463
Jens Axboefe0f07d2015-04-15 17:05:48 -06008464 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008465
Josef Bacik0e267c42013-07-02 10:38:02 -04008466 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008467 * The generic stuff only does filemap_write_and_wait_range, which
8468 * isn't enough if we've written compressed pages to this area, so
8469 * we need to flush the dirty pages again to make absolutely sure
8470 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008471 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008472 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008473 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8474 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008475 filemap_fdatawrite_range(inode->i_mapping, offset,
8476 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008477
Omar Sandoval6f673762015-03-16 04:33:52 -07008478 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008479 /*
8480 * If the write DIO is beyond the EOF, we need update
8481 * the isize, but it is protected by i_mutex. So we can
8482 * not unlock the i_mutex at this case.
8483 */
8484 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008485 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008486 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008487 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008488 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8489 ret = -EAGAIN;
8490 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008491 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008492 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8493 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008494 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008495 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008496
8497 /*
8498 * We need to know how many extents we reserved so that we can
8499 * do the accounting properly if we go over the number we
8500 * originally calculated. Abuse current->journal_info for this.
8501 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008502 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008503 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008504 dio_data.unsubmitted_oe_range_start = (u64)offset;
8505 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308506 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308507 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008508 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8509 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008510 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008511 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8512 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008513 }
8514
Omar Sandoval17f8c842015-03-16 04:33:50 -07008515 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008516 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008517 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008518 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008519 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308520 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008521 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008522 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308523 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008524 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008525 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008526 /*
8527 * On error we might have left some ordered extents
8528 * without submitting corresponding bios for them, so
8529 * cleanup them up to avoid other tasks getting them
8530 * and waiting for them to complete forever.
8531 */
8532 if (dio_data.unsubmitted_oe_range_start <
8533 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008534 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008535 dio_data.unsubmitted_oe_range_start,
8536 dio_data.unsubmitted_oe_range_end -
8537 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008538 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008539 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008540 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008541 offset, count - (size_t)ret, true);
8542 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008543 }
Miao Xie38851cc2013-02-08 07:04:11 +00008544out:
Miao Xie2e60a512013-02-08 07:01:08 +00008545 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008546 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008547 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008548 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008549
Qu Wenruo364ecf32017-02-27 15:10:38 +08008550 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008551 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008552}
8553
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008554#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8555
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008556static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8557 __u64 start, __u64 len)
8558{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008559 int ret;
8560
8561 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8562 if (ret)
8563 return ret;
8564
David Sterba2135fb92017-06-23 04:09:57 +02008565 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008566}
8567
Chris Mason9ebefb182007-06-15 13:50:00 -04008568int btrfs_readpage(struct file *file, struct page *page)
8569{
Chris Masond1310b22008-01-24 16:13:08 -05008570 struct extent_io_tree *tree;
8571 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008572 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008573}
Chris Mason1832a6d2007-12-21 16:27:21 -05008574
Chris Mason39279cc2007-06-12 06:35:45 -04008575static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8576{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008577 struct inode *inode = page->mapping->host;
8578 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008579
8580 if (current->flags & PF_MEMALLOC) {
8581 redirty_page_for_writepage(wbc, page);
8582 unlock_page(page);
8583 return 0;
8584 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008585
8586 /*
8587 * If we are under memory pressure we will call this directly from the
8588 * VM, we need to make sure we have the inode referenced for the ordered
8589 * extent. If not just return like we didn't do anything.
8590 */
8591 if (!igrab(inode)) {
8592 redirty_page_for_writepage(wbc, page);
8593 return AOP_WRITEPAGE_ACTIVATE;
8594 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008595 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008596 btrfs_add_delayed_iput(inode);
8597 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008598}
Chris Mason39279cc2007-06-12 06:35:45 -04008599
Eric Sandeen48a3b632013-04-25 20:41:01 +00008600static int btrfs_writepages(struct address_space *mapping,
8601 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008602{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008603 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008604}
8605
Chris Mason3ab2fb52007-11-08 10:59:22 -05008606static int
8607btrfs_readpages(struct file *file, struct address_space *mapping,
8608 struct list_head *pages, unsigned nr_pages)
8609{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008610 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008611}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008612
Chris Masone6dcd2d2008-07-17 12:53:50 -04008613static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008614{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008615 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008616 if (ret == 1) {
8617 ClearPagePrivate(page);
8618 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008619 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008620 }
8621 return ret;
8622}
Chris Mason39279cc2007-06-12 06:35:45 -04008623
Chris Masone6dcd2d2008-07-17 12:53:50 -04008624static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8625{
Chris Mason98509cf2008-09-11 15:51:43 -04008626 if (PageWriteback(page) || PageDirty(page))
8627 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008628 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008629}
8630
Lukas Czernerd47992f2013-05-21 23:17:23 -04008631static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8632 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008633{
Josef Bacik5fd02042012-05-02 14:00:54 -04008634 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008635 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008636 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008637 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008638 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008639 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308640 u64 start;
8641 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008642 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008643
Chris Mason8b62b722009-09-02 16:53:46 -04008644 /*
8645 * we have the page locked, so new writeback can't start,
8646 * and the dirty bit won't be cleared while we are here.
8647 *
8648 * Wait for IO on this page so that we can safely clear
8649 * the PagePrivate2 bit and do ordered accounting
8650 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008651 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008652
Josef Bacik5fd02042012-05-02 14:00:54 -04008653 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008654 if (offset) {
8655 btrfs_releasepage(page, GFP_NOFS);
8656 return;
8657 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008658
8659 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008660 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308661again:
8662 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008663 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308664 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008665 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308666 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008667 /*
8668 * IO on this page will never be started, so we need
8669 * to account for any ordered extents now
8670 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008671 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308672 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008673 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008674 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008675 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008676 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008677 /*
8678 * whoever cleared the private bit is responsible
8679 * for the finish_ordered_io
8680 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008681 if (TestClearPagePrivate2(page)) {
8682 struct btrfs_ordered_inode_tree *tree;
8683 u64 new_len;
8684
8685 tree = &BTRFS_I(inode)->ordered_tree;
8686
8687 spin_lock_irq(&tree->lock);
8688 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308689 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008690 if (new_len < ordered->truncated_len)
8691 ordered->truncated_len = new_len;
8692 spin_unlock_irq(&tree->lock);
8693
8694 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308695 start,
8696 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008697 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008698 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008699 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008700 if (!inode_evicting) {
8701 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308702 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008703 &cached_state);
8704 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308705
8706 start = end + 1;
8707 if (start < page_end)
8708 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008709 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008710
Qu Wenruob9d0b382015-09-29 10:35:16 +08008711 /*
8712 * Qgroup reserved space handler
8713 * Page here will be either
8714 * 1) Already written to disk
8715 * In this case, its reserved space is released from data rsv map
8716 * and will be freed by delayed_ref handler finally.
8717 * So even we call qgroup_free_data(), it won't decrease reserved
8718 * space.
8719 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008720 * This means the reserved space should be freed here. However,
8721 * if a truncate invalidates the page (by clearing PageDirty)
8722 * and the page is accounted for while allocating extent
8723 * in btrfs_check_data_free_space() we let delayed_ref to
8724 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008725 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008726 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008727 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008728 if (!inode_evicting) {
8729 clear_extent_bit(tree, page_start, page_end,
8730 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008731 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8732 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008733 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008734
8735 __btrfs_releasepage(page, GFP_NOFS);
8736 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008737
Chris Mason4a096752008-07-21 10:29:44 -04008738 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008739 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008740 ClearPagePrivate(page);
8741 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008742 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008743 }
Chris Mason39279cc2007-06-12 06:35:45 -04008744}
8745
Chris Mason9ebefb182007-06-15 13:50:00 -04008746/*
8747 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8748 * called from a page fault handler when a page is first dirtied. Hence we must
8749 * be careful to check for EOF conditions here. We set the page up correctly
8750 * for a written page which means we get ENOSPC checking when writing into
8751 * holes and correct delalloc and unwritten extent mapping on filesystems that
8752 * support these features.
8753 *
8754 * We are not allowed to take the i_mutex here so we have to play games to
8755 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008756 * truncate_setsize() writes the inode size before removing pages, once we have
8757 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008758 * beyond EOF, then the page is guaranteed safe against truncation until we
8759 * unlock the page.
8760 */
Souptick Joardera528a242018-06-06 19:54:44 +05308761vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008762{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008763 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008764 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008765 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008766 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8767 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008768 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008769 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008770 char *kaddr;
8771 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008772 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308773 vm_fault_t ret;
8774 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008775 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308776 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008777 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008778 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308779 u64 end;
8780
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008781 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008782
Jan Karab2b5ef52012-06-12 16:20:45 +02008783 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008784 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008785 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308786 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008787
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308788 /*
8789 * Reserving delalloc space after obtaining the page lock can lead to
8790 * deadlock. For example, if a dirty page is locked by this function
8791 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8792 * dirty page write out, then the btrfs_writepage() function could
8793 * end up waiting indefinitely to get a lock on the page currently
8794 * being processed by btrfs_page_mkwrite() function.
8795 */
Souptick Joardera528a242018-06-06 19:54:44 +05308796 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308797 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308798 if (!ret2) {
8799 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008800 reserved = 1;
8801 }
Souptick Joardera528a242018-06-06 19:54:44 +05308802 if (ret2) {
8803 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008804 if (reserved)
8805 goto out;
8806 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008807 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008808
Nick Piggin56a76f82009-03-31 15:23:23 -07008809 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008810again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008811 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008812 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008813
Chris Mason9ebefb182007-06-15 13:50:00 -04008814 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008815 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008816 /* page got truncated out from underneath us */
8817 goto out_unlock;
8818 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008819 wait_on_page_writeback(page);
8820
David Sterbaff13db42015-12-03 14:30:40 +01008821 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008822 set_page_extent_mapped(page);
8823
Chris Masoneb84ae02008-07-17 13:53:27 -04008824 /*
8825 * we can't set the delalloc bits if there are pending ordered
8826 * extents. Drop our locks and wait for them to finish
8827 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008828 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8829 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008830 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008831 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008832 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008833 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008834 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008835 btrfs_put_ordered_extent(ordered);
8836 goto again;
8837 }
8838
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008839 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008840 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008841 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008842 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308843 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008844 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008845 page_start, PAGE_SIZE - reserved_space,
8846 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308847 }
8848 }
8849
Josef Bacikfbf19082009-10-01 17:10:23 -04008850 /*
Liu Bo54160342016-12-13 12:15:19 -08008851 * page_mkwrite gets called when the page is firstly dirtied after it's
8852 * faulted in, but write(2) could also dirty a page and set delalloc
8853 * bits, thus in this case for space account reason, we still need to
8854 * clear any delalloc bits within this page range since we have to
8855 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008856 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308857 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008858 EXTENT_DIRTY | EXTENT_DELALLOC |
8859 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008860 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008861
Souptick Joardera528a242018-06-06 19:54:44 +05308862 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008863 &cached_state, 0);
Souptick Joardera528a242018-06-06 19:54:44 +05308864 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008865 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008866 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008867 ret = VM_FAULT_SIGBUS;
8868 goto out_unlock;
8869 }
Souptick Joardera528a242018-06-06 19:54:44 +05308870 ret2 = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008871
8872 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008873 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008874 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008875 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008876 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008877
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008878 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008879 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008880 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008881 flush_dcache_page(page);
8882 kunmap(page);
8883 }
Chris Mason247e7432008-07-17 12:53:51 -04008884 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008885 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008886 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008887
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008888 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008889 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008890 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008891
David Sterbae43bbe52017-12-12 21:43:52 +01008892 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008893
Souptick Joardera528a242018-06-06 19:54:44 +05308894 if (!ret2) {
Qu Wenruo43b18592017-12-12 15:34:32 +08008895 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02008896 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008897 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04008898 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008899 }
Chris Mason717beb92018-06-25 10:03:41 -07008900
8901out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008902 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008903out:
Qu Wenruo43b18592017-12-12 15:34:32 +08008904 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08008905 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008906 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008907out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008908 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008909 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008910 return ret;
8911}
8912
Filipe Manana213e8c52018-02-06 20:40:31 +00008913static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008914{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008915 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008916 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008917 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008918 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008919 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008920 u64 mask = fs_info->sectorsize - 1;
8921 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008922
Filipe Manana213e8c52018-02-06 20:40:31 +00008923 if (!skip_writeback) {
8924 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8925 (u64)-1);
8926 if (ret)
8927 return ret;
8928 }
Chris Mason39279cc2007-06-12 06:35:45 -04008929
Josef Bacikfcb80c22011-05-03 10:40:22 -04008930 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008931 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8932 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008933 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008934 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008935 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008936 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008937 * be free'd up by the truncate operation, but also have some slack
8938 * space reserved in case it uses space during the truncate (thank you
8939 * very much snapshotting).
8940 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008941 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008942 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008943 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008944 * doesn't end up using space reserved for updating the inode. We also
8945 * need to be able to stop the transaction and start a new one, which
8946 * means we need to be able to update the inode several times, and we
8947 * have no idea of knowing how many times that will be, so we can't just
8948 * reserve 1 item for the entirety of the operation, so that has to be
8949 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008950 *
8951 * So that leaves us with
8952 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008953 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008954 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008955 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008956 * updating the inode.
8957 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008958 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008959 if (!rsv)
8960 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008961 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008962 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008963
Josef Bacik907cbce2011-08-08 13:46:15 -04008964 /*
Josef Bacik07127182011-08-19 10:29:59 -04008965 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008966 * 1 for updating the inode.
8967 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008968 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008969 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008970 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008971 goto out;
8972 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008973
Josef Bacik907cbce2011-08-08 13:46:15 -04008974 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008975 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008976 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008977 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008978
Chris Mason5a3f23d2009-03-31 13:27:11 -04008979 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008980 * So if we truncate and then write and fsync we normally would just
8981 * write the extents that changed, which is a problem if we need to
8982 * first truncate that entire inode. So set this flag so we write out
8983 * all of the extents in the inode to the sync log so we're completely
8984 * safe.
8985 */
8986 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008987 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008988
Yan, Zheng80825102009-11-12 09:35:36 +00008989 while (1) {
8990 ret = btrfs_truncate_inode_items(trans, root, inode,
8991 inode->i_size,
8992 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008993 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008994 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008995 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008996
Yan, Zheng80825102009-11-12 09:35:36 +00008997 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008998 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008999 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009000
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009001 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009002 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009003
9004 trans = btrfs_start_transaction(root, 2);
9005 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009006 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009007 trans = NULL;
9008 break;
9009 }
9010
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009011 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009012 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08009013 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009014 BUG_ON(ret); /* shouldn't happen */
9015 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009016 }
9017
Josef Bacikddfae632017-10-19 14:16:02 -04009018 /*
9019 * We can't call btrfs_truncate_block inside a trans handle as we could
9020 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9021 * we've truncated everything except the last little bit, and can do
9022 * btrfs_truncate_block and then update the disk_i_size.
9023 */
9024 if (ret == NEED_TRUNCATE_BLOCK) {
9025 btrfs_end_transaction(trans);
9026 btrfs_btree_balance_dirty(fs_info);
9027
9028 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9029 if (ret)
9030 goto out;
9031 trans = btrfs_start_transaction(root, 1);
9032 if (IS_ERR(trans)) {
9033 ret = PTR_ERR(trans);
9034 goto out;
9035 }
9036 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9037 }
9038
Chris Mason917c16b2011-11-08 14:49:59 -05009039 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009040 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04009041
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009042 trans->block_rsv = &fs_info->trans_block_rsv;
9043 ret2 = btrfs_update_inode(trans, root, inode);
9044 if (ret2 && !ret)
9045 ret = ret2;
9046
9047 ret2 = btrfs_end_transaction(trans);
9048 if (ret2 && !ret)
9049 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009050 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009051 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009052out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009053 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009054
Omar Sandovalad7e1a72018-05-22 09:59:50 -07009055 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009056}
9057
Sven Wegener3b963622008-06-09 21:57:42 -04009058/*
Chris Masond352ac62008-09-29 15:18:18 -04009059 * create a new subvolume directory/inode (helper for the ioctl).
9060 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009061int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009062 struct btrfs_root *new_root,
9063 struct btrfs_root *parent_root,
9064 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009065{
Chris Mason39279cc2007-06-12 06:35:45 -04009066 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009067 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009068 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009069
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009070 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9071 new_dirid, new_dirid,
9072 S_IFDIR | (~current_umask() & S_IRWXUGO),
9073 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009074 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009075 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009076 inode->i_op = &btrfs_dir_inode_operations;
9077 inode->i_fop = &btrfs_dir_file_operations;
9078
Miklos Szeredibfe86842011-10-28 14:13:29 +02009079 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009080 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009081 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009082
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009083 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9084 if (err)
9085 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009086 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009087 new_root->root_key.objectid, err);
9088
Yan, Zheng76dda932009-09-21 16:00:26 -04009089 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009090
Yan, Zheng76dda932009-09-21 16:00:26 -04009091 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009092 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009093}
9094
Chris Mason39279cc2007-06-12 06:35:45 -04009095struct inode *btrfs_alloc_inode(struct super_block *sb)
9096{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009097 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009098 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009099 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009100
David Sterba712e36c2017-10-31 17:08:27 +01009101 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009102 if (!ei)
9103 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009104
9105 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009106 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009107 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009108 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009109 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009110 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009111 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009112 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009113 ei->disk_i_size = 0;
9114 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009115 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009116 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009117 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009118 ei->last_unlink_trans = 0;
Filipe Manana41bd6062018-11-28 14:54:28 +00009119 ei->last_link_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009120 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009121
Josef Bacik9e0baf62011-07-15 15:16:44 +00009122 spin_lock_init(&ei->lock);
9123 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009124 if (sb->s_magic != BTRFS_TEST_MAGIC)
9125 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9126 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009127 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009128 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009129 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009130
Miao Xie16cdcec2011-04-22 18:12:22 +08009131 ei->delayed_node = NULL;
9132
chandan r9cc97d62012-07-04 12:48:07 +05309133 ei->i_otime.tv_sec = 0;
9134 ei->i_otime.tv_nsec = 0;
9135
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009136 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009137 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009138 extent_io_tree_init(&ei->io_tree, inode);
9139 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009140 ei->io_tree.track_uptodate = 1;
9141 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009142 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009143 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009144 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009145 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009146 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009147 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009148 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009149 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009150
9151 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009152}
9153
Josef Bacikaaedb552013-10-11 14:44:09 -04009154#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9155void btrfs_test_destroy_inode(struct inode *inode)
9156{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009157 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009158 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9159}
9160#endif
9161
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009162static void btrfs_i_callback(struct rcu_head *head)
9163{
9164 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009165 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9166}
9167
Chris Mason39279cc2007-06-12 06:35:45 -04009168void btrfs_destroy_inode(struct inode *inode)
9169{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009170 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009171 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009172 struct btrfs_root *root = BTRFS_I(inode)->root;
9173
Al Virob3d9b7a2012-06-09 13:51:19 -04009174 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009175 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009176 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9177 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009178 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009179 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009180 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009181 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009182 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009183
Chris Mason5a3f23d2009-03-31 13:27:11 -04009184 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009185 * This can happen where we create an inode, but somebody else also
9186 * created the same inode and we need to destroy the one we already
9187 * created.
9188 */
9189 if (!root)
9190 goto free;
9191
Chris Masond3977122009-01-05 21:25:51 -05009192 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009193 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9194 if (!ordered)
9195 break;
9196 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009197 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009198 "found ordered extent %llu %llu on inode cleanup",
9199 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009200 btrfs_remove_ordered_extent(inode, ordered);
9201 btrfs_put_ordered_extent(ordered);
9202 btrfs_put_ordered_extent(ordered);
9203 }
9204 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009205 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009206 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009207 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009208free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009209 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009210}
9211
Al Viro45321ac2010-06-07 13:43:19 -04009212int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009213{
9214 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009215
Naohiro Aota6379ef92013-06-06 09:56:34 +00009216 if (root == NULL)
9217 return 1;
9218
Liu Bofa6ac872013-02-20 14:10:23 +00009219 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009220 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009221 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009222 else
Al Viro45321ac2010-06-07 13:43:19 -04009223 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009224}
9225
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009226static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009227{
9228 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9229
9230 inode_init_once(&ei->vfs_inode);
9231}
9232
David Sterbae67c7182018-02-19 17:24:18 +01009233void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009234{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009235 /*
9236 * Make sure all delayed rcu free inodes are flushed before we
9237 * destroy cache.
9238 */
9239 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009240 kmem_cache_destroy(btrfs_inode_cachep);
9241 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009242 kmem_cache_destroy(btrfs_path_cachep);
9243 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009244}
9245
Liu Bof5c29bd2017-11-02 17:21:50 -06009246int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009247{
David Sterba837e1972012-09-07 03:00:48 -06009248 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009249 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009250 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9251 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009252 if (!btrfs_inode_cachep)
9253 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009254
David Sterba837e1972012-09-07 03:00:48 -06009255 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009256 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009257 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009258 if (!btrfs_trans_handle_cachep)
9259 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009260
David Sterba837e1972012-09-07 03:00:48 -06009261 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009262 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009263 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009264 if (!btrfs_path_cachep)
9265 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009266
David Sterba837e1972012-09-07 03:00:48 -06009267 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009268 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009269 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009270 if (!btrfs_free_space_cachep)
9271 goto fail;
9272
Chris Mason39279cc2007-06-12 06:35:45 -04009273 return 0;
9274fail:
9275 btrfs_destroy_cachep();
9276 return -ENOMEM;
9277}
9278
David Howellsa528d352017-01-31 16:46:22 +00009279static int btrfs_getattr(const struct path *path, struct kstat *stat,
9280 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009281{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009282 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009283 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009284 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009285 u32 bi_flags = BTRFS_I(inode)->flags;
9286
9287 stat->result_mask |= STATX_BTIME;
9288 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9289 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9290 if (bi_flags & BTRFS_INODE_APPEND)
9291 stat->attributes |= STATX_ATTR_APPEND;
9292 if (bi_flags & BTRFS_INODE_COMPRESS)
9293 stat->attributes |= STATX_ATTR_COMPRESSED;
9294 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9295 stat->attributes |= STATX_ATTR_IMMUTABLE;
9296 if (bi_flags & BTRFS_INODE_NODUMP)
9297 stat->attributes |= STATX_ATTR_NODUMP;
9298
9299 stat->attributes_mask |= (STATX_ATTR_APPEND |
9300 STATX_ATTR_COMPRESSED |
9301 STATX_ATTR_IMMUTABLE |
9302 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009303
Chris Mason39279cc2007-06-12 06:35:45 -04009304 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009305 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009306
9307 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009308 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009309 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009310 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009311 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009312 return 0;
9313}
9314
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009315static int btrfs_rename_exchange(struct inode *old_dir,
9316 struct dentry *old_dentry,
9317 struct inode *new_dir,
9318 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009319{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009320 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009321 struct btrfs_trans_handle *trans;
9322 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009323 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009324 struct inode *new_inode = new_dentry->d_inode;
9325 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009326 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009327 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009328 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9329 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009330 u64 old_idx = 0;
9331 u64 new_idx = 0;
9332 u64 root_objectid;
9333 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009334 bool root_log_pinned = false;
9335 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009336 struct btrfs_log_ctx ctx_root;
9337 struct btrfs_log_ctx ctx_dest;
9338 bool sync_log_root = false;
9339 bool sync_log_dest = false;
9340 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009341
9342 /* we only allow rename subvolume link between subvolumes */
9343 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9344 return -EXDEV;
9345
Filipe Mananad4682ba2018-06-11 19:24:28 +01009346 btrfs_init_log_ctx(&ctx_root, old_inode);
9347 btrfs_init_log_ctx(&ctx_dest, new_inode);
9348
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009349 /* close the race window with snapshot create/destroy ioctl */
9350 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009351 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009352 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009353 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009354
9355 /*
9356 * We want to reserve the absolute worst case amount of items. So if
9357 * both inodes are subvols and we need to unlink them then that would
9358 * require 4 item modifications, but if they are both normal inodes it
9359 * would require 5 item modifications, so we'll assume their normal
9360 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9361 * should cover the worst case number of items we'll modify.
9362 */
9363 trans = btrfs_start_transaction(root, 12);
9364 if (IS_ERR(trans)) {
9365 ret = PTR_ERR(trans);
9366 goto out_notrans;
9367 }
9368
9369 /*
9370 * We need to find a free sequence number both in the source and
9371 * in the destination directory for the exchange.
9372 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009373 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009374 if (ret)
9375 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009376 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009377 if (ret)
9378 goto out_fail;
9379
9380 BTRFS_I(old_inode)->dir_index = 0ULL;
9381 BTRFS_I(new_inode)->dir_index = 0ULL;
9382
9383 /* Reference for the source. */
9384 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9385 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009386 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009387 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009388 btrfs_pin_log_trans(root);
9389 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009390 ret = btrfs_insert_inode_ref(trans, dest,
9391 new_dentry->d_name.name,
9392 new_dentry->d_name.len,
9393 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009394 btrfs_ino(BTRFS_I(new_dir)),
9395 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009396 if (ret)
9397 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009398 }
9399
9400 /* And now for the dest. */
9401 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9402 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009403 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009404 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009405 btrfs_pin_log_trans(dest);
9406 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009407 ret = btrfs_insert_inode_ref(trans, root,
9408 old_dentry->d_name.name,
9409 old_dentry->d_name.len,
9410 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009411 btrfs_ino(BTRFS_I(old_dir)),
9412 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009413 if (ret)
9414 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009415 }
9416
9417 /* Update inode version and ctime/mtime. */
9418 inode_inc_iversion(old_dir);
9419 inode_inc_iversion(new_dir);
9420 inode_inc_iversion(old_inode);
9421 inode_inc_iversion(new_inode);
9422 old_dir->i_ctime = old_dir->i_mtime = ctime;
9423 new_dir->i_ctime = new_dir->i_mtime = ctime;
9424 old_inode->i_ctime = ctime;
9425 new_inode->i_ctime = ctime;
9426
9427 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009428 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9429 BTRFS_I(old_inode), 1);
9430 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9431 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009432 }
9433
9434 /* src is a subvolume */
9435 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9436 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009437 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009438 old_dentry->d_name.name,
9439 old_dentry->d_name.len);
9440 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009441 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9442 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009443 old_dentry->d_name.name,
9444 old_dentry->d_name.len);
9445 if (!ret)
9446 ret = btrfs_update_inode(trans, root, old_inode);
9447 }
9448 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009449 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009450 goto out_fail;
9451 }
9452
9453 /* dest is a subvolume */
9454 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9455 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009456 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009457 new_dentry->d_name.name,
9458 new_dentry->d_name.len);
9459 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009460 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9461 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009462 new_dentry->d_name.name,
9463 new_dentry->d_name.len);
9464 if (!ret)
9465 ret = btrfs_update_inode(trans, dest, new_inode);
9466 }
9467 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009468 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009469 goto out_fail;
9470 }
9471
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009472 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009473 new_dentry->d_name.name,
9474 new_dentry->d_name.len, 0, old_idx);
9475 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009476 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009477 goto out_fail;
9478 }
9479
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009480 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009481 old_dentry->d_name.name,
9482 old_dentry->d_name.len, 0, new_idx);
9483 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009484 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009485 goto out_fail;
9486 }
9487
9488 if (old_inode->i_nlink == 1)
9489 BTRFS_I(old_inode)->dir_index = old_idx;
9490 if (new_inode->i_nlink == 1)
9491 BTRFS_I(new_inode)->dir_index = new_idx;
9492
Filipe Manana86e8aa02016-05-05 02:02:27 +01009493 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009494 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009495 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9496 BTRFS_I(old_dir), parent,
9497 false, &ctx_root);
9498 if (ret == BTRFS_NEED_LOG_SYNC)
9499 sync_log_root = true;
9500 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9501 commit_transaction = true;
9502 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009503 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009504 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009505 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009506 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009507 if (!commit_transaction) {
9508 parent = old_dentry->d_parent;
9509 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9510 BTRFS_I(new_dir), parent,
9511 false, &ctx_dest);
9512 if (ret == BTRFS_NEED_LOG_SYNC)
9513 sync_log_dest = true;
9514 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9515 commit_transaction = true;
9516 ret = 0;
9517 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009518 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009519 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009520 }
9521out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009522 /*
9523 * If we have pinned a log and an error happened, we unpin tasks
9524 * trying to sync the log and force them to fallback to a transaction
9525 * commit if the log currently contains any of the inodes involved in
9526 * this rename operation (to ensure we do not persist a log with an
9527 * inconsistent state for any of these inodes or leading to any
9528 * inconsistencies when replayed). If the transaction was aborted, the
9529 * abortion reason is propagated to userspace when attempting to commit
9530 * the transaction. If the log does not contain any of these inodes, we
9531 * allow the tasks to sync it.
9532 */
9533 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009534 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9535 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9536 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009537 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009538 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009539 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009540
9541 if (root_log_pinned) {
9542 btrfs_end_log_trans(root);
9543 root_log_pinned = false;
9544 }
9545 if (dest_log_pinned) {
9546 btrfs_end_log_trans(dest);
9547 dest_log_pinned = false;
9548 }
9549 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009550 if (!ret && sync_log_root && !commit_transaction) {
9551 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9552 &ctx_root);
9553 if (ret)
9554 commit_transaction = true;
9555 }
9556 if (!ret && sync_log_dest && !commit_transaction) {
9557 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9558 &ctx_dest);
9559 if (ret)
9560 commit_transaction = true;
9561 }
9562 if (commit_transaction) {
9563 ret = btrfs_commit_transaction(trans);
9564 } else {
9565 int ret2;
9566
9567 ret2 = btrfs_end_transaction(trans);
9568 ret = ret ? ret : ret2;
9569 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009570out_notrans:
9571 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009572 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009573 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009574 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009575
9576 return ret;
9577}
9578
9579static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9580 struct btrfs_root *root,
9581 struct inode *dir,
9582 struct dentry *dentry)
9583{
9584 int ret;
9585 struct inode *inode;
9586 u64 objectid;
9587 u64 index;
9588
9589 ret = btrfs_find_free_ino(root, &objectid);
9590 if (ret)
9591 return ret;
9592
9593 inode = btrfs_new_inode(trans, root, dir,
9594 dentry->d_name.name,
9595 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009596 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 objectid,
9598 S_IFCHR | WHITEOUT_MODE,
9599 &index);
9600
9601 if (IS_ERR(inode)) {
9602 ret = PTR_ERR(inode);
9603 return ret;
9604 }
9605
9606 inode->i_op = &btrfs_special_inode_operations;
9607 init_special_inode(inode, inode->i_mode,
9608 WHITEOUT_DEV);
9609
9610 ret = btrfs_init_inode_security(trans, inode, dir,
9611 &dentry->d_name);
9612 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009613 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009615 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9616 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009618 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009619
9620 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009621out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009622 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009623 if (ret)
9624 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009625 iput(inode);
9626
Filipe Mananac9901612016-05-05 01:41:57 +01009627 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009628}
9629
Chris Mason39279cc2007-06-12 06:35:45 -04009630static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009631 struct inode *new_dir, struct dentry *new_dentry,
9632 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009633{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009634 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009635 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009636 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009637 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9638 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009639 struct inode *new_inode = d_inode(new_dentry);
9640 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009641 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009642 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009643 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009644 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009645 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009646 struct btrfs_log_ctx ctx;
9647 bool sync_log = false;
9648 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009649
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009650 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009651 return -EPERM;
9652
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009653 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009654 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009655 return -EXDEV;
9656
Li Zefan33345d012011-04-20 10:31:50 +08009657 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009658 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009659 return -ENOTEMPTY;
9660
Chris Mason39279cc2007-06-12 06:35:45 -04009661 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009662 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009663 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009664
9665
9666 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009667 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009668 new_dentry->d_name.name,
9669 new_dentry->d_name.len);
9670
9671 if (ret) {
9672 if (ret == -EEXIST) {
9673 /* we shouldn't get
9674 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309675 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009676 return ret;
9677 }
9678 } else {
9679 /* maybe -EOVERFLOW */
9680 return ret;
9681 }
9682 }
9683 ret = 0;
9684
Chris Mason5a3f23d2009-03-31 13:27:11 -04009685 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009686 * we're using rename to replace one file with another. Start IO on it
9687 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009688 */
Chris Mason8d875f92014-08-12 10:47:42 -07009689 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009690 filemap_flush(old_inode->i_mapping);
9691
Yan, Zheng76dda932009-09-21 16:00:26 -04009692 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009693 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009694 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009695 /*
9696 * We want to reserve the absolute worst case amount of items. So if
9697 * both inodes are subvols and we need to unlink them then that would
9698 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009699 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009700 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9701 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009702 * If our rename has the whiteout flag, we need more 5 units for the
9703 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9704 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009705 */
Filipe Manana5062af32016-05-05 10:26:26 +01009706 trans_num_items = 11;
9707 if (flags & RENAME_WHITEOUT)
9708 trans_num_items += 5;
9709 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009710 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009711 ret = PTR_ERR(trans);
9712 goto out_notrans;
9713 }
Chris Mason5f39d392007-10-15 16:14:19 -04009714
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009715 if (dest != root)
9716 btrfs_record_root_in_trans(trans, dest);
9717
Nikolay Borisov877574e2017-02-20 13:50:33 +02009718 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009719 if (ret)
9720 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009721
Miao Xie67de1172013-12-26 13:07:06 +08009722 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009723 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009724 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009725 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009726 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009727 btrfs_pin_log_trans(root);
9728 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009729 ret = btrfs_insert_inode_ref(trans, dest,
9730 new_dentry->d_name.name,
9731 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009732 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009733 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009734 if (ret)
9735 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009736 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009737
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009738 inode_inc_iversion(old_dir);
9739 inode_inc_iversion(new_dir);
9740 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009741 old_dir->i_ctime = old_dir->i_mtime =
9742 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009743 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009744
Chris Mason12fcfd22009-03-24 10:24:20 -04009745 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009746 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9747 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009748
Li Zefan33345d012011-04-20 10:31:50 +08009749 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009750 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009751 ret = btrfs_unlink_subvol(trans, old_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009752 old_dentry->d_name.name,
9753 old_dentry->d_name.len);
9754 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009755 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9756 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009757 old_dentry->d_name.name,
9758 old_dentry->d_name.len);
9759 if (!ret)
9760 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009761 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009762 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009763 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009764 goto out_fail;
9765 }
Chris Mason39279cc2007-06-12 06:35:45 -04009766
9767 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009768 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009769 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009770 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009771 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9772 root_objectid = BTRFS_I(new_inode)->location.objectid;
Lu Fengqi401b3b12018-08-01 11:32:30 +08009773 ret = btrfs_unlink_subvol(trans, new_dir, root_objectid,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009774 new_dentry->d_name.name,
9775 new_dentry->d_name.len);
9776 BUG_ON(new_inode->i_nlink == 0);
9777 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009778 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9779 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009780 new_dentry->d_name.name,
9781 new_dentry->d_name.len);
9782 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009783 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009784 ret = btrfs_orphan_add(trans,
9785 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009786 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009787 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009788 goto out_fail;
9789 }
Chris Mason39279cc2007-06-12 06:35:45 -04009790 }
Josef Bacikaec74772008-07-24 12:12:38 -04009791
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009792 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009793 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009794 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009795 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009796 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009797 goto out_fail;
9798 }
Chris Mason39279cc2007-06-12 06:35:45 -04009799
Miao Xie67de1172013-12-26 13:07:06 +08009800 if (old_inode->i_nlink == 1)
9801 BTRFS_I(old_inode)->dir_index = index;
9802
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009803 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009804 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009805
Filipe Mananad4682ba2018-06-11 19:24:28 +01009806 btrfs_init_log_ctx(&ctx, old_inode);
9807 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9808 BTRFS_I(old_dir), parent,
9809 false, &ctx);
9810 if (ret == BTRFS_NEED_LOG_SYNC)
9811 sync_log = true;
9812 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9813 commit_transaction = true;
9814 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009815 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009816 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009817 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009818
9819 if (flags & RENAME_WHITEOUT) {
9820 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9821 old_dentry);
9822
9823 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009824 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009825 goto out_fail;
9826 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009827 }
Chris Mason39279cc2007-06-12 06:35:45 -04009828out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009829 /*
9830 * If we have pinned the log and an error happened, we unpin tasks
9831 * trying to sync the log and force them to fallback to a transaction
9832 * commit if the log currently contains any of the inodes involved in
9833 * this rename operation (to ensure we do not persist a log with an
9834 * inconsistent state for any of these inodes or leading to any
9835 * inconsistencies when replayed). If the transaction was aborted, the
9836 * abortion reason is propagated to userspace when attempting to commit
9837 * the transaction. If the log does not contain any of these inodes, we
9838 * allow the tasks to sync it.
9839 */
9840 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009841 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9842 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9843 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009844 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009845 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009846 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009847
9848 btrfs_end_log_trans(root);
9849 log_pinned = false;
9850 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009851 if (!ret && sync_log) {
9852 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9853 if (ret)
9854 commit_transaction = true;
9855 }
9856 if (commit_transaction) {
9857 ret = btrfs_commit_transaction(trans);
9858 } else {
9859 int ret2;
9860
9861 ret2 = btrfs_end_transaction(trans);
9862 ret = ret ? ret : ret2;
9863 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009864out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009865 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009866 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009867
Chris Mason39279cc2007-06-12 06:35:45 -04009868 return ret;
9869}
9870
Miklos Szeredi80ace852014-07-23 15:15:32 +02009871static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9872 struct inode *new_dir, struct dentry *new_dentry,
9873 unsigned int flags)
9874{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009875 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009876 return -EINVAL;
9877
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009878 if (flags & RENAME_EXCHANGE)
9879 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9880 new_dentry);
9881
9882 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009883}
9884
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009885struct btrfs_delalloc_work {
9886 struct inode *inode;
9887 struct completion completion;
9888 struct list_head list;
9889 struct btrfs_work work;
9890};
9891
Miao Xie8ccf6f192012-10-25 09:28:04 +00009892static void btrfs_run_delalloc_work(struct btrfs_work *work)
9893{
9894 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009895 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009896
9897 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9898 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009899 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009900 filemap_flush(inode->i_mapping);
9901 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9902 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009903 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009904
Nikolay Borisov076da912018-04-23 10:54:16 +03009905 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009906 complete(&delalloc_work->completion);
9907}
9908
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009909static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009910{
9911 struct btrfs_delalloc_work *work;
9912
David Sterba100d5702015-12-08 14:39:32 +01009913 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009914 if (!work)
9915 return NULL;
9916
9917 init_completion(&work->completion);
9918 INIT_LIST_HEAD(&work->list);
9919 work->inode = inode;
Liu Bo9e0af232014-08-15 23:36:53 +08009920 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9921 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009922
9923 return work;
9924}
9925
Chris Masond352ac62008-09-29 15:18:18 -04009926/*
9927 * some fairly slow code that needs optimization. This walks the list
9928 * of all the inodes with pending delalloc and forces them to disk.
9929 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009930static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009931{
Chris Masonea8c2812008-08-04 23:17:27 -04009932 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009933 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009934 struct btrfs_delalloc_work *work, *next;
9935 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009936 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009937 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009938
Miao Xie8ccf6f192012-10-25 09:28:04 +00009939 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009940 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009941
Miao Xie573bfb72014-03-06 13:55:03 +08009942 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009943 spin_lock(&root->delalloc_lock);
9944 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009945 while (!list_empty(&splice)) {
9946 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009947 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009948
Miao Xieeb73c1b2013-05-15 07:48:22 +00009949 list_move_tail(&binode->delalloc_inodes,
9950 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009951 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009952 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009953 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009954 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009955 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009956 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009957
Ethan Lien3cd24c62018-11-01 14:49:03 +08009958 if (snapshot)
9959 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9960 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009961 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009962 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009963 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009964 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009965 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009966 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009967 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009968 btrfs_queue_work(root->fs_info->flush_workers,
9969 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009970 ret++;
9971 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009972 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009973 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009974 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009975 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009976 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009977
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009978out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009979 list_for_each_entry_safe(work, next, &works, list) {
9980 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009981 wait_for_completion(&work->completion);
9982 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009983 }
9984
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009985 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009986 spin_lock(&root->delalloc_lock);
9987 list_splice_tail(&splice, &root->delalloc_inodes);
9988 spin_unlock(&root->delalloc_lock);
9989 }
Miao Xie573bfb72014-03-06 13:55:03 +08009990 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009991 return ret;
9992}
9993
Ethan Lien3cd24c62018-11-01 14:49:03 +08009994int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009995{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009996 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009997 int ret;
9998
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009999 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010000 return -EROFS;
10001
Ethan Lien3cd24c62018-11-01 14:49:03 +080010002 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +080010003 if (ret > 0)
10004 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010005 return ret;
10006}
10007
Nikolay Borisov82b3e532018-04-23 10:54:13 +030010008int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010009{
10010 struct btrfs_root *root;
10011 struct list_head splice;
10012 int ret;
10013
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010014 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010015 return -EROFS;
10016
10017 INIT_LIST_HEAD(&splice);
10018
Miao Xie573bfb72014-03-06 13:55:03 +080010019 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010020 spin_lock(&fs_info->delalloc_root_lock);
10021 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010022 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010023 root = list_first_entry(&splice, struct btrfs_root,
10024 delalloc_root);
10025 root = btrfs_grab_fs_root(root);
10026 BUG_ON(!root);
10027 list_move_tail(&root->delalloc_root,
10028 &fs_info->delalloc_roots);
10029 spin_unlock(&fs_info->delalloc_root_lock);
10030
Ethan Lien3cd24c62018-11-01 14:49:03 +080010031 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010032 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010033 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010034 goto out;
10035
Miao Xie6c255e62014-03-06 13:55:01 +080010036 if (nr != -1) {
10037 nr -= ret;
10038 WARN_ON(nr < 0);
10039 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010040 spin_lock(&fs_info->delalloc_root_lock);
10041 }
10042 spin_unlock(&fs_info->delalloc_root_lock);
10043
Miao Xie6c255e62014-03-06 13:55:01 +080010044 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010045out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +030010046 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010047 spin_lock(&fs_info->delalloc_root_lock);
10048 list_splice_tail(&splice, &fs_info->delalloc_roots);
10049 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010050 }
Miao Xie573bfb72014-03-06 13:55:03 +080010051 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010052 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010053}
10054
Chris Mason39279cc2007-06-12 06:35:45 -040010055static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10056 const char *symname)
10057{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010058 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010059 struct btrfs_trans_handle *trans;
10060 struct btrfs_root *root = BTRFS_I(dir)->root;
10061 struct btrfs_path *path;
10062 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010063 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010064 int err;
Chris Mason39279cc2007-06-12 06:35:45 -040010065 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010066 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010067 int name_len;
10068 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010069 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010070 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010071 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010072
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010073 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010074 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010075 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010076
Josef Bacik9ed74f22009-09-11 16:12:44 -040010077 /*
10078 * 2 items for inode item and ref
10079 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010080 * 1 item for updating parent inode item
10081 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010082 * 1 item for xattr if selinux is on
10083 */
Filipe Manana9269d122015-12-31 18:16:29 +000010084 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010085 if (IS_ERR(trans))
10086 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010087
Li Zefan581bb052011-04-20 10:06:11 +080010088 err = btrfs_find_free_ino(root, &objectid);
10089 if (err)
10090 goto out_unlock;
10091
Josef Bacikaec74772008-07-24 12:12:38 -040010092 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010093 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10094 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010095 if (IS_ERR(inode)) {
10096 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -040010097 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010098 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010099 }
Chris Mason39279cc2007-06-12 06:35:45 -040010100
Casey Schauflerad19db72011-12-15 10:09:07 -050010101 /*
10102 * If the active LSM wants to access the inode during
10103 * d_instantiate it needs these. Smack checks to see
10104 * if the filesystem supports xattrs by looking at the
10105 * ops vector.
10106 */
10107 inode->i_fop = &btrfs_file_operations;
10108 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010109 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010110 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10111
10112 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10113 if (err)
Al Viro32955c52018-05-16 12:20:05 -040010114 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -050010115
Chris Mason39279cc2007-06-12 06:35:45 -040010116 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010117 if (!path) {
10118 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -040010119 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010120 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010121 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010122 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010123 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010124 datasize = btrfs_file_extent_calc_inline_size(name_len);
10125 err = btrfs_insert_empty_item(trans, root, path, &key,
10126 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010127 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010128 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -040010129 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -040010130 }
Chris Mason5f39d392007-10-15 16:14:19 -040010131 leaf = path->nodes[0];
10132 ei = btrfs_item_ptr(leaf, path->slots[0],
10133 struct btrfs_file_extent_item);
10134 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10135 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010136 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010137 btrfs_set_file_extent_encryption(leaf, ei, 0);
10138 btrfs_set_file_extent_compression(leaf, ei, 0);
10139 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10140 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10141
Chris Mason39279cc2007-06-12 06:35:45 -040010142 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010143 write_extent_buffer(leaf, symname, ptr, name_len);
10144 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010145 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010146
Chris Mason39279cc2007-06-12 06:35:45 -040010147 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010148 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -070010149 inode->i_mapping->a_ops = &btrfs_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010150 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010151 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010152 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010153 /*
10154 * Last step, add directory indexes for our symlink inode. This is the
10155 * last step to avoid extra cleanup of these indexes if an error happens
10156 * elsewhere above.
10157 */
10158 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010159 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10160 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -040010161 if (err)
10162 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -070010163
Al Viro1e2e5472018-05-04 08:23:01 -040010164 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010165
10166out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010167 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010168 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -040010169 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -040010170 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010171 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010172 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010173 return err;
10174}
Chris Mason16432982008-04-10 10:23:21 -040010175
Josef Bacik0af3d002010-06-21 14:48:16 -040010176static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10177 u64 start, u64 num_bytes, u64 min_size,
10178 loff_t actual_len, u64 *alloc_hint,
10179 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010181 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010182 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10183 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010184 struct btrfs_root *root = BTRFS_I(inode)->root;
10185 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010186 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010187 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010188 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010189 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010190 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010191 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010192 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010193
Josef Bacik0af3d002010-06-21 14:48:16 -040010194 if (trans)
10195 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010196 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010197 if (own_trans) {
10198 trans = btrfs_start_transaction(root, 3);
10199 if (IS_ERR(trans)) {
10200 ret = PTR_ERR(trans);
10201 break;
10202 }
Yan Zhengd899e052008-10-30 14:25:28 -040010203 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010204
Byongho Leeee221842015-12-15 01:42:10 +090010205 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010206 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010207 /*
10208 * If we are severely fragmented we could end up with really
10209 * small allocations, so if the allocator is returning small
10210 * chunks lets make its job easier by only searching for those
10211 * sized chunks.
10212 */
10213 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010214 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10215 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010216 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010217 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010218 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010219 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010220 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010221 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010222
Josef Bacik0b670dc2015-09-23 17:11:16 -040010223 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010224 ret = insert_reserved_file_extent(trans, inode,
10225 cur_offset, ins.objectid,
10226 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010227 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010228 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010229 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010230 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010231 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010232 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010233 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010234 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010235 break;
10236 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010237
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010238 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010239 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010240
Josef Bacik5dc562c2012-08-17 13:14:17 -040010241 em = alloc_extent_map();
10242 if (!em) {
10243 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10244 &BTRFS_I(inode)->runtime_flags);
10245 goto next;
10246 }
10247
10248 em->start = cur_offset;
10249 em->orig_start = cur_offset;
10250 em->len = ins.offset;
10251 em->block_start = ins.objectid;
10252 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010253 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010254 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010255 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010256 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10257 em->generation = trans->transid;
10258
10259 while (1) {
10260 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010261 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010262 write_unlock(&em_tree->lock);
10263 if (ret != -EEXIST)
10264 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010265 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010266 cur_offset + ins.offset - 1,
10267 0);
10268 }
10269 free_extent_map(em);
10270next:
Yan Zhengd899e052008-10-30 14:25:28 -040010271 num_bytes -= ins.offset;
10272 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010273 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010274
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010275 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010276 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010277 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010278 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010279 (actual_len > inode->i_size) &&
10280 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010281 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010282 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010283 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010284 i_size = cur_offset;
10285 i_size_write(inode, i_size);
10286 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010287 }
10288
Yan Zhengd899e052008-10-30 14:25:28 -040010289 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010290
10291 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010292 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010293 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010294 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010295 break;
10296 }
Yan Zhengd899e052008-10-30 14:25:28 -040010297
Josef Bacik0af3d002010-06-21 14:48:16 -040010298 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010299 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010300 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010301 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010302 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010303 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010304 return ret;
10305}
10306
Josef Bacik0af3d002010-06-21 14:48:16 -040010307int btrfs_prealloc_file_range(struct inode *inode, int mode,
10308 u64 start, u64 num_bytes, u64 min_size,
10309 loff_t actual_len, u64 *alloc_hint)
10310{
10311 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10312 min_size, actual_len, alloc_hint,
10313 NULL);
10314}
10315
10316int btrfs_prealloc_file_range_trans(struct inode *inode,
10317 struct btrfs_trans_handle *trans, int mode,
10318 u64 start, u64 num_bytes, u64 min_size,
10319 loff_t actual_len, u64 *alloc_hint)
10320{
10321 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10322 min_size, actual_len, alloc_hint, trans);
10323}
10324
Chris Masone6dcd2d2008-07-17 12:53:50 -040010325static int btrfs_set_page_dirty(struct page *page)
10326{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010327 return __set_page_dirty_nobuffers(page);
10328}
10329
Al Viro10556cb22011-06-20 19:28:19 -040010330static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010331{
Li Zefanb83cc962010-12-20 16:04:08 +080010332 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010333 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010334
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010335 if (mask & MAY_WRITE &&
10336 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10337 if (btrfs_root_readonly(root))
10338 return -EROFS;
10339 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10340 return -EACCES;
10341 }
Al Viro2830ba72011-06-20 19:16:29 -040010342 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010343}
Chris Mason39279cc2007-06-12 06:35:45 -040010344
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010345static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10346{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010347 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010348 struct btrfs_trans_handle *trans;
10349 struct btrfs_root *root = BTRFS_I(dir)->root;
10350 struct inode *inode = NULL;
10351 u64 objectid;
10352 u64 index;
10353 int ret = 0;
10354
10355 /*
10356 * 5 units required for adding orphan entry
10357 */
10358 trans = btrfs_start_transaction(root, 5);
10359 if (IS_ERR(trans))
10360 return PTR_ERR(trans);
10361
10362 ret = btrfs_find_free_ino(root, &objectid);
10363 if (ret)
10364 goto out;
10365
10366 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010367 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010368 if (IS_ERR(inode)) {
10369 ret = PTR_ERR(inode);
10370 inode = NULL;
10371 goto out;
10372 }
10373
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010374 inode->i_fop = &btrfs_file_operations;
10375 inode->i_op = &btrfs_file_inode_operations;
10376
10377 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010378 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10379
Chris Masonb0d5d102014-09-08 13:08:51 -070010380 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10381 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010382 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010383
10384 ret = btrfs_update_inode(trans, root, inode);
10385 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010386 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010387 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010388 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010389 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010390
Filipe Manana5762b5c2014-08-01 00:10:32 +010010391 /*
10392 * We set number of links to 0 in btrfs_new_inode(), and here we set
10393 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10394 * through:
10395 *
10396 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10397 */
10398 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010399 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010400 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010401 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010402out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010403 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010404 if (ret && inode)
10405 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010406 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010407 return ret;
10408}
10409
David Sterba5cdc84b2018-07-18 20:32:52 +020010410void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010411{
David Sterba5cdc84b2018-07-18 20:32:52 +020010412 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010413 unsigned long index = start >> PAGE_SHIFT;
10414 unsigned long end_index = end >> PAGE_SHIFT;
10415 struct page *page;
10416
10417 while (index <= end_index) {
10418 page = find_get_page(inode->i_mapping, index);
10419 ASSERT(page); /* Pages should be in the extent_io_tree */
10420 set_page_writeback(page);
10421 put_page(page);
10422 index++;
10423 }
10424}
10425
Omar Sandovaled46ff32016-11-03 10:28:14 -070010426#ifdef CONFIG_SWAP
10427/*
10428 * Add an entry indicating a block group or device which is pinned by a
10429 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10430 * negative errno on failure.
10431 */
10432static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10433 bool is_block_group)
10434{
10435 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10436 struct btrfs_swapfile_pin *sp, *entry;
10437 struct rb_node **p;
10438 struct rb_node *parent = NULL;
10439
10440 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10441 if (!sp)
10442 return -ENOMEM;
10443 sp->ptr = ptr;
10444 sp->inode = inode;
10445 sp->is_block_group = is_block_group;
10446
10447 spin_lock(&fs_info->swapfile_pins_lock);
10448 p = &fs_info->swapfile_pins.rb_node;
10449 while (*p) {
10450 parent = *p;
10451 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10452 if (sp->ptr < entry->ptr ||
10453 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10454 p = &(*p)->rb_left;
10455 } else if (sp->ptr > entry->ptr ||
10456 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10457 p = &(*p)->rb_right;
10458 } else {
10459 spin_unlock(&fs_info->swapfile_pins_lock);
10460 kfree(sp);
10461 return 1;
10462 }
10463 }
10464 rb_link_node(&sp->node, parent, p);
10465 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10466 spin_unlock(&fs_info->swapfile_pins_lock);
10467 return 0;
10468}
10469
10470/* Free all of the entries pinned by this swapfile. */
10471static void btrfs_free_swapfile_pins(struct inode *inode)
10472{
10473 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10474 struct btrfs_swapfile_pin *sp;
10475 struct rb_node *node, *next;
10476
10477 spin_lock(&fs_info->swapfile_pins_lock);
10478 node = rb_first(&fs_info->swapfile_pins);
10479 while (node) {
10480 next = rb_next(node);
10481 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10482 if (sp->inode == inode) {
10483 rb_erase(&sp->node, &fs_info->swapfile_pins);
10484 if (sp->is_block_group)
10485 btrfs_put_block_group(sp->ptr);
10486 kfree(sp);
10487 }
10488 node = next;
10489 }
10490 spin_unlock(&fs_info->swapfile_pins_lock);
10491}
10492
10493struct btrfs_swap_info {
10494 u64 start;
10495 u64 block_start;
10496 u64 block_len;
10497 u64 lowest_ppage;
10498 u64 highest_ppage;
10499 unsigned long nr_pages;
10500 int nr_extents;
10501};
10502
10503static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10504 struct btrfs_swap_info *bsi)
10505{
10506 unsigned long nr_pages;
10507 u64 first_ppage, first_ppage_reported, next_ppage;
10508 int ret;
10509
10510 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10511 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10512 PAGE_SIZE) >> PAGE_SHIFT;
10513
10514 if (first_ppage >= next_ppage)
10515 return 0;
10516 nr_pages = next_ppage - first_ppage;
10517
10518 first_ppage_reported = first_ppage;
10519 if (bsi->start == 0)
10520 first_ppage_reported++;
10521 if (bsi->lowest_ppage > first_ppage_reported)
10522 bsi->lowest_ppage = first_ppage_reported;
10523 if (bsi->highest_ppage < (next_ppage - 1))
10524 bsi->highest_ppage = next_ppage - 1;
10525
10526 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10527 if (ret < 0)
10528 return ret;
10529 bsi->nr_extents += ret;
10530 bsi->nr_pages += nr_pages;
10531 return 0;
10532}
10533
10534static void btrfs_swap_deactivate(struct file *file)
10535{
10536 struct inode *inode = file_inode(file);
10537
10538 btrfs_free_swapfile_pins(inode);
10539 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10540}
10541
10542static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10543 sector_t *span)
10544{
10545 struct inode *inode = file_inode(file);
10546 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10547 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10548 struct extent_state *cached_state = NULL;
10549 struct extent_map *em = NULL;
10550 struct btrfs_device *device = NULL;
10551 struct btrfs_swap_info bsi = {
10552 .lowest_ppage = (sector_t)-1ULL,
10553 };
10554 int ret = 0;
10555 u64 isize;
10556 u64 start;
10557
10558 /*
10559 * If the swap file was just created, make sure delalloc is done. If the
10560 * file changes again after this, the user is doing something stupid and
10561 * we don't really care.
10562 */
10563 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10564 if (ret)
10565 return ret;
10566
10567 /*
10568 * The inode is locked, so these flags won't change after we check them.
10569 */
10570 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10571 btrfs_warn(fs_info, "swapfile must not be compressed");
10572 return -EINVAL;
10573 }
10574 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10575 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10576 return -EINVAL;
10577 }
10578 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10579 btrfs_warn(fs_info, "swapfile must not be checksummed");
10580 return -EINVAL;
10581 }
10582
10583 /*
10584 * Balance or device remove/replace/resize can move stuff around from
10585 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10586 * concurrently while we are mapping the swap extents, and
10587 * fs_info->swapfile_pins prevents them from running while the swap file
10588 * is active and moving the extents. Note that this also prevents a
10589 * concurrent device add which isn't actually necessary, but it's not
10590 * really worth the trouble to allow it.
10591 */
10592 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10593 btrfs_warn(fs_info,
10594 "cannot activate swapfile while exclusive operation is running");
10595 return -EBUSY;
10596 }
10597 /*
10598 * Snapshots can create extents which require COW even if NODATACOW is
10599 * set. We use this counter to prevent snapshots. We must increment it
10600 * before walking the extents because we don't want a concurrent
10601 * snapshot to run after we've already checked the extents.
10602 */
10603 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10604
10605 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10606
10607 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10608 start = 0;
10609 while (start < isize) {
10610 u64 logical_block_start, physical_block_start;
10611 struct btrfs_block_group_cache *bg;
10612 u64 len = isize - start;
10613
10614 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10615 if (IS_ERR(em)) {
10616 ret = PTR_ERR(em);
10617 goto out;
10618 }
10619
10620 if (em->block_start == EXTENT_MAP_HOLE) {
10621 btrfs_warn(fs_info, "swapfile must not have holes");
10622 ret = -EINVAL;
10623 goto out;
10624 }
10625 if (em->block_start == EXTENT_MAP_INLINE) {
10626 /*
10627 * It's unlikely we'll ever actually find ourselves
10628 * here, as a file small enough to fit inline won't be
10629 * big enough to store more than the swap header, but in
10630 * case something changes in the future, let's catch it
10631 * here rather than later.
10632 */
10633 btrfs_warn(fs_info, "swapfile must not be inline");
10634 ret = -EINVAL;
10635 goto out;
10636 }
10637 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10638 btrfs_warn(fs_info, "swapfile must not be compressed");
10639 ret = -EINVAL;
10640 goto out;
10641 }
10642
10643 logical_block_start = em->block_start + (start - em->start);
10644 len = min(len, em->len - (start - em->start));
10645 free_extent_map(em);
10646 em = NULL;
10647
10648 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10649 if (ret < 0) {
10650 goto out;
10651 } else if (ret) {
10652 ret = 0;
10653 } else {
10654 btrfs_warn(fs_info,
10655 "swapfile must not be copy-on-write");
10656 ret = -EINVAL;
10657 goto out;
10658 }
10659
10660 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10661 if (IS_ERR(em)) {
10662 ret = PTR_ERR(em);
10663 goto out;
10664 }
10665
10666 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10667 btrfs_warn(fs_info,
10668 "swapfile must have single data profile");
10669 ret = -EINVAL;
10670 goto out;
10671 }
10672
10673 if (device == NULL) {
10674 device = em->map_lookup->stripes[0].dev;
10675 ret = btrfs_add_swapfile_pin(inode, device, false);
10676 if (ret == 1)
10677 ret = 0;
10678 else if (ret)
10679 goto out;
10680 } else if (device != em->map_lookup->stripes[0].dev) {
10681 btrfs_warn(fs_info, "swapfile must be on one device");
10682 ret = -EINVAL;
10683 goto out;
10684 }
10685
10686 physical_block_start = (em->map_lookup->stripes[0].physical +
10687 (logical_block_start - em->start));
10688 len = min(len, em->len - (logical_block_start - em->start));
10689 free_extent_map(em);
10690 em = NULL;
10691
10692 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10693 if (!bg) {
10694 btrfs_warn(fs_info,
10695 "could not find block group containing swapfile");
10696 ret = -EINVAL;
10697 goto out;
10698 }
10699
10700 ret = btrfs_add_swapfile_pin(inode, bg, true);
10701 if (ret) {
10702 btrfs_put_block_group(bg);
10703 if (ret == 1)
10704 ret = 0;
10705 else
10706 goto out;
10707 }
10708
10709 if (bsi.block_len &&
10710 bsi.block_start + bsi.block_len == physical_block_start) {
10711 bsi.block_len += len;
10712 } else {
10713 if (bsi.block_len) {
10714 ret = btrfs_add_swap_extent(sis, &bsi);
10715 if (ret)
10716 goto out;
10717 }
10718 bsi.start = start;
10719 bsi.block_start = physical_block_start;
10720 bsi.block_len = len;
10721 }
10722
10723 start += len;
10724 }
10725
10726 if (bsi.block_len)
10727 ret = btrfs_add_swap_extent(sis, &bsi);
10728
10729out:
10730 if (!IS_ERR_OR_NULL(em))
10731 free_extent_map(em);
10732
10733 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10734
10735 if (ret)
10736 btrfs_swap_deactivate(file);
10737
10738 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10739
10740 if (ret)
10741 return ret;
10742
10743 if (device)
10744 sis->bdev = device->bdev;
10745 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10746 sis->max = bsi.nr_pages;
10747 sis->pages = bsi.nr_pages - 1;
10748 sis->highest_bit = bsi.nr_pages - 1;
10749 return bsi.nr_extents;
10750}
10751#else
10752static void btrfs_swap_deactivate(struct file *file)
10753{
10754}
10755
10756static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10757 sector_t *span)
10758{
10759 return -EOPNOTSUPP;
10760}
10761#endif
10762
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010763static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010764 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010765 .lookup = btrfs_lookup,
10766 .create = btrfs_create,
10767 .unlink = btrfs_unlink,
10768 .link = btrfs_link,
10769 .mkdir = btrfs_mkdir,
10770 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010771 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010772 .symlink = btrfs_symlink,
10773 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010774 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010775 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010776 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010777 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010778 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010779 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010780 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010781};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010782static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010783 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010784 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010785 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010786};
Yan, Zheng76dda932009-09-21 16:00:26 -040010787
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010788static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010789 .llseek = generic_file_llseek,
10790 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010791 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010792 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010793 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010794#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010795 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010796#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010797 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010798 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010799};
10800
David Sterba20e55062015-11-19 11:42:28 +010010801static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010802 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010803 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010804 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10805};
10806
Chris Mason35054392009-01-21 13:11:13 -050010807/*
10808 * btrfs doesn't support the bmap operation because swapfiles
10809 * use bmap to make a mapping of extents in the file. They assume
10810 * these extents won't change over the life of the file and they
10811 * use the bmap result to do IO directly to the drive.
10812 *
10813 * the btrfs bmap call would return logical addresses that aren't
10814 * suitable for IO and they also will change frequently as COW
10815 * operations happen. So, swapfile + btrfs == corruption.
10816 *
10817 * For now we're avoiding this by dropping bmap.
10818 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010819static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010820 .readpage = btrfs_readpage,
10821 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010822 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010823 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010824 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010825 .invalidatepage = btrfs_invalidatepage,
10826 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010827 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010828 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010829 .swap_activate = btrfs_swap_activate,
10830 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010831};
10832
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010833static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010834 .getattr = btrfs_getattr,
10835 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010836 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010837 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010838 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010839 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010840 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010841 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010842};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010843static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010844 .getattr = btrfs_getattr,
10845 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010846 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010847 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010848 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010849 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010850 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010851};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010852static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010853 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010854 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010855 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010856 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010857 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010858 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010859};
Yan, Zheng76dda932009-09-21 16:00:26 -040010860
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010861const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010862 .d_delete = btrfs_dentry_delete,
10863};