blob: b904c74f31fd32c2120e179033bdff4dc62fe1ff [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800112static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Eric Sandeen48a3b632013-04-25 20:41:01 +0000118static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400119
Josef Bacik6a3891c2015-03-16 17:38:52 -0400120#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121void btrfs_test_inode_set_ops(struct inode *inode)
122{
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124}
125#endif
126
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000127static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500130{
131 int err;
132
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000133 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500136 return err;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/*
Chris Masonc8b97812008-10-29 14:49:59 -0400140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
143 */
Chris Mason40f76582014-05-21 13:35:51 -0700144static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000145 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000148 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct page **compressed_pages)
150{
Chris Masonc8b97812008-10-29 14:49:59 -0400151 struct extent_buffer *leaf;
152 struct page *page = NULL;
153 char *kaddr;
154 unsigned long ptr;
155 struct btrfs_file_extent_item *ei;
156 int err = 0;
157 int ret;
158 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400160
Li Zefanfe3f5662011-03-28 08:30:38 +0000161 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400163
Chris Masonc8b97812008-10-29 14:49:59 -0400164 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 if (!extent_inserted) {
167 struct btrfs_key key;
168 size_t datasize;
169
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200170 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000171 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200172 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000173
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 datasize);
178 if (ret) {
179 err = ret;
180 goto fail;
181 }
Chris Masonc8b97812008-10-29 14:49:59 -0400182 }
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
192
Li Zefan261507a02010-12-17 14:21:50 +0800193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 struct page *cpage;
195 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500196 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500198 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400200
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204
205 i++;
206 ptr += cur_size;
207 compressed_size -= cur_size;
208 }
209 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800210 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 } else {
212 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800215 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400220 }
221 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000222 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Yan, Zhengc2167752009-11-12 09:34:21 +0000224 /*
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
228 *
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
232 */
Chris Masonc8b97812008-10-29 14:49:59 -0400233 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000235
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100236 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400237fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400238 return err;
239}
240
241
242/*
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
246 */
Josef Bacik00361582013-08-14 14:02:47 -0400247static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
250 int compress_type,
251 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400253 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400254 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400255 u64 isize = i_size_read(inode);
256 u64 actual_end = min(end + 1, isize);
257 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400258 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400259 u64 data_len = inline_len;
260 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 struct btrfs_path *path;
262 int extent_inserted = 0;
263 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400264
265 if (compressed_size)
266 data_len = compressed_size;
267
268 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400269 actual_end > fs_info->sectorsize ||
270 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400271 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400272 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400273 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400274 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400275 return 1;
276 }
277
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000278 path = btrfs_alloc_path();
279 if (!path)
280 return -ENOMEM;
281
Josef Bacik00361582013-08-14 14:02:47 -0400282 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000283 if (IS_ERR(trans)) {
284 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400285 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000286 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400287 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400288
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 if (compressed_size && compressed_pages)
290 extent_item_size = btrfs_file_extent_calc_inline_size(
291 compressed_size);
292 else
293 extent_item_size = btrfs_file_extent_calc_inline_size(
294 inline_len);
295
296 ret = __btrfs_drop_extents(trans, root, inode, path,
297 start, aligned_end, NULL,
298 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400299 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400300 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400301 goto out;
302 }
Chris Masonc8b97812008-10-29 14:49:59 -0400303
304 if (isize > actual_end)
305 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000306 ret = insert_inline_extent(trans, path, extent_inserted,
307 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400308 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000309 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400310 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400311 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400312 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400313 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400314 ret = 1;
315 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100316 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400317
Josef Bacikbdc20e62013-02-28 13:23:38 -0500318 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov691fa052017-02-20 13:50:42 +0200319 btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200320 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400321out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800322 /*
323 * Don't forget to free the reserved space, as for inlined extent
324 * it won't count as data extent, free them directly here.
325 * And at reserve time, it's always aligned to page size, so
326 * just free one page here.
327 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300328 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000329 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400330 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400331 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400332}
333
Chris Mason771ed682008-11-06 22:02:51 -0500334struct async_extent {
335 u64 start;
336 u64 ram_size;
337 u64 compressed_size;
338 struct page **pages;
339 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800340 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500341 struct list_head list;
342};
343
344struct async_cow {
345 struct inode *inode;
346 struct btrfs_root *root;
347 struct page *locked_page;
348 u64 start;
349 u64 end;
350 struct list_head extents;
351 struct btrfs_work work;
352};
353
354static noinline int add_async_extent(struct async_cow *cow,
355 u64 start, u64 ram_size,
356 u64 compressed_size,
357 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800358 unsigned long nr_pages,
359 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500360{
361 struct async_extent *async_extent;
362
363 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100364 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500365 async_extent->start = start;
366 async_extent->ram_size = ram_size;
367 async_extent->compressed_size = compressed_size;
368 async_extent->pages = pages;
369 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800370 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500371 list_add_tail(&async_extent->list, &cow->extents);
372 return 0;
373}
374
Wang Shilongf79707b2014-07-17 11:44:09 +0800375static inline int inode_need_compress(struct inode *inode)
376{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400377 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800378
379 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400380 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800381 return 1;
382 /* bad compression ratios */
383 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
384 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400385 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800386 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
387 BTRFS_I(inode)->force_compress)
388 return 1;
389 return 0;
390}
391
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200392static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800393 u64 start, u64 end, u64 num_bytes, u64 small_write)
394{
395 /* If this is a small write inside eof, kick off a defrag */
396 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200397 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800398 btrfs_add_inode_defrag(NULL, inode);
399}
400
Chris Masonc8b97812008-10-29 14:49:59 -0400401/*
Chris Mason771ed682008-11-06 22:02:51 -0500402 * we create compressed extents in two phases. The first
403 * phase compresses a range of pages that have already been
404 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400405 *
Chris Mason771ed682008-11-06 22:02:51 -0500406 * This is done inside an ordered work queue, and the compression
407 * is spread across many cpus. The actual IO submission is step
408 * two, and the ordered work queue takes care of making sure that
409 * happens in the same order things were put onto the queue by
410 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400411 *
Chris Mason771ed682008-11-06 22:02:51 -0500412 * If this code finds it can't get good compression, it puts an
413 * entry onto the work queue to write the uncompressed bytes. This
414 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300415 * are written in the same order that the flusher thread sent them
416 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400417 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100418static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500419 struct page *locked_page,
420 u64 start, u64 end,
421 struct async_cow *async_cow,
422 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400423{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db2b2007-08-27 16:49:44 -0400425 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400426 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400427 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400428 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500429 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400430 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400431 struct page **pages = NULL;
432 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400433 unsigned long total_compressed = 0;
434 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400435 int i;
436 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400437 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400438 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400439
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200440 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
441 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400442
Chris Mason42dc7ba2008-12-15 11:44:56 -0500443 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400444again:
445 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300446 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100447 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
448 nr_pages = min_t(unsigned long, nr_pages,
449 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400450
Chris Masonf03d9301f2009-02-04 09:31:06 -0500451 /*
452 * we don't want to send crud past the end of i_size through
453 * compression, that's just a waste of CPU time. So, if the
454 * end of the file is before the start of our current
455 * requested range of bytes, we bail out to the uncompressed
456 * cleanup code that can deal with all of this.
457 *
458 * It isn't really the fastest way to fix things, but this is a
459 * very uncommon corner.
460 */
461 if (actual_end <= start)
462 goto cleanup_and_bail_uncompressed;
463
Chris Masonc8b97812008-10-29 14:49:59 -0400464 total_compressed = actual_end - start;
465
Shilong Wang4bcbb332014-10-07 18:44:35 -0400466 /*
467 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400468 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400469 */
470 if (total_compressed <= blocksize &&
471 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
472 goto cleanup_and_bail_uncompressed;
473
David Sterba069eac72017-02-14 19:36:54 +0100474 total_compressed = min_t(unsigned long, total_compressed,
475 BTRFS_MAX_UNCOMPRESSED);
Qu Wenruofda28322013-02-26 08:10:22 +0000476 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500477 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400478 total_in = 0;
479 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400480
Chris Mason771ed682008-11-06 22:02:51 -0500481 /*
482 * we do compression for mount -o compress and when the
483 * inode has not been flagged as nocompress. This flag can
484 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400485 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800486 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400487 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100488 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800489 if (!pages) {
490 /* just bail out to the uncompressed code */
491 goto cont;
492 }
Chris Mason179e29e2007-11-01 11:28:41 -0400493
Li Zefan261507a02010-12-17 14:21:50 +0800494 if (BTRFS_I(inode)->force_compress)
495 compress_type = BTRFS_I(inode)->force_compress;
496
Chris Mason4adaa612013-03-26 13:07:00 -0400497 /*
498 * we need to call clear_page_dirty_for_io on each
499 * page in the range. Otherwise applications with the file
500 * mmap'd can wander in and change the page contents while
501 * we are compressing them.
502 *
503 * If the compression fails for any reason, we set the pages
504 * dirty again later on.
505 */
506 extent_range_clear_dirty_for_io(inode, start, end);
507 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800508 ret = btrfs_compress_pages(compress_type,
509 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100510 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100511 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800512 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100513 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400514
515 if (!ret) {
516 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300517 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100518 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400519 char *kaddr;
520
521 /* zero the tail end of the last page, we might be
522 * sending it down to disk
523 */
524 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800525 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400526 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300527 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800528 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 }
530 will_compress = 1;
531 }
532 }
Li Zefan560f7d72011-09-08 10:22:01 +0800533cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400534 if (start == 0) {
535 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500536 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400537 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500538 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400539 */
Josef Bacik00361582013-08-14 14:02:47 -0400540 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800541 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400542 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500543 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400544 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000545 total_compressed,
546 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400547 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100548 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400549 unsigned long clear_flags = EXTENT_DELALLOC |
550 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100551 unsigned long page_error_op;
552
Josef Bacik151a41b2013-07-29 13:22:24 -0400553 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100554 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400555
Chris Mason771ed682008-11-06 22:02:51 -0500556 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100557 * inline extent creation worked or returned error,
558 * we don't need to create any more async work items.
559 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500560 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800561 extent_clear_unlock_delalloc(inode, start, end, end,
562 NULL, clear_flags,
563 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400564 PAGE_CLEAR_DIRTY |
565 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100566 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400567 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800568 btrfs_free_reserved_data_space_noquota(inode, start,
569 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400570 goto free_pages_out;
571 }
572 }
573
574 if (will_compress) {
575 /*
576 * we aren't doing an inline extent round the compressed size
577 * up to a block size boundary so the allocator does sane
578 * things
579 */
Qu Wenruofda28322013-02-26 08:10:22 +0000580 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400581
582 /*
583 * one last check to make sure the compression is really a
584 * win, compare the page count read with the blocks on disk
585 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300586 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400587 if (total_compressed >= total_in) {
588 will_compress = 0;
589 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400590 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700591 *num_added += 1;
592
593 /*
594 * The async work queues will take care of doing actual
595 * allocation on disk for these compressed pages, and
596 * will submit them to the elevator.
597 */
598 add_async_extent(async_cow, start, num_bytes,
David Sterba4d3a8002017-02-14 19:04:07 +0100599 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700600 compress_type);
601
602 if (start + num_bytes < end) {
603 start += num_bytes;
604 pages = NULL;
605 cond_resched();
606 goto again;
607 }
608 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400609 }
610 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700611 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400612 /*
613 * the compression code ran but failed to make things smaller,
614 * free any pages it allocated and our page pointer array
615 */
David Sterba4d3a8002017-02-14 19:04:07 +0100616 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400617 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300618 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400619 }
620 kfree(pages);
621 pages = NULL;
622 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100623 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400624
625 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400626 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500627 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500628 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500629 }
Chris Masonc8b97812008-10-29 14:49:59 -0400630 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500631cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700632 /*
633 * No compression, but we still need to write the pages in the file
634 * we've been given so far. redirty the locked page if it corresponds
635 * to our extent and set things up for the async work queue to run
636 * cow_file_range to do the normal delalloc dance.
637 */
638 if (page_offset(locked_page) >= start &&
639 page_offset(locked_page) <= end)
640 __set_page_dirty_nobuffers(locked_page);
641 /* unlocked later on in the async handlers */
642
643 if (redirty)
644 extent_range_redirty_for_io(inode, start, end);
645 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
646 BTRFS_COMPRESS_NONE);
647 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500648
Filipe Mananac44f6492014-10-09 21:15:44 +0100649 return;
Chris Mason771ed682008-11-06 22:02:51 -0500650
651free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100652 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500653 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300654 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500655 }
Chris Masond3977122009-01-05 21:25:51 -0500656 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500657}
Chris Mason771ed682008-11-06 22:02:51 -0500658
Filipe Manana40ae8372014-10-06 22:14:24 +0100659static void free_async_extent_pages(struct async_extent *async_extent)
660{
661 int i;
662
663 if (!async_extent->pages)
664 return;
665
666 for (i = 0; i < async_extent->nr_pages; i++) {
667 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300668 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100669 }
670 kfree(async_extent->pages);
671 async_extent->nr_pages = 0;
672 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500673}
674
675/*
676 * phase two of compressed writeback. This is the ordered portion
677 * of the code, which only gets called in the order the work was
678 * queued. We walk all the async extents created by compress_file_range
679 * and send them down to the disk.
680 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100681static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500682 struct async_cow *async_cow)
683{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400684 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500685 struct async_extent *async_extent;
686 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500687 struct btrfs_key ins;
688 struct extent_map *em;
689 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500690 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500691 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500692
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500693again:
Chris Masond3977122009-01-05 21:25:51 -0500694 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500695 async_extent = list_entry(async_cow->extents.next,
696 struct async_extent, list);
697 list_del(&async_extent->list);
698
699 io_tree = &BTRFS_I(inode)->io_tree;
700
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500701retry:
Chris Mason771ed682008-11-06 22:02:51 -0500702 /* did the compression code fall back to uncompressed IO? */
703 if (!async_extent->pages) {
704 int page_started = 0;
705 unsigned long nr_written = 0;
706
707 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000708 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100709 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500710
711 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500712 ret = cow_file_range(inode, async_cow->locked_page,
713 async_extent->start,
714 async_extent->start +
715 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800716 async_extent->start +
717 async_extent->ram_size - 1,
718 &page_started, &nr_written, 0,
719 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500720
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100721 /* JDM XXX */
722
Chris Mason771ed682008-11-06 22:02:51 -0500723 /*
724 * if page_started, cow_file_range inserted an
725 * inline extent and took care of all the unlocking
726 * and IO for us. Otherwise, we need to submit
727 * all those pages down to the drive.
728 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500729 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500730 extent_write_locked_range(io_tree,
731 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500732 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500733 async_extent->ram_size - 1,
734 btrfs_get_extent,
735 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500736 else if (ret)
737 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500738 kfree(async_extent);
739 cond_resched();
740 continue;
741 }
742
743 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100744 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500745
Wang Xiaoguang18513092016-07-25 15:51:40 +0800746 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500747 async_extent->compressed_size,
748 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800749 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500750 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100751 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500752
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400753 if (ret == -ENOSPC) {
754 unlock_extent(io_tree, async_extent->start,
755 async_extent->start +
756 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800757
758 /*
759 * we need to redirty the pages if we decide to
760 * fallback to uncompressed IO, otherwise we
761 * will not submit these pages down to lower
762 * layers.
763 */
764 extent_range_redirty_for_io(inode,
765 async_extent->start,
766 async_extent->start +
767 async_extent->ram_size - 1);
768
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100769 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400770 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500771 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500772 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000773 /*
774 * here we're doing allocation and writeback of the
775 * compressed pages
776 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800777 em = create_io_em(inode, async_extent->start,
778 async_extent->ram_size, /* len */
779 async_extent->start, /* orig_start */
780 ins.objectid, /* block_start */
781 ins.offset, /* block_len */
782 ins.offset, /* orig_block_len */
783 async_extent->ram_size, /* ram_bytes */
784 async_extent->compress_type,
785 BTRFS_ORDERED_COMPRESSED);
786 if (IS_ERR(em))
787 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500788 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800789 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500790
Li Zefan261507a02010-12-17 14:21:50 +0800791 ret = btrfs_add_ordered_extent_compress(inode,
792 async_extent->start,
793 ins.objectid,
794 async_extent->ram_size,
795 ins.offset,
796 BTRFS_ORDERED_COMPRESSED,
797 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100798 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200799 btrfs_drop_extent_cache(BTRFS_I(inode),
800 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100801 async_extent->start +
802 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500803 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100804 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400805 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500806
Chris Mason771ed682008-11-06 22:02:51 -0500807 /*
808 * clear dirty, set writeback and unlock the pages.
809 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400810 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400811 async_extent->start +
812 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800813 async_extent->start +
814 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400815 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
816 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400817 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500818 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500819 async_extent->start,
820 async_extent->ram_size,
821 ins.objectid,
822 ins.offset, async_extent->pages,
823 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100824 if (ret) {
825 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
826 struct page *p = async_extent->pages[0];
827 const u64 start = async_extent->start;
828 const u64 end = start + async_extent->ram_size - 1;
829
830 p->mapping = inode->i_mapping;
831 tree->ops->writepage_end_io_hook(p, start, end,
832 NULL, 0);
833 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800834 extent_clear_unlock_delalloc(inode, start, end, end,
835 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100836 PAGE_END_WRITEBACK |
837 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100838 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100839 }
Chris Mason771ed682008-11-06 22:02:51 -0500840 alloc_hint = ins.objectid + ins.offset;
841 kfree(async_extent);
842 cond_resched();
843 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100844 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500845out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400846 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400847 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100848out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400849 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500850 async_extent->start +
851 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800852 async_extent->start +
853 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400854 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400855 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
856 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100857 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
858 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100859 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100860 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500861 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500862}
863
Josef Bacik4b46fce2010-05-23 11:00:55 -0400864static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
865 u64 num_bytes)
866{
867 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
868 struct extent_map *em;
869 u64 alloc_hint = 0;
870
871 read_lock(&em_tree->lock);
872 em = search_extent_mapping(em_tree, start, num_bytes);
873 if (em) {
874 /*
875 * if block start isn't an actual block number then find the
876 * first block in this inode and use that as a hint. If that
877 * block is also bogus then just don't worry about it.
878 */
879 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
880 free_extent_map(em);
881 em = search_extent_mapping(em_tree, 0, 0);
882 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
883 alloc_hint = em->block_start;
884 if (em)
885 free_extent_map(em);
886 } else {
887 alloc_hint = em->block_start;
888 free_extent_map(em);
889 }
890 }
891 read_unlock(&em_tree->lock);
892
893 return alloc_hint;
894}
895
Chris Mason771ed682008-11-06 22:02:51 -0500896/*
897 * when extent_io.c finds a delayed allocation range in the file,
898 * the call backs end up in this code. The basic idea is to
899 * allocate extents on disk for the range, and create ordered data structs
900 * in ram to track those extents.
901 *
902 * locked_page is the page that writepage had locked already. We use
903 * it to make sure we don't do extra locks or unlocks.
904 *
905 * *page_started is set to one if we unlock locked_page and do everything
906 * required to start IO on it. It may be clean and already done with
907 * IO when we return.
908 */
Josef Bacik00361582013-08-14 14:02:47 -0400909static noinline int cow_file_range(struct inode *inode,
910 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800911 u64 start, u64 end, u64 delalloc_end,
912 int *page_started, unsigned long *nr_written,
913 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500914{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400915 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400916 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500917 u64 alloc_hint = 0;
918 u64 num_bytes;
919 unsigned long ram_size;
920 u64 disk_num_bytes;
921 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400922 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500923 struct btrfs_key ins;
924 struct extent_map *em;
Chris Mason771ed682008-11-06 22:02:51 -0500925 int ret = 0;
926
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200927 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400928 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500929 ret = -EINVAL;
930 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400931 }
Chris Mason771ed682008-11-06 22:02:51 -0500932
Qu Wenruofda28322013-02-26 08:10:22 +0000933 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500934 num_bytes = max(blocksize, num_bytes);
935 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500936
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200937 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400938
Chris Mason771ed682008-11-06 22:02:51 -0500939 if (start == 0) {
940 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800941 ret = cow_file_range_inline(root, inode, start, end, 0,
942 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500943 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800944 extent_clear_unlock_delalloc(inode, start, end,
945 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400946 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400947 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400948 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
949 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800950 btrfs_free_reserved_data_space_noquota(inode, start,
951 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500952 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300953 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500954 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500955 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100956 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100957 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500958 }
959 }
Chris Masonc8b97812008-10-29 14:49:59 -0400960
961 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400962 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400963
Josef Bacik4b46fce2010-05-23 11:00:55 -0400964 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200965 btrfs_drop_extent_cache(BTRFS_I(inode), start,
966 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400967
Chris Masond3977122009-01-05 21:25:51 -0500968 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400969 unsigned long op;
970
Josef Bacik287a0ab2010-03-19 18:07:23 +0000971 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800972 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400973 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800974 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400975 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100976 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500977
Chris Mason771ed682008-11-06 22:02:51 -0500978 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -0800979 em = create_io_em(inode, start, ins.offset, /* len */
980 start, /* orig_start */
981 ins.objectid, /* block_start */
982 ins.offset, /* block_len */
983 ins.offset, /* orig_block_len */
984 ram_size, /* ram_bytes */
985 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -0800986 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -0800987 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +0000988 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800989 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400990
Chris Mason98d20f62008-04-14 09:46:10 -0400991 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400992 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500993 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000994 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +0100995 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -0400996
Yan Zheng17d217f2008-12-12 10:03:38 -0500997 if (root->root_key.objectid ==
998 BTRFS_DATA_RELOC_TREE_OBJECTID) {
999 ret = btrfs_reloc_clone_csums(inode, start,
1000 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001001 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001002 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001003 }
1004
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001005 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001006
Chris Masond3977122009-01-05 21:25:51 -05001007 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001008 break;
Chris Masond3977122009-01-05 21:25:51 -05001009
Chris Masonc8b97812008-10-29 14:49:59 -04001010 /* we're not doing compressed IO, don't unlock the first
1011 * page (which the caller expects to stay locked), don't
1012 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001013 *
1014 * Do set the Private2 bit so we know this page was properly
1015 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001016 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001017 op = unlock ? PAGE_UNLOCK : 0;
1018 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001019
Josef Bacikc2790a22013-07-29 11:20:47 -04001020 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001021 start + ram_size - 1,
1022 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001023 EXTENT_LOCKED | EXTENT_DELALLOC,
1024 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001025 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001026 num_bytes -= cur_alloc_size;
1027 alloc_hint = ins.objectid + ins.offset;
1028 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -04001029 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001030out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001031 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001032
Filipe Mananad9f85962014-08-25 10:43:00 +01001033out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001034 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001035out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001036 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001037 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001038out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001039 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1040 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001041 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1042 EXTENT_DELALLOC | EXTENT_DEFRAG,
1043 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1044 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001045 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001046}
Chris Masonc8b97812008-10-29 14:49:59 -04001047
Chris Mason771ed682008-11-06 22:02:51 -05001048/*
1049 * work queue call back to started compression on a file and pages
1050 */
1051static noinline void async_cow_start(struct btrfs_work *work)
1052{
1053 struct async_cow *async_cow;
1054 int num_added = 0;
1055 async_cow = container_of(work, struct async_cow, work);
1056
1057 compress_file_range(async_cow->inode, async_cow->locked_page,
1058 async_cow->start, async_cow->end, async_cow,
1059 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001060 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001061 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001062 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001063 }
Chris Mason771ed682008-11-06 22:02:51 -05001064}
1065
1066/*
1067 * work queue call back to submit previously compressed pages
1068 */
1069static noinline void async_cow_submit(struct btrfs_work *work)
1070{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001071 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001072 struct async_cow *async_cow;
1073 struct btrfs_root *root;
1074 unsigned long nr_pages;
1075
1076 async_cow = container_of(work, struct async_cow, work);
1077
1078 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001079 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001080 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1081 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001082
David Sterbaee863952015-02-16 19:41:40 +01001083 /*
1084 * atomic_sub_return implies a barrier for waitqueue_active
1085 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001086 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001087 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001088 waitqueue_active(&fs_info->async_submit_wait))
1089 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001090
Chris Masond3977122009-01-05 21:25:51 -05001091 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001092 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001093}
Chris Masonc8b97812008-10-29 14:49:59 -04001094
Chris Mason771ed682008-11-06 22:02:51 -05001095static noinline void async_cow_free(struct btrfs_work *work)
1096{
1097 struct async_cow *async_cow;
1098 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001099 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001100 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001101 kfree(async_cow);
1102}
1103
1104static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1105 u64 start, u64 end, int *page_started,
1106 unsigned long *nr_written)
1107{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001108 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001109 struct async_cow *async_cow;
1110 struct btrfs_root *root = BTRFS_I(inode)->root;
1111 unsigned long nr_pages;
1112 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001113
Chris Masona3429ab2009-10-08 12:30:20 -04001114 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1115 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001116 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001117 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001118 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001119 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001120 async_cow->root = root;
1121 async_cow->locked_page = locked_page;
1122 async_cow->start = start;
1123
Wang Shilongf79707b2014-07-17 11:44:09 +08001124 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001125 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001126 cur_end = end;
1127 else
Byongho Leeee221842015-12-15 01:42:10 +09001128 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001129
1130 async_cow->end = cur_end;
1131 INIT_LIST_HEAD(&async_cow->extents);
1132
Liu Bo9e0af232014-08-15 23:36:53 +08001133 btrfs_init_work(&async_cow->work,
1134 btrfs_delalloc_helper,
1135 async_cow_start, async_cow_submit,
1136 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001137
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001138 nr_pages = (cur_end - start + PAGE_SIZE) >>
1139 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001140 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001141
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001142 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001143
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001144 while (atomic_read(&fs_info->async_submit_draining) &&
1145 atomic_read(&fs_info->async_delalloc_pages)) {
1146 wait_event(fs_info->async_submit_wait,
1147 (atomic_read(&fs_info->async_delalloc_pages) ==
1148 0));
Chris Mason771ed682008-11-06 22:02:51 -05001149 }
1150
1151 *nr_written += nr_pages;
1152 start = cur_end + 1;
1153 }
1154 *page_started = 1;
1155 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001156}
1157
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001158static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001159 u64 bytenr, u64 num_bytes)
1160{
1161 int ret;
1162 struct btrfs_ordered_sum *sums;
1163 LIST_HEAD(list);
1164
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001165 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001166 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001167 if (ret == 0 && list_empty(&list))
1168 return 0;
1169
1170 while (!list_empty(&list)) {
1171 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1172 list_del(&sums->list);
1173 kfree(sums);
1174 }
1175 return 1;
1176}
1177
Chris Masond352ac62008-09-29 15:18:18 -04001178/*
1179 * when nowcow writeback call back. This checks for snapshots or COW copies
1180 * of the extents that exist in the file, and COWs the file as required.
1181 *
1182 * If no cow copies or snapshots exist, we write directly to the existing
1183 * blocks on disk
1184 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001185static noinline int run_delalloc_nocow(struct inode *inode,
1186 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001187 u64 start, u64 end, int *page_started, int force,
1188 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001189{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001190 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001191 struct btrfs_root *root = BTRFS_I(inode)->root;
1192 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001193 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001194 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001195 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001196 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001197 u64 cow_start;
1198 u64 cur_offset;
1199 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001200 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 u64 disk_bytenr;
1202 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001203 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001204 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001205 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001206 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001207 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 int nocow;
1209 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001210 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001211 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001212
1213 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001214 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001215 extent_clear_unlock_delalloc(inode, start, end, end,
1216 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001217 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001218 EXTENT_DO_ACCOUNTING |
1219 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001220 PAGE_CLEAR_DIRTY |
1221 PAGE_SET_WRITEBACK |
1222 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001223 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001224 }
Li Zefan82d59022011-04-20 10:33:24 +08001225
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001226 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001227
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 cow_start = (u64)-1;
1229 cur_offset = start;
1230 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001231 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001232 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001233 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001234 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001235 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1236 leaf = path->nodes[0];
1237 btrfs_item_key_to_cpu(leaf, &found_key,
1238 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001239 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 found_key.type == BTRFS_EXTENT_DATA_KEY)
1241 path->slots[0]--;
1242 }
1243 check_prev = 0;
1244next_slot:
1245 leaf = path->nodes[0];
1246 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1247 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001248 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001249 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001250 if (ret > 0)
1251 break;
1252 leaf = path->nodes[0];
1253 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001254
Yan Zheng80ff3852008-10-30 14:20:02 -04001255 nocow = 0;
1256 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001257 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001259
Filipe Manana1d512cb2015-11-09 00:33:58 +00001260 if (found_key.objectid > ino)
1261 break;
1262 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1263 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1264 path->slots[0]++;
1265 goto next_slot;
1266 }
1267 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 found_key.offset > end)
1269 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001270
Yan Zheng80ff3852008-10-30 14:20:02 -04001271 if (found_key.offset > cur_offset) {
1272 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001273 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 goto out_check;
1275 }
Chris Masonc31f8832008-01-08 15:46:31 -05001276
Yan Zheng80ff3852008-10-30 14:20:02 -04001277 fi = btrfs_item_ptr(leaf, path->slots[0],
1278 struct btrfs_file_extent_item);
1279 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001280
Josef Bacikcc95bef2013-04-04 14:31:27 -04001281 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001282 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1283 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001285 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 extent_end = found_key.offset +
1287 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001288 disk_num_bytes =
1289 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 if (extent_end <= start) {
1291 path->slots[0]++;
1292 goto next_slot;
1293 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001294 if (disk_bytenr == 0)
1295 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 if (btrfs_file_extent_compression(leaf, fi) ||
1297 btrfs_file_extent_encryption(leaf, fi) ||
1298 btrfs_file_extent_other_encoding(leaf, fi))
1299 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001300 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1301 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001302 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001304 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001305 found_key.offset -
1306 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001307 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001308 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001309 disk_bytenr += cur_offset - found_key.offset;
1310 num_bytes = min(end + 1, extent_end) - cur_offset;
1311 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001312 * if there are pending snapshots for this root,
1313 * we fall into common COW way.
1314 */
1315 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001316 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001317 if (!err)
1318 goto out_check;
1319 }
1320 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001321 * force cow if csum exists in the range.
1322 * this ensure that csum for a given extent are
1323 * either valid or do not exist.
1324 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001325 if (csum_exist_in_range(fs_info, disk_bytenr,
1326 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001327 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001328 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001329 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001330 nocow = 1;
1331 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1332 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001333 btrfs_file_extent_inline_len(leaf,
1334 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001335 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001336 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001337 } else {
1338 BUG_ON(1);
1339 }
1340out_check:
1341 if (extent_end <= start) {
1342 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001343 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001344 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001345 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001346 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 goto next_slot;
1348 }
1349 if (!nocow) {
1350 if (cow_start == (u64)-1)
1351 cow_start = cur_offset;
1352 cur_offset = extent_end;
1353 if (cur_offset > end)
1354 break;
1355 path->slots[0]++;
1356 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001357 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001358
David Sterbab3b4aa72011-04-21 01:20:15 +02001359 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001361 ret = cow_file_range(inode, locked_page,
1362 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001363 end, page_started, nr_written, 1,
1364 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001365 if (ret) {
1366 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001367 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001368 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001369 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001370 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001371 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001372 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001374 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001375
Yan Zhengd899e052008-10-30 14:25:28 -04001376 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001377 u64 orig_start = found_key.offset - extent_offset;
1378
1379 em = create_io_em(inode, cur_offset, num_bytes,
1380 orig_start,
1381 disk_bytenr, /* block_start */
1382 num_bytes, /* block_len */
1383 disk_num_bytes, /* orig_block_len */
1384 ram_bytes, BTRFS_COMPRESS_NONE,
1385 BTRFS_ORDERED_PREALLOC);
1386 if (IS_ERR(em)) {
1387 if (!nolock && nocow)
1388 btrfs_end_write_no_snapshoting(root);
1389 if (nocow)
1390 btrfs_dec_nocow_writers(fs_info,
1391 disk_bytenr);
1392 ret = PTR_ERR(em);
1393 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001394 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001395 free_extent_map(em);
1396 }
1397
1398 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001399 type = BTRFS_ORDERED_PREALLOC;
1400 } else {
1401 type = BTRFS_ORDERED_NOCOW;
1402 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001403
1404 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001405 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001406 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001407 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001408 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001409
Yan, Zhengefa56462010-05-16 10:49:59 -04001410 if (root->root_key.objectid ==
1411 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1412 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1413 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001414 if (ret) {
1415 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001416 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001417 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001418 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001419 }
1420
Josef Bacikc2790a22013-07-29 11:20:47 -04001421 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001422 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001423 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001424 EXTENT_DELALLOC |
1425 EXTENT_CLEAR_DATA_RESV,
1426 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1427
Wang Shilonge9894fd2014-03-27 11:12:25 +08001428 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001429 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001430 cur_offset = extent_end;
1431 if (cur_offset > end)
1432 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001433 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001434 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001435
Josef Bacik17ca04a2012-05-31 15:58:55 -04001436 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001438 cur_offset = end;
1439 }
1440
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001442 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1443 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001444 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001445 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001446 }
1447
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001448error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001449 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001450 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001451 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001452 EXTENT_DELALLOC | EXTENT_DEFRAG |
1453 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1454 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001455 PAGE_SET_WRITEBACK |
1456 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001457 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001458 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001459}
1460
Wang Shilong47059d92014-07-03 18:22:07 +08001461static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1462{
1463
1464 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1465 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1466 return 0;
1467
1468 /*
1469 * @defrag_bytes is a hint value, no spinlock held here,
1470 * if is not zero, it means the file is defragging.
1471 * Force cow if given extent needs to be defragged.
1472 */
1473 if (BTRFS_I(inode)->defrag_bytes &&
1474 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1475 EXTENT_DEFRAG, 0, NULL))
1476 return 1;
1477
1478 return 0;
1479}
1480
Chris Masond352ac62008-09-29 15:18:18 -04001481/*
1482 * extent_io.c call back to do delayed allocation processing
1483 */
Chris Masonc8b97812008-10-29 14:49:59 -04001484static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001485 u64 start, u64 end, int *page_started,
1486 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001487{
Chris Masonbe20aa92007-12-17 20:14:01 -05001488 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001489 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001490
Wang Shilong47059d92014-07-03 18:22:07 +08001491 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001492 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001493 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001494 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001495 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001496 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001497 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001498 ret = cow_file_range(inode, locked_page, start, end, end,
1499 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001500 } else {
1501 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1502 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001503 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001504 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001505 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001506 return ret;
1507}
1508
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001509static void btrfs_split_extent_hook(struct inode *inode,
1510 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001511{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001512 u64 size;
1513
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001514 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001515 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001516 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001517
Josef Bacikdcab6a32015-02-11 15:08:59 -05001518 size = orig->end - orig->start + 1;
1519 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001520 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001521 u64 new_size;
1522
1523 /*
Josef Bacikba117212015-03-13 15:01:24 -04001524 * See the explanation in btrfs_merge_extent_hook, the same
1525 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001526 */
1527 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001528 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001529 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001530 num_extents += count_max_extents(new_size);
1531 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001532 return;
1533 }
1534
Josef Bacik9e0baf62011-07-15 15:16:44 +00001535 spin_lock(&BTRFS_I(inode)->lock);
1536 BTRFS_I(inode)->outstanding_extents++;
1537 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001538}
1539
1540/*
1541 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1542 * extents so we can keep track of new extents that are just merged onto old
1543 * extents, such as when we are doing sequential writes, so we can properly
1544 * account for the metadata space we'll need.
1545 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001546static void btrfs_merge_extent_hook(struct inode *inode,
1547 struct extent_state *new,
1548 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001549{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001550 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001551 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001552
Josef Bacik9ed74f22009-09-11 16:12:44 -04001553 /* not delalloc, ignore it */
1554 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001555 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001556
Josef Bacik8461a3d2015-03-13 15:12:08 -04001557 if (new->start > other->start)
1558 new_size = new->end - other->start + 1;
1559 else
1560 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001561
1562 /* we're not bigger than the max, unreserve the space and go */
1563 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1564 spin_lock(&BTRFS_I(inode)->lock);
1565 BTRFS_I(inode)->outstanding_extents--;
1566 spin_unlock(&BTRFS_I(inode)->lock);
1567 return;
1568 }
1569
1570 /*
Josef Bacikba117212015-03-13 15:01:24 -04001571 * We have to add up either side to figure out how many extents were
1572 * accounted for before we merged into one big extent. If the number of
1573 * extents we accounted for is <= the amount we need for the new range
1574 * then we can return, otherwise drop. Think of it like this
1575 *
1576 * [ 4k][MAX_SIZE]
1577 *
1578 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1579 * need 2 outstanding extents, on one side we have 1 and the other side
1580 * we have 1 so they are == and we can return. But in this case
1581 *
1582 * [MAX_SIZE+4k][MAX_SIZE+4k]
1583 *
1584 * Each range on their own accounts for 2 extents, but merged together
1585 * they are only 3 extents worth of accounting, so we need to drop in
1586 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001587 */
Josef Bacikba117212015-03-13 15:01:24 -04001588 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001589 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001590 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001591 num_extents += count_max_extents(old_size);
1592 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001593 return;
1594
Josef Bacik9e0baf62011-07-15 15:16:44 +00001595 spin_lock(&BTRFS_I(inode)->lock);
1596 BTRFS_I(inode)->outstanding_extents--;
1597 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001598}
1599
Miao Xieeb73c1b2013-05-15 07:48:22 +00001600static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1601 struct inode *inode)
1602{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001603 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1604
Miao Xieeb73c1b2013-05-15 07:48:22 +00001605 spin_lock(&root->delalloc_lock);
1606 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1607 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1608 &root->delalloc_inodes);
1609 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1610 &BTRFS_I(inode)->runtime_flags);
1611 root->nr_delalloc_inodes++;
1612 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001613 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001614 BUG_ON(!list_empty(&root->delalloc_root));
1615 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001616 &fs_info->delalloc_roots);
1617 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001618 }
1619 }
1620 spin_unlock(&root->delalloc_lock);
1621}
1622
1623static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001624 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001625{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001626 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001627
Miao Xieeb73c1b2013-05-15 07:48:22 +00001628 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001629 if (!list_empty(&inode->delalloc_inodes)) {
1630 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001631 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001632 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001633 root->nr_delalloc_inodes--;
1634 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001635 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001636 BUG_ON(list_empty(&root->delalloc_root));
1637 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001638 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001639 }
1640 }
1641 spin_unlock(&root->delalloc_lock);
1642}
1643
Chris Masond352ac62008-09-29 15:18:18 -04001644/*
1645 * extent_io.c set_bit_hook, used to track delayed allocation
1646 * bytes in this file, and to maintain the list of inodes that
1647 * have pending delalloc work to be done.
1648 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001649static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001650 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001651{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001652
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001653 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1654
Wang Shilong47059d92014-07-03 18:22:07 +08001655 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1656 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001657 /*
1658 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001659 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001660 * bit, which is only set or cleared with irqs on
1661 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001662 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001663 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001664 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001665 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001666
Josef Bacik9e0baf62011-07-15 15:16:44 +00001667 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001668 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001669 } else {
1670 spin_lock(&BTRFS_I(inode)->lock);
1671 BTRFS_I(inode)->outstanding_extents++;
1672 spin_unlock(&BTRFS_I(inode)->lock);
1673 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001674
Josef Bacik6a3891c2015-03-16 17:38:52 -04001675 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001676 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001677 return;
1678
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001679 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1680 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001681 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001682 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001683 if (*bits & EXTENT_DEFRAG)
1684 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001685 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001686 &BTRFS_I(inode)->runtime_flags))
1687 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001688 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001689 }
Chris Mason291d6732008-01-29 15:55:23 -05001690}
1691
Chris Masond352ac62008-09-29 15:18:18 -04001692/*
1693 * extent_io.c clear_bit_hook, see set_bit_hook for why
1694 */
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001695static void btrfs_clear_bit_hook(struct btrfs_inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001696 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001697 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001698{
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001699 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001700 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001701 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001702
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001703 spin_lock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001704 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001705 inode->defrag_bytes -= len;
1706 spin_unlock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001707
Chris Mason75eff682008-12-15 15:54:40 -05001708 /*
1709 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001710 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001711 * bit, which is only set or cleared with irqs on
1712 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001713 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001714 struct btrfs_root *root = inode->root;
1715 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001716
Josef Bacik9e0baf62011-07-15 15:16:44 +00001717 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001718 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001719 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001720 spin_lock(&inode->lock);
1721 inode->outstanding_extents -= num_extents;
1722 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001723 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001724
Josef Bacikb6d08f02013-09-27 14:57:43 -04001725 /*
1726 * We don't reserve metadata space for space cache inodes so we
1727 * don't need to call dellalloc_release_metadata if there is an
1728 * error.
1729 */
1730 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001731 root != fs_info->tree_root)
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001732 btrfs_delalloc_release_metadata(inode, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001733
Josef Bacik6a3891c2015-03-16 17:38:52 -04001734 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001735 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001736 return;
1737
Josef Bacik0cb59c92010-07-02 12:14:14 -04001738 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001739 && do_list && !(state->state & EXTENT_NORESERVE)
1740 && (*bits & (EXTENT_DO_ACCOUNTING |
1741 EXTENT_CLEAR_DATA_RESV)))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001742 btrfs_free_reserved_data_space_noquota(
1743 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001744 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001745
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001746 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1747 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001748 spin_lock(&inode->lock);
1749 inode->delalloc_bytes -= len;
1750 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001751 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001752 &inode->runtime_flags))
1753 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001754 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001755 }
Chris Mason291d6732008-01-29 15:55:23 -05001756}
1757
Chris Masond352ac62008-09-29 15:18:18 -04001758/*
1759 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1760 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001761 *
1762 * return 1 if page cannot be merged to bio
1763 * return 0 if page can be merged to bio
1764 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001765 */
Mike Christie81a75f672016-06-05 14:31:54 -05001766int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001767 size_t size, struct bio *bio,
1768 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001769{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001770 struct inode *inode = page->mapping->host;
1771 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001772 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001773 u64 length = 0;
1774 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001775 int ret;
1776
Chris Mason771ed682008-11-06 22:02:51 -05001777 if (bio_flags & EXTENT_BIO_COMPRESSED)
1778 return 0;
1779
Kent Overstreet4f024f32013-10-11 15:44:27 -07001780 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001781 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001782 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1783 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001784 if (ret < 0)
1785 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001786 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001787 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001788 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001789}
1790
Chris Masond352ac62008-09-29 15:18:18 -04001791/*
1792 * in order to insert checksums into the metadata in large chunks,
1793 * we wait until bio submission time. All the pages in the bio are
1794 * checksummed and sums are attached onto the ordered extent record.
1795 *
1796 * At IO completion time the cums attached on the ordered extent record
1797 * are inserted into the btree
1798 */
Mike Christie81a75f672016-06-05 14:31:54 -05001799static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1800 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001801 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001802{
Chris Mason065631f2008-02-20 12:07:25 -05001803 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001804
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001805 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001806 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001807 return 0;
1808}
Chris Masone0156402008-04-16 11:15:20 -04001809
Chris Mason4a69a412008-11-06 22:03:00 -05001810/*
1811 * in order to insert checksums into the metadata in large chunks,
1812 * we wait until bio submission time. All the pages in the bio are
1813 * checksummed and sums are attached onto the ordered extent record.
1814 *
1815 * At IO completion time the cums attached on the ordered extent record
1816 * are inserted into the btree
1817 */
Mike Christie81a75f672016-06-05 14:31:54 -05001818static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001819 int mirror_num, unsigned long bio_flags,
1820 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001821{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001822 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001823 int ret;
1824
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001825 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001826 if (ret) {
1827 bio->bi_error = ret;
1828 bio_endio(bio);
1829 }
Stefan Behrens61891922012-11-05 18:51:52 +01001830 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001831}
1832
Chris Masond352ac62008-09-29 15:18:18 -04001833/*
Chris Masoncad321a2008-12-17 14:51:42 -05001834 * extent_io.c submission hook. This does the right thing for csum calculation
1835 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001836 */
Mike Christie81a75f672016-06-05 14:31:54 -05001837static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001838 int mirror_num, unsigned long bio_flags,
1839 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001840{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001841 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001842 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301843 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001844 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001845 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001846 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001847
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001848 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001849
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001850 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301851 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001852
Mike Christie37226b22016-06-05 14:31:52 -05001853 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001854 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001855 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001856 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001857
Chris Masond20f7042008-12-08 16:58:54 -05001858 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001859 ret = btrfs_submit_compressed_read(inode, bio,
1860 mirror_num,
1861 bio_flags);
1862 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001863 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001864 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001865 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001866 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001867 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001868 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001869 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001870 /* csum items have already been cloned */
1871 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1872 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001873 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001874 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1875 bio_flags, bio_offset,
1876 __btrfs_submit_bio_start,
1877 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001878 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001879 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001880 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001881 if (ret)
1882 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001883 }
1884
Chris Mason0b86a832008-03-24 15:01:56 -04001885mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001886 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001887
1888out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001889 if (ret < 0) {
1890 bio->bi_error = ret;
1891 bio_endio(bio);
1892 }
Stefan Behrens61891922012-11-05 18:51:52 +01001893 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001894}
Chris Mason6885f302008-02-20 16:11:05 -05001895
Chris Masond352ac62008-09-29 15:18:18 -04001896/*
1897 * given a list of ordered sums record them in the inode. This happens
1898 * at IO completion time based on sums calculated at bio submission time.
1899 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001900static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01001901 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001902{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001903 struct btrfs_ordered_sum *sum;
1904
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001905 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001906 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001907 btrfs_csum_file_blocks(trans,
1908 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001909 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001910 }
1911 return 0;
1912}
1913
Josef Bacik2ac55d42010-02-03 19:33:23 +00001914int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001915 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04001916{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001917 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001918 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001919 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001920}
1921
Chris Masond352ac62008-09-29 15:18:18 -04001922/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001923struct btrfs_writepage_fixup {
1924 struct page *page;
1925 struct btrfs_work work;
1926};
1927
Christoph Hellwigb2950862008-12-02 09:54:17 -05001928static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001929{
1930 struct btrfs_writepage_fixup *fixup;
1931 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001932 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001933 struct page *page;
1934 struct inode *inode;
1935 u64 page_start;
1936 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001937 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001938
1939 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1940 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001941again:
Chris Mason247e7432008-07-17 12:53:51 -04001942 lock_page(page);
1943 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1944 ClearPageChecked(page);
1945 goto out_page;
1946 }
1947
1948 inode = page->mapping->host;
1949 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001950 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04001951
David Sterbaff13db42015-12-03 14:30:40 +01001952 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001953 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001954
1955 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001956 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001957 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001958
Nikolay Borisova776c6f2017-02-20 13:50:49 +02001959 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001960 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04001961 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001962 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1963 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001964 unlock_page(page);
1965 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001966 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001967 goto again;
1968 }
Chris Mason247e7432008-07-17 12:53:51 -04001969
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001970 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001971 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001972 if (ret) {
1973 mapping_set_error(page->mapping, ret);
1974 end_extent_writepage(page, ret, page_start, page_end);
1975 ClearPageChecked(page);
1976 goto out;
1977 }
1978
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001979 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
1980 0);
Chris Mason247e7432008-07-17 12:53:51 -04001981 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001982 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001983out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001984 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1985 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001986out_page:
1987 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001988 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001989 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001990}
1991
1992/*
1993 * There are a few paths in the higher layers of the kernel that directly
1994 * set the page dirty bit without asking the filesystem if it is a
1995 * good idea. This causes problems because we want to make sure COW
1996 * properly happens and the data=ordered rules are followed.
1997 *
Chris Masonc8b97812008-10-29 14:49:59 -04001998 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001999 * hasn't been properly setup for IO. We kick off an async process
2000 * to fix it up. The async helper will wait for ordered extents, set
2001 * the delalloc bit and make it safe to write the page.
2002 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002003static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002004{
2005 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002006 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002007 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002008
Chris Mason8b62b722009-09-02 16:53:46 -04002009 /* this page is properly in the ordered list */
2010 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002011 return 0;
2012
2013 if (PageChecked(page))
2014 return -EAGAIN;
2015
2016 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2017 if (!fixup)
2018 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002019
Chris Mason247e7432008-07-17 12:53:51 -04002020 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002021 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002022 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2023 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002024 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002025 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002026 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002027}
2028
Yan Zhengd899e052008-10-30 14:25:28 -04002029static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2030 struct inode *inode, u64 file_pos,
2031 u64 disk_bytenr, u64 disk_num_bytes,
2032 u64 num_bytes, u64 ram_bytes,
2033 u8 compression, u8 encryption,
2034 u16 other_encoding, int extent_type)
2035{
2036 struct btrfs_root *root = BTRFS_I(inode)->root;
2037 struct btrfs_file_extent_item *fi;
2038 struct btrfs_path *path;
2039 struct extent_buffer *leaf;
2040 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002041 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002042 int ret;
2043
2044 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002045 if (!path)
2046 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002047
Chris Masona1ed8352009-09-11 12:27:37 -04002048 /*
2049 * we may be replacing one extent in the tree with another.
2050 * The new extent is pinned in the extent map, and we don't want
2051 * to drop it from the cache until it is completely in the btree.
2052 *
2053 * So, tell btrfs_drop_extents to leave this extent in the cache.
2054 * the caller is expected to unpin it and allow it to be merged
2055 * with the others.
2056 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002057 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2058 file_pos + num_bytes, NULL, 0,
2059 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002060 if (ret)
2061 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002062
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002063 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002064 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002065 ins.offset = file_pos;
2066 ins.type = BTRFS_EXTENT_DATA_KEY;
2067
2068 path->leave_spinning = 1;
2069 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2070 sizeof(*fi));
2071 if (ret)
2072 goto out;
2073 }
Yan Zhengd899e052008-10-30 14:25:28 -04002074 leaf = path->nodes[0];
2075 fi = btrfs_item_ptr(leaf, path->slots[0],
2076 struct btrfs_file_extent_item);
2077 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2078 btrfs_set_file_extent_type(leaf, fi, extent_type);
2079 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2080 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2081 btrfs_set_file_extent_offset(leaf, fi, 0);
2082 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2083 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2084 btrfs_set_file_extent_compression(leaf, fi, compression);
2085 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2086 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002087
Yan Zhengd899e052008-10-30 14:25:28 -04002088 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002089 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002090
2091 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002092
2093 ins.objectid = disk_bytenr;
2094 ins.offset = disk_num_bytes;
2095 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002096 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002097 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002098 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002099 * Release the reserved range from inode dirty range map, as it is
2100 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002101 */
2102 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002103out:
Yan Zhengd899e052008-10-30 14:25:28 -04002104 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002105
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002106 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002107}
2108
Liu Bo38c227d2013-01-29 03:18:40 +00002109/* snapshot-aware defrag */
2110struct sa_defrag_extent_backref {
2111 struct rb_node node;
2112 struct old_sa_defrag_extent *old;
2113 u64 root_id;
2114 u64 inum;
2115 u64 file_pos;
2116 u64 extent_offset;
2117 u64 num_bytes;
2118 u64 generation;
2119};
2120
2121struct old_sa_defrag_extent {
2122 struct list_head list;
2123 struct new_sa_defrag_extent *new;
2124
2125 u64 extent_offset;
2126 u64 bytenr;
2127 u64 offset;
2128 u64 len;
2129 int count;
2130};
2131
2132struct new_sa_defrag_extent {
2133 struct rb_root root;
2134 struct list_head head;
2135 struct btrfs_path *path;
2136 struct inode *inode;
2137 u64 file_pos;
2138 u64 len;
2139 u64 bytenr;
2140 u64 disk_len;
2141 u8 compress_type;
2142};
2143
2144static int backref_comp(struct sa_defrag_extent_backref *b1,
2145 struct sa_defrag_extent_backref *b2)
2146{
2147 if (b1->root_id < b2->root_id)
2148 return -1;
2149 else if (b1->root_id > b2->root_id)
2150 return 1;
2151
2152 if (b1->inum < b2->inum)
2153 return -1;
2154 else if (b1->inum > b2->inum)
2155 return 1;
2156
2157 if (b1->file_pos < b2->file_pos)
2158 return -1;
2159 else if (b1->file_pos > b2->file_pos)
2160 return 1;
2161
2162 /*
2163 * [------------------------------] ===> (a range of space)
2164 * |<--->| |<---->| =============> (fs/file tree A)
2165 * |<---------------------------->| ===> (fs/file tree B)
2166 *
2167 * A range of space can refer to two file extents in one tree while
2168 * refer to only one file extent in another tree.
2169 *
2170 * So we may process a disk offset more than one time(two extents in A)
2171 * and locate at the same extent(one extent in B), then insert two same
2172 * backrefs(both refer to the extent in B).
2173 */
2174 return 0;
2175}
2176
2177static void backref_insert(struct rb_root *root,
2178 struct sa_defrag_extent_backref *backref)
2179{
2180 struct rb_node **p = &root->rb_node;
2181 struct rb_node *parent = NULL;
2182 struct sa_defrag_extent_backref *entry;
2183 int ret;
2184
2185 while (*p) {
2186 parent = *p;
2187 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2188
2189 ret = backref_comp(backref, entry);
2190 if (ret < 0)
2191 p = &(*p)->rb_left;
2192 else
2193 p = &(*p)->rb_right;
2194 }
2195
2196 rb_link_node(&backref->node, parent, p);
2197 rb_insert_color(&backref->node, root);
2198}
2199
2200/*
2201 * Note the backref might has changed, and in this case we just return 0.
2202 */
2203static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2204 void *ctx)
2205{
2206 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002207 struct old_sa_defrag_extent *old = ctx;
2208 struct new_sa_defrag_extent *new = old->new;
2209 struct btrfs_path *path = new->path;
2210 struct btrfs_key key;
2211 struct btrfs_root *root;
2212 struct sa_defrag_extent_backref *backref;
2213 struct extent_buffer *leaf;
2214 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002215 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002216 int slot;
2217 int ret;
2218 u64 extent_offset;
2219 u64 num_bytes;
2220
2221 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002222 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002223 return 0;
2224
2225 key.objectid = root_id;
2226 key.type = BTRFS_ROOT_ITEM_KEY;
2227 key.offset = (u64)-1;
2228
Liu Bo38c227d2013-01-29 03:18:40 +00002229 root = btrfs_read_fs_root_no_name(fs_info, &key);
2230 if (IS_ERR(root)) {
2231 if (PTR_ERR(root) == -ENOENT)
2232 return 0;
2233 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002234 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002235 inum, offset, root_id);
2236 return PTR_ERR(root);
2237 }
2238
2239 key.objectid = inum;
2240 key.type = BTRFS_EXTENT_DATA_KEY;
2241 if (offset > (u64)-1 << 32)
2242 key.offset = 0;
2243 else
2244 key.offset = offset;
2245
2246 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302247 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002248 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002249 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002250
2251 while (1) {
2252 cond_resched();
2253
2254 leaf = path->nodes[0];
2255 slot = path->slots[0];
2256
2257 if (slot >= btrfs_header_nritems(leaf)) {
2258 ret = btrfs_next_leaf(root, path);
2259 if (ret < 0) {
2260 goto out;
2261 } else if (ret > 0) {
2262 ret = 0;
2263 goto out;
2264 }
2265 continue;
2266 }
2267
2268 path->slots[0]++;
2269
2270 btrfs_item_key_to_cpu(leaf, &key, slot);
2271
2272 if (key.objectid > inum)
2273 goto out;
2274
2275 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2276 continue;
2277
2278 extent = btrfs_item_ptr(leaf, slot,
2279 struct btrfs_file_extent_item);
2280
2281 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2282 continue;
2283
Liu Boe68afa42013-07-01 22:13:26 +08002284 /*
2285 * 'offset' refers to the exact key.offset,
2286 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2287 * (key.offset - extent_offset).
2288 */
2289 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002290 continue;
2291
Liu Boe68afa42013-07-01 22:13:26 +08002292 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002293 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002294
Liu Bo38c227d2013-01-29 03:18:40 +00002295 if (extent_offset >= old->extent_offset + old->offset +
2296 old->len || extent_offset + num_bytes <=
2297 old->extent_offset + old->offset)
2298 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002299 break;
2300 }
2301
2302 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2303 if (!backref) {
2304 ret = -ENOENT;
2305 goto out;
2306 }
2307
2308 backref->root_id = root_id;
2309 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002310 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002311 backref->num_bytes = num_bytes;
2312 backref->extent_offset = extent_offset;
2313 backref->generation = btrfs_file_extent_generation(leaf, extent);
2314 backref->old = old;
2315 backref_insert(&new->root, backref);
2316 old->count++;
2317out:
2318 btrfs_release_path(path);
2319 WARN_ON(ret);
2320 return ret;
2321}
2322
2323static noinline bool record_extent_backrefs(struct btrfs_path *path,
2324 struct new_sa_defrag_extent *new)
2325{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002326 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002327 struct old_sa_defrag_extent *old, *tmp;
2328 int ret;
2329
2330 new->path = path;
2331
2332 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002333 ret = iterate_inodes_from_logical(old->bytenr +
2334 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002335 path, record_one_backref,
2336 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002337 if (ret < 0 && ret != -ENOENT)
2338 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002339
2340 /* no backref to be processed for this extent */
2341 if (!old->count) {
2342 list_del(&old->list);
2343 kfree(old);
2344 }
2345 }
2346
2347 if (list_empty(&new->head))
2348 return false;
2349
2350 return true;
2351}
2352
2353static int relink_is_mergable(struct extent_buffer *leaf,
2354 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002355 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002356{
Liu Bo116e0022013-08-02 16:30:40 +08002357 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002358 return 0;
2359
2360 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2361 return 0;
2362
Liu Bo116e0022013-08-02 16:30:40 +08002363 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2364 return 0;
2365
2366 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002367 btrfs_file_extent_other_encoding(leaf, fi))
2368 return 0;
2369
2370 return 1;
2371}
2372
2373/*
2374 * Note the backref might has changed, and in this case we just return 0.
2375 */
2376static noinline int relink_extent_backref(struct btrfs_path *path,
2377 struct sa_defrag_extent_backref *prev,
2378 struct sa_defrag_extent_backref *backref)
2379{
2380 struct btrfs_file_extent_item *extent;
2381 struct btrfs_file_extent_item *item;
2382 struct btrfs_ordered_extent *ordered;
2383 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002384 struct btrfs_root *root;
2385 struct btrfs_key key;
2386 struct extent_buffer *leaf;
2387 struct old_sa_defrag_extent *old = backref->old;
2388 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002389 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002390 struct inode *inode;
2391 struct extent_state *cached = NULL;
2392 int ret = 0;
2393 u64 start;
2394 u64 len;
2395 u64 lock_start;
2396 u64 lock_end;
2397 bool merge = false;
2398 int index;
2399
2400 if (prev && prev->root_id == backref->root_id &&
2401 prev->inum == backref->inum &&
2402 prev->file_pos + prev->num_bytes == backref->file_pos)
2403 merge = true;
2404
2405 /* step 1: get root */
2406 key.objectid = backref->root_id;
2407 key.type = BTRFS_ROOT_ITEM_KEY;
2408 key.offset = (u64)-1;
2409
Liu Bo38c227d2013-01-29 03:18:40 +00002410 index = srcu_read_lock(&fs_info->subvol_srcu);
2411
2412 root = btrfs_read_fs_root_no_name(fs_info, &key);
2413 if (IS_ERR(root)) {
2414 srcu_read_unlock(&fs_info->subvol_srcu, index);
2415 if (PTR_ERR(root) == -ENOENT)
2416 return 0;
2417 return PTR_ERR(root);
2418 }
Liu Bo38c227d2013-01-29 03:18:40 +00002419
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002420 if (btrfs_root_readonly(root)) {
2421 srcu_read_unlock(&fs_info->subvol_srcu, index);
2422 return 0;
2423 }
2424
Liu Bo38c227d2013-01-29 03:18:40 +00002425 /* step 2: get inode */
2426 key.objectid = backref->inum;
2427 key.type = BTRFS_INODE_ITEM_KEY;
2428 key.offset = 0;
2429
2430 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2431 if (IS_ERR(inode)) {
2432 srcu_read_unlock(&fs_info->subvol_srcu, index);
2433 return 0;
2434 }
2435
2436 srcu_read_unlock(&fs_info->subvol_srcu, index);
2437
2438 /* step 3: relink backref */
2439 lock_start = backref->file_pos;
2440 lock_end = backref->file_pos + backref->num_bytes - 1;
2441 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002442 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002443
2444 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2445 if (ordered) {
2446 btrfs_put_ordered_extent(ordered);
2447 goto out_unlock;
2448 }
2449
2450 trans = btrfs_join_transaction(root);
2451 if (IS_ERR(trans)) {
2452 ret = PTR_ERR(trans);
2453 goto out_unlock;
2454 }
2455
2456 key.objectid = backref->inum;
2457 key.type = BTRFS_EXTENT_DATA_KEY;
2458 key.offset = backref->file_pos;
2459
2460 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2461 if (ret < 0) {
2462 goto out_free_path;
2463 } else if (ret > 0) {
2464 ret = 0;
2465 goto out_free_path;
2466 }
2467
2468 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2469 struct btrfs_file_extent_item);
2470
2471 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2472 backref->generation)
2473 goto out_free_path;
2474
2475 btrfs_release_path(path);
2476
2477 start = backref->file_pos;
2478 if (backref->extent_offset < old->extent_offset + old->offset)
2479 start += old->extent_offset + old->offset -
2480 backref->extent_offset;
2481
2482 len = min(backref->extent_offset + backref->num_bytes,
2483 old->extent_offset + old->offset + old->len);
2484 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2485
2486 ret = btrfs_drop_extents(trans, root, inode, start,
2487 start + len, 1);
2488 if (ret)
2489 goto out_free_path;
2490again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002491 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002492 key.type = BTRFS_EXTENT_DATA_KEY;
2493 key.offset = start;
2494
Liu Boa09a0a72013-03-11 09:20:58 +00002495 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002496 if (merge) {
2497 struct btrfs_file_extent_item *fi;
2498 u64 extent_len;
2499 struct btrfs_key found_key;
2500
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002501 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002502 if (ret < 0)
2503 goto out_free_path;
2504
2505 path->slots[0]--;
2506 leaf = path->nodes[0];
2507 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2508
2509 fi = btrfs_item_ptr(leaf, path->slots[0],
2510 struct btrfs_file_extent_item);
2511 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2512
Liu Bo116e0022013-08-02 16:30:40 +08002513 if (extent_len + found_key.offset == start &&
2514 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002515 btrfs_set_file_extent_num_bytes(leaf, fi,
2516 extent_len + len);
2517 btrfs_mark_buffer_dirty(leaf);
2518 inode_add_bytes(inode, len);
2519
2520 ret = 1;
2521 goto out_free_path;
2522 } else {
2523 merge = false;
2524 btrfs_release_path(path);
2525 goto again;
2526 }
2527 }
2528
2529 ret = btrfs_insert_empty_item(trans, root, path, &key,
2530 sizeof(*extent));
2531 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002532 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002533 goto out_free_path;
2534 }
2535
2536 leaf = path->nodes[0];
2537 item = btrfs_item_ptr(leaf, path->slots[0],
2538 struct btrfs_file_extent_item);
2539 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2540 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2541 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2542 btrfs_set_file_extent_num_bytes(leaf, item, len);
2543 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2544 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2545 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2546 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2547 btrfs_set_file_extent_encryption(leaf, item, 0);
2548 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2549
2550 btrfs_mark_buffer_dirty(leaf);
2551 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002552 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002553
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002554 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002555 new->disk_len, 0,
2556 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002557 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002558 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002559 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002560 goto out_free_path;
2561 }
2562
2563 ret = 1;
2564out_free_path:
2565 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002566 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002567 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002568out_unlock:
2569 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2570 &cached, GFP_NOFS);
2571 iput(inode);
2572 return ret;
2573}
2574
Liu Bo6f519562013-10-29 10:45:05 +08002575static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2576{
2577 struct old_sa_defrag_extent *old, *tmp;
2578
2579 if (!new)
2580 return;
2581
2582 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002583 kfree(old);
2584 }
2585 kfree(new);
2586}
2587
Liu Bo38c227d2013-01-29 03:18:40 +00002588static void relink_file_extents(struct new_sa_defrag_extent *new)
2589{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002590 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002591 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002592 struct sa_defrag_extent_backref *backref;
2593 struct sa_defrag_extent_backref *prev = NULL;
2594 struct inode *inode;
2595 struct btrfs_root *root;
2596 struct rb_node *node;
2597 int ret;
2598
2599 inode = new->inode;
2600 root = BTRFS_I(inode)->root;
2601
2602 path = btrfs_alloc_path();
2603 if (!path)
2604 return;
2605
2606 if (!record_extent_backrefs(path, new)) {
2607 btrfs_free_path(path);
2608 goto out;
2609 }
2610 btrfs_release_path(path);
2611
2612 while (1) {
2613 node = rb_first(&new->root);
2614 if (!node)
2615 break;
2616 rb_erase(node, &new->root);
2617
2618 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2619
2620 ret = relink_extent_backref(path, prev, backref);
2621 WARN_ON(ret < 0);
2622
2623 kfree(prev);
2624
2625 if (ret == 1)
2626 prev = backref;
2627 else
2628 prev = NULL;
2629 cond_resched();
2630 }
2631 kfree(prev);
2632
2633 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002634out:
Liu Bo6f519562013-10-29 10:45:05 +08002635 free_sa_defrag_extent(new);
2636
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002637 atomic_dec(&fs_info->defrag_running);
2638 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002639}
2640
2641static struct new_sa_defrag_extent *
2642record_old_file_extents(struct inode *inode,
2643 struct btrfs_ordered_extent *ordered)
2644{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002645 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002646 struct btrfs_root *root = BTRFS_I(inode)->root;
2647 struct btrfs_path *path;
2648 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002649 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002650 struct new_sa_defrag_extent *new;
2651 int ret;
2652
2653 new = kmalloc(sizeof(*new), GFP_NOFS);
2654 if (!new)
2655 return NULL;
2656
2657 new->inode = inode;
2658 new->file_pos = ordered->file_offset;
2659 new->len = ordered->len;
2660 new->bytenr = ordered->start;
2661 new->disk_len = ordered->disk_len;
2662 new->compress_type = ordered->compress_type;
2663 new->root = RB_ROOT;
2664 INIT_LIST_HEAD(&new->head);
2665
2666 path = btrfs_alloc_path();
2667 if (!path)
2668 goto out_kfree;
2669
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002670 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002671 key.type = BTRFS_EXTENT_DATA_KEY;
2672 key.offset = new->file_pos;
2673
2674 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2675 if (ret < 0)
2676 goto out_free_path;
2677 if (ret > 0 && path->slots[0] > 0)
2678 path->slots[0]--;
2679
2680 /* find out all the old extents for the file range */
2681 while (1) {
2682 struct btrfs_file_extent_item *extent;
2683 struct extent_buffer *l;
2684 int slot;
2685 u64 num_bytes;
2686 u64 offset;
2687 u64 end;
2688 u64 disk_bytenr;
2689 u64 extent_offset;
2690
2691 l = path->nodes[0];
2692 slot = path->slots[0];
2693
2694 if (slot >= btrfs_header_nritems(l)) {
2695 ret = btrfs_next_leaf(root, path);
2696 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002697 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002698 else if (ret > 0)
2699 break;
2700 continue;
2701 }
2702
2703 btrfs_item_key_to_cpu(l, &key, slot);
2704
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002705 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002706 break;
2707 if (key.type != BTRFS_EXTENT_DATA_KEY)
2708 break;
2709 if (key.offset >= new->file_pos + new->len)
2710 break;
2711
2712 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2713
2714 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2715 if (key.offset + num_bytes < new->file_pos)
2716 goto next;
2717
2718 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2719 if (!disk_bytenr)
2720 goto next;
2721
2722 extent_offset = btrfs_file_extent_offset(l, extent);
2723
2724 old = kmalloc(sizeof(*old), GFP_NOFS);
2725 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002726 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002727
2728 offset = max(new->file_pos, key.offset);
2729 end = min(new->file_pos + new->len, key.offset + num_bytes);
2730
2731 old->bytenr = disk_bytenr;
2732 old->extent_offset = extent_offset;
2733 old->offset = offset - key.offset;
2734 old->len = end - offset;
2735 old->new = new;
2736 old->count = 0;
2737 list_add_tail(&old->list, &new->head);
2738next:
2739 path->slots[0]++;
2740 cond_resched();
2741 }
2742
2743 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002744 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002745
2746 return new;
2747
Liu Bo38c227d2013-01-29 03:18:40 +00002748out_free_path:
2749 btrfs_free_path(path);
2750out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002751 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002752 return NULL;
2753}
2754
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002755static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002756 u64 start, u64 len)
2757{
2758 struct btrfs_block_group_cache *cache;
2759
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002760 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002761 ASSERT(cache);
2762
2763 spin_lock(&cache->lock);
2764 cache->delalloc_bytes -= len;
2765 spin_unlock(&cache->lock);
2766
2767 btrfs_put_block_group(cache);
2768}
2769
Chris Masond352ac62008-09-29 15:18:18 -04002770/* as ordered data IO finishes, this gets called so we can finish
2771 * an ordered extent if the range of bytes in the file it covers are
2772 * fully written.
2773 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002774static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002775{
Josef Bacik5fd02042012-05-02 14:00:54 -04002776 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002777 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002778 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002779 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002780 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002781 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002782 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002783 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002784 int ret = 0;
2785 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002786 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002787 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002788
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002789 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002790
Josef Bacik5fd02042012-05-02 14:00:54 -04002791 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2792 ret = -EIO;
2793 goto out;
2794 }
2795
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002796 btrfs_free_io_failure_record(BTRFS_I(inode),
2797 ordered_extent->file_offset,
2798 ordered_extent->file_offset +
2799 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002800
Josef Bacik77cef2e2013-08-29 13:57:21 -04002801 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2802 truncated = true;
2803 logical_len = ordered_extent->truncated_len;
2804 /* Truncated the entire extent, don't bother adding */
2805 if (!logical_len)
2806 goto out;
2807 }
2808
Yan, Zhengc2167752009-11-12 09:34:21 +00002809 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002810 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002811
2812 /*
2813 * For mwrite(mmap + memset to write) case, we still reserve
2814 * space for NOCOW range.
2815 * As NOCOW won't cause a new delayed ref, just free the space
2816 */
2817 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2818 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002819 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2820 if (nolock)
2821 trans = btrfs_join_transaction_nolock(root);
2822 else
2823 trans = btrfs_join_transaction(root);
2824 if (IS_ERR(trans)) {
2825 ret = PTR_ERR(trans);
2826 trans = NULL;
2827 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002828 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002829 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002830 ret = btrfs_update_inode_fallback(trans, root, inode);
2831 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002832 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002833 goto out;
2834 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002835
Josef Bacik2ac55d42010-02-03 19:33:23 +00002836 lock_extent_bits(io_tree, ordered_extent->file_offset,
2837 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002838 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002839
Liu Bo38c227d2013-01-29 03:18:40 +00002840 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2841 ordered_extent->file_offset + ordered_extent->len - 1,
2842 EXTENT_DEFRAG, 1, cached_state);
2843 if (ret) {
2844 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002845 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002846 /* the inode is shared */
2847 new = record_old_file_extents(inode, ordered_extent);
2848
2849 clear_extent_bit(io_tree, ordered_extent->file_offset,
2850 ordered_extent->file_offset + ordered_extent->len - 1,
2851 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2852 }
2853
Josef Bacik0cb59c92010-07-02 12:14:14 -04002854 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002855 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002856 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002857 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002858 if (IS_ERR(trans)) {
2859 ret = PTR_ERR(trans);
2860 trans = NULL;
2861 goto out_unlock;
2862 }
Chris Masona79b7d42014-05-22 16:18:52 -07002863
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002864 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002865
Chris Masonc8b97812008-10-29 14:49:59 -04002866 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002867 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002868 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002869 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002870 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002871 ordered_extent->file_offset,
2872 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002873 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002874 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002875 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002876 ret = insert_reserved_file_extent(trans, inode,
2877 ordered_extent->file_offset,
2878 ordered_extent->start,
2879 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002880 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002881 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002882 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002883 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002884 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002885 ordered_extent->start,
2886 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002887 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002888 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2889 ordered_extent->file_offset, ordered_extent->len,
2890 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002891 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002892 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002893 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002894 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002895
David Sterbadf9f6282017-02-10 19:35:37 +01002896 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002897
Josef Bacik6c760c02012-11-09 10:53:21 -05002898 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2899 ret = btrfs_update_inode_fallback(trans, root, inode);
2900 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002901 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002902 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002903 }
2904 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002905out_unlock:
2906 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2907 ordered_extent->file_offset +
2908 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002909out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002910 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02002911 btrfs_delalloc_release_metadata(BTRFS_I(inode),
2912 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002913 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002914 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002915
Josef Bacik77cef2e2013-08-29 13:57:21 -04002916 if (ret || truncated) {
2917 u64 start, end;
2918
2919 if (truncated)
2920 start = ordered_extent->file_offset + logical_len;
2921 else
2922 start = ordered_extent->file_offset;
2923 end = ordered_extent->file_offset + ordered_extent->len - 1;
2924 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2925
2926 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02002927 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002928
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002929 /*
2930 * If the ordered extent had an IOERR or something else went
2931 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002932 * back to the allocator. We only free the extent in the
2933 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002934 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002935 if ((ret || !logical_len) &&
2936 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002937 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002938 btrfs_free_reserved_extent(fs_info,
2939 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002940 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002941 }
2942
2943
Josef Bacik5fd02042012-05-02 14:00:54 -04002944 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002945 * This needs to be done to make sure anybody waiting knows we are done
2946 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002947 */
2948 btrfs_remove_ordered_extent(inode, ordered_extent);
2949
Liu Bo38c227d2013-01-29 03:18:40 +00002950 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002951 if (new) {
2952 if (ret) {
2953 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002954 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08002955 } else {
2956 relink_file_extents(new);
2957 }
2958 }
Liu Bo38c227d2013-01-29 03:18:40 +00002959
Chris Masone6dcd2d2008-07-17 12:53:50 -04002960 /* once for us */
2961 btrfs_put_ordered_extent(ordered_extent);
2962 /* once for the tree */
2963 btrfs_put_ordered_extent(ordered_extent);
2964
Josef Bacik5fd02042012-05-02 14:00:54 -04002965 return ret;
2966}
2967
2968static void finish_ordered_fn(struct btrfs_work *work)
2969{
2970 struct btrfs_ordered_extent *ordered_extent;
2971 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2972 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002973}
2974
David Sterbac3988d62017-02-17 15:18:32 +01002975static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002976 struct extent_state *state, int uptodate)
2977{
Josef Bacik5fd02042012-05-02 14:00:54 -04002978 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002979 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002980 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002981 struct btrfs_workqueue *wq;
2982 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002983
liubo1abe9b82011-03-24 11:18:59 +00002984 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2985
Chris Mason8b62b722009-09-02 16:53:46 -04002986 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002987 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2988 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002989 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002990
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002991 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002992 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08002993 func = btrfs_freespace_write_helper;
2994 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002995 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08002996 func = btrfs_endio_write_helper;
2997 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002998
Liu Bo9e0af232014-08-15 23:36:53 +08002999 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3000 NULL);
3001 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003002}
3003
Miao Xiedc380ae2014-09-12 18:43:55 +08003004static int __readpage_endio_check(struct inode *inode,
3005 struct btrfs_io_bio *io_bio,
3006 int icsum, struct page *page,
3007 int pgoff, u64 start, size_t len)
3008{
3009 char *kaddr;
3010 u32 csum_expected;
3011 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003012
3013 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3014
3015 kaddr = kmap_atomic(page);
3016 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003017 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003018 if (csum != csum_expected)
3019 goto zeroit;
3020
3021 kunmap_atomic(kaddr);
3022 return 0;
3023zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003024 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003025 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003026 memset(kaddr + pgoff, 1, len);
3027 flush_dcache_page(page);
3028 kunmap_atomic(kaddr);
3029 if (csum_expected == 0)
3030 return 0;
3031 return -EIO;
3032}
3033
Chris Masond352ac62008-09-29 15:18:18 -04003034/*
Chris Masond352ac62008-09-29 15:18:18 -04003035 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003036 * if there's a match, we allow the bio to finish. If not, the code in
3037 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003038 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003039static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3040 u64 phy_offset, struct page *page,
3041 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003042{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003043 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003044 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003045 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003046 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003047
Chris Masond20f7042008-12-08 16:58:54 -05003048 if (PageChecked(page)) {
3049 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003050 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003051 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003052
3053 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003054 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003055
Yan Zheng17d217f2008-12-12 10:03:38 -05003056 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003057 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003058 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003059 return 0;
3060 }
3061
Miao Xiefacc8a222013-07-25 19:22:34 +08003062 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003063 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3064 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003065}
Chris Masonb888db2b2007-08-27 16:49:44 -04003066
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003067void btrfs_add_delayed_iput(struct inode *inode)
3068{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003069 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003070 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003071
3072 if (atomic_add_unless(&inode->i_count, -1, 1))
3073 return;
3074
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003075 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003076 if (binode->delayed_iput_count == 0) {
3077 ASSERT(list_empty(&binode->delayed_iput));
3078 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3079 } else {
3080 binode->delayed_iput_count++;
3081 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003082 spin_unlock(&fs_info->delayed_iput_lock);
3083}
3084
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003085void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003086{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003087
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003088 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003089 while (!list_empty(&fs_info->delayed_iputs)) {
3090 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003091
David Sterba8089fe62015-11-19 14:15:51 +01003092 inode = list_first_entry(&fs_info->delayed_iputs,
3093 struct btrfs_inode, delayed_iput);
3094 if (inode->delayed_iput_count) {
3095 inode->delayed_iput_count--;
3096 list_move_tail(&inode->delayed_iput,
3097 &fs_info->delayed_iputs);
3098 } else {
3099 list_del_init(&inode->delayed_iput);
3100 }
3101 spin_unlock(&fs_info->delayed_iput_lock);
3102 iput(&inode->vfs_inode);
3103 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003104 }
David Sterba8089fe62015-11-19 14:15:51 +01003105 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003106}
3107
Yan, Zhengd68fc572010-05-16 10:49:58 -04003108/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003109 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003110 * files in the subvolume, it removes orphan item and frees block_rsv
3111 * structure.
3112 */
3113void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3114 struct btrfs_root *root)
3115{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003116 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003117 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003118 int ret;
3119
Josef Bacik8a35d952012-05-23 14:26:42 -04003120 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003121 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3122 return;
3123
Josef Bacik90290e12011-12-02 15:44:12 -05003124 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003125 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003126 spin_unlock(&root->orphan_lock);
3127 return;
3128 }
3129
3130 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3131 spin_unlock(&root->orphan_lock);
3132 return;
3133 }
3134
3135 block_rsv = root->orphan_block_rsv;
3136 root->orphan_block_rsv = NULL;
3137 spin_unlock(&root->orphan_lock);
3138
Miao Xie27cdeb72014-04-02 19:51:05 +08003139 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003140 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003141 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003142 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003143 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003144 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003145 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003146 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3147 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003148 }
3149
Josef Bacik90290e12011-12-02 15:44:12 -05003150 if (block_rsv) {
3151 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003152 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003153 }
3154}
3155
3156/*
Josef Bacik7b128762008-07-24 12:17:14 -04003157 * This creates an orphan entry for the given inode in case something goes
3158 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003159 *
3160 * NOTE: caller of this function should reserve 5 units of metadata for
3161 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003162 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003163int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3164 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003165{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003166 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3167 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003168 struct btrfs_block_rsv *block_rsv = NULL;
3169 int reserve = 0;
3170 int insert = 0;
3171 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003172
Yan, Zhengd68fc572010-05-16 10:49:58 -04003173 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003174 block_rsv = btrfs_alloc_block_rsv(fs_info,
3175 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003176 if (!block_rsv)
3177 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003178 }
3179
Yan, Zhengd68fc572010-05-16 10:49:58 -04003180 spin_lock(&root->orphan_lock);
3181 if (!root->orphan_block_rsv) {
3182 root->orphan_block_rsv = block_rsv;
3183 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003184 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003185 block_rsv = NULL;
3186 }
Josef Bacik7b128762008-07-24 12:17:14 -04003187
Josef Bacik8a35d952012-05-23 14:26:42 -04003188 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003189 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003190#if 0
3191 /*
3192 * For proper ENOSPC handling, we should do orphan
3193 * cleanup when mounting. But this introduces backward
3194 * compatibility issue.
3195 */
3196 if (!xchg(&root->orphan_item_inserted, 1))
3197 insert = 2;
3198 else
3199 insert = 1;
3200#endif
3201 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003202 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003203 }
Josef Bacik7b128762008-07-24 12:17:14 -04003204
Josef Bacik72ac3c02012-05-23 14:13:11 -04003205 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003206 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003207 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003208 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003209
Yan, Zhengd68fc572010-05-16 10:49:58 -04003210 /* grab metadata reservation from transaction handle */
3211 if (reserve) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003212 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003213 ASSERT(!ret);
3214 if (ret) {
3215 atomic_dec(&root->orphan_inodes);
3216 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003217 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003218 if (insert)
3219 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003220 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003221 return ret;
3222 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003223 }
3224
3225 /* insert an orphan item to track this unlinked/truncated file */
3226 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003227 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003228 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003229 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003230 if (reserve) {
3231 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003232 &inode->runtime_flags);
3233 btrfs_orphan_release_metadata(inode);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003234 }
3235 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003236 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003237 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003238 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003239 return ret;
3240 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003241 }
3242 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003243 }
3244
3245 /* insert an orphan item to track subvolume contains orphan files */
3246 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003247 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003248 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003249 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003250 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003251 return ret;
3252 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003253 }
3254 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003255}
3256
3257/*
3258 * We have done the truncate/delete so we can go ahead and remove the orphan
3259 * item for this particular inode.
3260 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003261static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003262 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003263{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003264 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003265 int delete_item = 0;
3266 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003267 int ret = 0;
3268
Yan, Zhengd68fc572010-05-16 10:49:58 -04003269 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003270 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003271 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003272 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003273
Josef Bacik72ac3c02012-05-23 14:13:11 -04003274 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003275 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003276 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 spin_unlock(&root->orphan_lock);
3278
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003279 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003280 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003281 if (trans)
3282 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003283 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003284 }
Josef Bacik7b128762008-07-24 12:17:14 -04003285
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003286 if (release_rsv)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003287 btrfs_orphan_release_metadata(inode);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003288
Josef Bacik4ef31a42013-08-13 14:10:08 -04003289 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003290}
3291
3292/*
3293 * this cleans up any orphans that may be left on the list from the last use
3294 * of this root.
3295 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003296int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003297{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003298 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003299 struct btrfs_path *path;
3300 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003301 struct btrfs_key key, found_key;
3302 struct btrfs_trans_handle *trans;
3303 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003304 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003305 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3306
Yan, Zhengd68fc572010-05-16 10:49:58 -04003307 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003308 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003309
3310 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003311 if (!path) {
3312 ret = -ENOMEM;
3313 goto out;
3314 }
David Sterbae4058b52015-11-27 16:31:35 +01003315 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003316
3317 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003318 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003319 key.offset = (u64)-1;
3320
Josef Bacik7b128762008-07-24 12:17:14 -04003321 while (1) {
3322 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003323 if (ret < 0)
3324 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003325
3326 /*
3327 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003328 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003329 * find the key and see if we have stuff that matches
3330 */
3331 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003332 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003333 if (path->slots[0] == 0)
3334 break;
3335 path->slots[0]--;
3336 }
3337
3338 /* pull out the item */
3339 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003340 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3341
3342 /* make sure the item matches what we want */
3343 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3344 break;
David Sterba962a2982014-06-04 18:41:45 +02003345 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003346 break;
3347
3348 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003349 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003350
3351 /*
3352 * this is where we are basically btrfs_lookup, without the
3353 * crossing root thing. we store the inode number in the
3354 * offset of the orphan item.
3355 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003356
3357 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003358 btrfs_err(fs_info,
3359 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003360 ret = -EINVAL;
3361 goto out;
3362 }
3363
3364 last_objectid = found_key.offset;
3365
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003366 found_key.objectid = found_key.offset;
3367 found_key.type = BTRFS_INODE_ITEM_KEY;
3368 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003369 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303370 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003371 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003372 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003373
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003374 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003375 struct btrfs_root *dead_root;
3376 struct btrfs_fs_info *fs_info = root->fs_info;
3377 int is_dead_root = 0;
3378
3379 /*
3380 * this is an orphan in the tree root. Currently these
3381 * could come from 2 sources:
3382 * a) a snapshot deletion in progress
3383 * b) a free space cache inode
3384 * We need to distinguish those two, as the snapshot
3385 * orphan must not get deleted.
3386 * find_dead_roots already ran before us, so if this
3387 * is a snapshot deletion, we should find the root
3388 * in the dead_roots list
3389 */
3390 spin_lock(&fs_info->trans_lock);
3391 list_for_each_entry(dead_root, &fs_info->dead_roots,
3392 root_list) {
3393 if (dead_root->root_key.objectid ==
3394 found_key.objectid) {
3395 is_dead_root = 1;
3396 break;
3397 }
3398 }
3399 spin_unlock(&fs_info->trans_lock);
3400 if (is_dead_root) {
3401 /* prevent this orphan from being found again */
3402 key.offset = found_key.objectid - 1;
3403 continue;
3404 }
3405 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003406 /*
3407 * Inode is already gone but the orphan item is still there,
3408 * kill the orphan item.
3409 */
Filipe Manana67710892016-06-06 11:51:25 +01003410 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003411 trans = btrfs_start_transaction(root, 1);
3412 if (IS_ERR(trans)) {
3413 ret = PTR_ERR(trans);
3414 goto out;
3415 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003416 btrfs_debug(fs_info, "auto deleting %Lu",
3417 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003418 ret = btrfs_del_orphan_item(trans, root,
3419 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003420 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003421 if (ret)
3422 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003423 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003424 }
Josef Bacik7b128762008-07-24 12:17:14 -04003425
Josef Bacik7b128762008-07-24 12:17:14 -04003426 /*
3427 * add this inode to the orphan list so btrfs_orphan_del does
3428 * the proper thing when we hit it
3429 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003430 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3431 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003432 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003433
Josef Bacik7b128762008-07-24 12:17:14 -04003434 /* if we have links, this was a truncate, lets do that */
3435 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303436 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003437 iput(inode);
3438 continue;
3439 }
Josef Bacik7b128762008-07-24 12:17:14 -04003440 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003441
3442 /* 1 for the orphan item deletion. */
3443 trans = btrfs_start_transaction(root, 1);
3444 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003445 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003446 ret = PTR_ERR(trans);
3447 goto out;
3448 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003449 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003450 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003451 if (ret) {
3452 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003453 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003454 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003455
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003456 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003457 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003458 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003459 } else {
3460 nr_unlink++;
3461 }
3462
3463 /* this will do delete_inode and everything for us */
3464 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003465 if (ret)
3466 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003467 }
Miao Xie3254c872011-11-10 20:45:05 -05003468 /* release the path since we're done with it */
3469 btrfs_release_path(path);
3470
Yan, Zhengd68fc572010-05-16 10:49:58 -04003471 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3472
3473 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003474 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003475 (u64)-1);
3476
Miao Xie27cdeb72014-04-02 19:51:05 +08003477 if (root->orphan_block_rsv ||
3478 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003479 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003480 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003481 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003482 }
Josef Bacik7b128762008-07-24 12:17:14 -04003483
3484 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003485 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003486 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003487 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003488
3489out:
3490 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003491 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003492 btrfs_free_path(path);
3493 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003494}
3495
Chris Masond352ac62008-09-29 15:18:18 -04003496/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003497 * very simple check to peek ahead in the leaf looking for xattrs. If we
3498 * don't find any xattrs, we know there can't be any acls.
3499 *
3500 * slot is the slot the inode is in, objectid is the objectid of the inode
3501 */
3502static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003503 int slot, u64 objectid,
3504 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003505{
3506 u32 nritems = btrfs_header_nritems(leaf);
3507 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003508 static u64 xattr_access = 0;
3509 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003510 int scanned = 0;
3511
Josef Bacikf23b5a52013-06-19 10:16:26 -04003512 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003513 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3514 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3515 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3516 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003517 }
3518
Chris Mason46a53cc2009-04-27 11:47:50 -04003519 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003520 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003521 while (slot < nritems) {
3522 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3523
3524 /* we found a different objectid, there must not be acls */
3525 if (found_key.objectid != objectid)
3526 return 0;
3527
3528 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003529 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003530 if (*first_xattr_slot == -1)
3531 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003532 if (found_key.offset == xattr_access ||
3533 found_key.offset == xattr_default)
3534 return 1;
3535 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003536
3537 /*
3538 * we found a key greater than an xattr key, there can't
3539 * be any acls later on
3540 */
3541 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3542 return 0;
3543
3544 slot++;
3545 scanned++;
3546
3547 /*
3548 * it goes inode, inode backrefs, xattrs, extents,
3549 * so if there are a ton of hard links to an inode there can
3550 * be a lot of backrefs. Don't waste time searching too hard,
3551 * this is just an optimization
3552 */
3553 if (scanned >= 8)
3554 break;
3555 }
3556 /* we hit the end of the leaf before we found an xattr or
3557 * something larger than an xattr. We have to assume the inode
3558 * has acls
3559 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003560 if (*first_xattr_slot == -1)
3561 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003562 return 1;
3563}
3564
3565/*
Chris Masond352ac62008-09-29 15:18:18 -04003566 * read an inode from the btree into the in-memory inode
3567 */
Filipe Manana67710892016-06-06 11:51:25 +01003568static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003569{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003570 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003571 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003572 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003573 struct btrfs_inode_item *inode_item;
3574 struct btrfs_root *root = BTRFS_I(inode)->root;
3575 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003576 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003577 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003578 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003579 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003580 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003581 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003582
3583 ret = btrfs_fill_inode(inode, &rdev);
3584 if (!ret)
3585 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003586
3587 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003588 if (!path) {
3589 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003590 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003591 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003592
Chris Mason39279cc2007-06-12 06:35:45 -04003593 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003594
Chris Mason39279cc2007-06-12 06:35:45 -04003595 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003596 if (ret) {
3597 if (ret > 0)
3598 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003599 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003600 }
Chris Mason39279cc2007-06-12 06:35:45 -04003601
Chris Mason5f39d392007-10-15 16:14:19 -04003602 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003603
3604 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003605 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003606
Chris Mason5f39d392007-10-15 16:14:19 -04003607 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3608 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003609 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003610 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003611 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3612 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003613 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003614
David Sterbaa937b972014-12-12 17:39:12 +01003615 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3616 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003617
David Sterbaa937b972014-12-12 17:39:12 +01003618 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3619 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003620
David Sterbaa937b972014-12-12 17:39:12 +01003621 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3622 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003623
chandan r9cc97d62012-07-04 12:48:07 +05303624 BTRFS_I(inode)->i_otime.tv_sec =
3625 btrfs_timespec_sec(leaf, &inode_item->otime);
3626 BTRFS_I(inode)->i_otime.tv_nsec =
3627 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003628
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003629 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003630 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003631 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3632
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003633 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003634 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003635 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003636 rdev = btrfs_inode_rdev(leaf, inode_item);
3637
Josef Bacikaec74772008-07-24 12:12:38 -04003638 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003639 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003640
3641cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003642 /*
3643 * If we were modified in the current generation and evicted from memory
3644 * and then re-read we need to do a full sync since we don't have any
3645 * idea about which extents were modified before we were evicted from
3646 * cache.
3647 *
3648 * This is required for both inode re-read from disk and delayed inode
3649 * in delayed_nodes_tree.
3650 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003651 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003652 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3653 &BTRFS_I(inode)->runtime_flags);
3654
Filipe Mananabde6c242015-07-24 00:00:19 +01003655 /*
3656 * We don't persist the id of the transaction where an unlink operation
3657 * against the inode was last made. So here we assume the inode might
3658 * have been evicted, and therefore the exact value of last_unlink_trans
3659 * lost, and set it to last_trans to avoid metadata inconsistencies
3660 * between the inode and its parent if the inode is fsync'ed and the log
3661 * replayed. For example, in the scenario:
3662 *
3663 * touch mydir/foo
3664 * ln mydir/foo mydir/bar
3665 * sync
3666 * unlink mydir/bar
3667 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3668 * xfs_io -c fsync mydir/foo
3669 * <power failure>
3670 * mount fs, triggers fsync log replay
3671 *
3672 * We must make sure that when we fsync our inode foo we also log its
3673 * parent inode, otherwise after log replay the parent still has the
3674 * dentry with the "bar" name but our inode foo has a link count of 1
3675 * and doesn't have an inode ref with the name "bar" anymore.
3676 *
3677 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003678 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003679 * transaction commits on fsync if our inode is a directory, or if our
3680 * inode is not a directory, logging its parent unnecessarily.
3681 */
3682 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3683
Miao Xie67de1172013-12-26 13:07:06 +08003684 path->slots[0]++;
3685 if (inode->i_nlink != 1 ||
3686 path->slots[0] >= btrfs_header_nritems(leaf))
3687 goto cache_acl;
3688
3689 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003690 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003691 goto cache_acl;
3692
3693 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3694 if (location.type == BTRFS_INODE_REF_KEY) {
3695 struct btrfs_inode_ref *ref;
3696
3697 ref = (struct btrfs_inode_ref *)ptr;
3698 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3699 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3700 struct btrfs_inode_extref *extref;
3701
3702 extref = (struct btrfs_inode_extref *)ptr;
3703 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3704 extref);
3705 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003706cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003707 /*
3708 * try to precache a NULL acl entry for files that don't have
3709 * any xattrs or acls
3710 */
Li Zefan33345d012011-04-20 10:31:50 +08003711 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003712 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003713 if (first_xattr_slot != -1) {
3714 path->slots[0] = first_xattr_slot;
3715 ret = btrfs_load_inode_props(inode, path);
3716 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003717 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003718 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003719 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003720 root->root_key.objectid, ret);
3721 }
3722 btrfs_free_path(path);
3723
Al Viro72c04902009-06-24 16:58:48 -04003724 if (!maybe_acls)
3725 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003726
Chris Mason39279cc2007-06-12 06:35:45 -04003727 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003728 case S_IFREG:
3729 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003730 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003731 inode->i_fop = &btrfs_file_operations;
3732 inode->i_op = &btrfs_file_inode_operations;
3733 break;
3734 case S_IFDIR:
3735 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003736 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003737 break;
3738 case S_IFLNK:
3739 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003740 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003741 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3742 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003743 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003744 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003745 init_special_inode(inode, inode->i_mode, rdev);
3746 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003747 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003748
3749 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003750 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003751
3752make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003753 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003754 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003755 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003756}
3757
Chris Masond352ac62008-09-29 15:18:18 -04003758/*
3759 * given a leaf and an inode, copy the inode fields into the leaf
3760 */
Chris Masone02119d2008-09-05 16:13:11 -04003761static void fill_inode_item(struct btrfs_trans_handle *trans,
3762 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003763 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003764 struct inode *inode)
3765{
Liu Bo51fab692012-12-27 09:01:21 +00003766 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003767
Liu Bo51fab692012-12-27 09:01:21 +00003768 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003769
Liu Bo51fab692012-12-27 09:01:21 +00003770 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3771 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3772 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3773 &token);
3774 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3775 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003776
David Sterbaa937b972014-12-12 17:39:12 +01003777 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003778 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003779 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003780 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003781
David Sterbaa937b972014-12-12 17:39:12 +01003782 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003783 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003784 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003785 inode->i_mtime.tv_nsec, &token);
3786
David Sterbaa937b972014-12-12 17:39:12 +01003787 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003788 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003789 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003790 inode->i_ctime.tv_nsec, &token);
3791
chandan r9cc97d62012-07-04 12:48:07 +05303792 btrfs_set_token_timespec_sec(leaf, &item->otime,
3793 BTRFS_I(inode)->i_otime.tv_sec, &token);
3794 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3795 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3796
Liu Bo51fab692012-12-27 09:01:21 +00003797 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3798 &token);
3799 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3800 &token);
3801 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3802 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3803 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3804 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3805 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003806}
3807
Chris Masond352ac62008-09-29 15:18:18 -04003808/*
3809 * copy everything in the in-memory inode into the btree.
3810 */
Chris Mason21151332011-11-10 20:39:08 -05003811static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003812 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003813{
3814 struct btrfs_inode_item *inode_item;
3815 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003816 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003817 int ret;
3818
3819 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003820 if (!path)
3821 return -ENOMEM;
3822
Chris Masonb9473432009-03-13 11:00:37 -04003823 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003824 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3825 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003826 if (ret) {
3827 if (ret > 0)
3828 ret = -ENOENT;
3829 goto failed;
3830 }
3831
Chris Mason5f39d392007-10-15 16:14:19 -04003832 leaf = path->nodes[0];
3833 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003834 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003835
Chris Masone02119d2008-09-05 16:13:11 -04003836 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003837 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003838 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003839 ret = 0;
3840failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003841 btrfs_free_path(path);
3842 return ret;
3843}
3844
Chris Masond352ac62008-09-29 15:18:18 -04003845/*
Chris Mason21151332011-11-10 20:39:08 -05003846 * copy everything in the in-memory inode into the btree.
3847 */
3848noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3849 struct btrfs_root *root, struct inode *inode)
3850{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003851 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003852 int ret;
3853
3854 /*
3855 * If the inode is a free space inode, we can deadlock during commit
3856 * if we put it into the delayed code.
3857 *
3858 * The data relocation inode should also be directly updated
3859 * without delay
3860 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003861 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003862 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003863 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003864 btrfs_update_root_times(trans, root);
3865
Chris Mason21151332011-11-10 20:39:08 -05003866 ret = btrfs_delayed_update_inode(trans, root, inode);
3867 if (!ret)
3868 btrfs_set_inode_last_trans(trans, inode);
3869 return ret;
3870 }
3871
3872 return btrfs_update_inode_item(trans, root, inode);
3873}
3874
Josef Bacikbe6aef62012-10-22 15:43:12 -04003875noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3876 struct btrfs_root *root,
3877 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003878{
3879 int ret;
3880
3881 ret = btrfs_update_inode(trans, root, inode);
3882 if (ret == -ENOSPC)
3883 return btrfs_update_inode_item(trans, root, inode);
3884 return ret;
3885}
3886
3887/*
Chris Masond352ac62008-09-29 15:18:18 -04003888 * unlink helper that gets used here in inode.c and in the tree logging
3889 * recovery code. It remove a link in a directory with a given name, and
3890 * also drops the back refs in the inode to the directory
3891 */
Al Viro92986792011-03-04 17:14:37 +00003892static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3893 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003894 struct btrfs_inode *dir,
3895 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003896 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003897{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003898 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003899 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003900 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003901 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003902 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003903 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003904 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003905 u64 ino = btrfs_ino(inode);
3906 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003907
3908 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003909 if (!path) {
3910 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003911 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003912 }
3913
Chris Masonb9473432009-03-13 11:00:37 -04003914 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003915 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003916 name, name_len, -1);
3917 if (IS_ERR(di)) {
3918 ret = PTR_ERR(di);
3919 goto err;
3920 }
3921 if (!di) {
3922 ret = -ENOENT;
3923 goto err;
3924 }
Chris Mason5f39d392007-10-15 16:14:19 -04003925 leaf = path->nodes[0];
3926 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003927 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003928 if (ret)
3929 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003930 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003931
Miao Xie67de1172013-12-26 13:07:06 +08003932 /*
3933 * If we don't have dir index, we have to get it by looking up
3934 * the inode ref, since we get the inode ref, remove it directly,
3935 * it is unnecessary to do delayed deletion.
3936 *
3937 * But if we have dir index, needn't search inode ref to get it.
3938 * Since the inode ref is close to the inode item, it is better
3939 * that we delay to delete it, and just do this deletion when
3940 * we update the inode item.
3941 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003942 if (inode->dir_index) {
3943 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08003944 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003945 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003946 goto skip_backref;
3947 }
3948 }
3949
Li Zefan33345d012011-04-20 10:31:50 +08003950 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3951 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003952 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003953 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003954 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003955 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003956 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003957 goto err;
3958 }
Miao Xie67de1172013-12-26 13:07:06 +08003959skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003960 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003961 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003962 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003963 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003964 }
Chris Mason39279cc2007-06-12 06:35:45 -04003965
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003966 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3967 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003968 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003969 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003970 goto err;
3971 }
Chris Masone02119d2008-09-05 16:13:11 -04003972
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003973 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3974 index);
Chris Mason6418c962010-10-30 07:34:24 -04003975 if (ret == -ENOENT)
3976 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003977 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003978 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003979err:
3980 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003981 if (ret)
3982 goto out;
3983
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003984 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003985 inode_inc_iversion(&inode->vfs_inode);
3986 inode_inc_iversion(&dir->vfs_inode);
3987 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3988 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3989 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003990out:
Chris Mason39279cc2007-06-12 06:35:45 -04003991 return ret;
3992}
3993
Al Viro92986792011-03-04 17:14:37 +00003994int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3995 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003996 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003997 const char *name, int name_len)
3998{
3999 int ret;
4000 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4001 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004002 drop_nlink(&inode->vfs_inode);
4003 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004004 }
4005 return ret;
4006}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004007
4008/*
4009 * helper to start transaction for unlink and rmdir.
4010 *
Josef Bacikd52be812013-05-29 14:54:47 -04004011 * unlink and rmdir are special in btrfs, they do not always free space, so
4012 * if we cannot make our reservations the normal way try and see if there is
4013 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4014 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004015 */
Josef Bacikd52be812013-05-29 14:54:47 -04004016static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004017{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004018 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004019
Josef Bacike70bea52011-10-11 14:18:24 -04004020 /*
4021 * 1 for the possible orphan item
4022 * 1 for the dir item
4023 * 1 for the dir index
4024 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004025 * 1 for the inode
4026 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004027 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004028}
4029
Chris Mason39279cc2007-06-12 06:35:45 -04004030static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4031{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004032 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004033 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004034 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004035 int ret;
4036
Josef Bacikd52be812013-05-29 14:54:47 -04004037 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004038 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004039 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004040
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004041 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4042 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004043
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004044 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4045 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4046 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004047 if (ret)
4048 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004049
Yan, Zhenga22285a2010-05-16 10:48:46 -04004050 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004051 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004052 if (ret)
4053 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004054 }
Josef Bacik7b128762008-07-24 12:17:14 -04004055
Tsutomu Itohb5324022011-07-19 07:27:20 +00004056out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004057 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004058 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004059 return ret;
4060}
4061
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004062int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4063 struct btrfs_root *root,
4064 struct inode *dir, u64 objectid,
4065 const char *name, int name_len)
4066{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004067 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004068 struct btrfs_path *path;
4069 struct extent_buffer *leaf;
4070 struct btrfs_dir_item *di;
4071 struct btrfs_key key;
4072 u64 index;
4073 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004074 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004075
4076 path = btrfs_alloc_path();
4077 if (!path)
4078 return -ENOMEM;
4079
Li Zefan33345d012011-04-20 10:31:50 +08004080 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004081 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004082 if (IS_ERR_OR_NULL(di)) {
4083 if (!di)
4084 ret = -ENOENT;
4085 else
4086 ret = PTR_ERR(di);
4087 goto out;
4088 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004089
4090 leaf = path->nodes[0];
4091 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4092 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4093 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004094 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004095 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004096 goto out;
4097 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004098 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004099
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004100 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4101 root->root_key.objectid, dir_ino,
4102 &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]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004121 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004122 index = key.offset;
4123 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004124 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004125
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004126 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004127 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004128 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004129 goto out;
4130 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004131
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004132 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004133 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004134 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004135 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004136 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004137 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004138out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004139 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004140 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004141}
4142
Chris Mason39279cc2007-06-12 06:35:45 -04004143static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4144{
David Howells2b0143b2015-03-17 22:25:59 +00004145 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004146 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004147 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004148 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004149 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004150
David Sterbab3ae2442012-09-13 16:04:34 -06004151 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004152 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004153 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004154 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004155
Josef Bacikd52be812013-05-29 14:54:47 -04004156 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004157 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004158 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004159
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004160 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004161 err = btrfs_unlink_subvol(trans, root, dir,
4162 BTRFS_I(inode)->location.objectid,
4163 dentry->d_name.name,
4164 dentry->d_name.len);
4165 goto out;
4166 }
4167
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004168 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004169 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004170 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004171
Filipe Manana44f714d2016-06-06 16:11:13 +01004172 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4173
Chris Mason39279cc2007-06-12 06:35:45 -04004174 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004175 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4176 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4177 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004178 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004179 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004180 /*
4181 * Propagate the last_unlink_trans value of the deleted dir to
4182 * its parent directory. This is to prevent an unrecoverable
4183 * log tree in the case we do something like this:
4184 * 1) create dir foo
4185 * 2) create snapshot under dir foo
4186 * 3) delete the snapshot
4187 * 4) rmdir foo
4188 * 5) mkdir foo
4189 * 6) fsync foo or some file inside foo
4190 */
4191 if (last_unlink_trans >= trans->transid)
4192 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4193 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004195 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004196 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004197
Chris Mason39279cc2007-06-12 06:35:45 -04004198 return err;
4199}
4200
Chris Mason28f75a02015-02-04 06:59:29 -08004201static int truncate_space_check(struct btrfs_trans_handle *trans,
4202 struct btrfs_root *root,
4203 u64 bytes_deleted)
4204{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004205 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004206 int ret;
4207
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004208 /*
4209 * This is only used to apply pressure to the enospc system, we don't
4210 * intend to use this reservation at all.
4211 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004212 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004213 bytes_deleted *= fs_info->nodesize;
4214 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004215 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004216 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004217 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004218 trans->transid,
4219 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004220 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004221 }
Chris Mason28f75a02015-02-04 06:59:29 -08004222 return ret;
4223
4224}
4225
Filipe Manana0305cd52015-10-16 12:34:25 +01004226static int truncate_inline_extent(struct inode *inode,
4227 struct btrfs_path *path,
4228 struct btrfs_key *found_key,
4229 const u64 item_end,
4230 const u64 new_size)
4231{
4232 struct extent_buffer *leaf = path->nodes[0];
4233 int slot = path->slots[0];
4234 struct btrfs_file_extent_item *fi;
4235 u32 size = (u32)(new_size - found_key->offset);
4236 struct btrfs_root *root = BTRFS_I(inode)->root;
4237
4238 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4239
4240 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4241 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004242 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004243
4244 /*
4245 * Zero out the remaining of the last page of our inline extent,
4246 * instead of directly truncating our inline extent here - that
4247 * would be much more complex (decompressing all the data, then
4248 * compressing the truncated data, which might be bigger than
4249 * the size of the inline extent, resize the extent, etc).
4250 * We release the path because to get the page we might need to
4251 * read the extent item from disk (data not in the page cache).
4252 */
4253 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304254 return btrfs_truncate_block(inode, offset, page_end - offset,
4255 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004256 }
4257
4258 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4259 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004260 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004261
4262 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4263 inode_sub_bytes(inode, item_end + 1 - new_size);
4264
4265 return 0;
4266}
4267
Chris Mason323ac952008-10-01 19:05:46 -04004268/*
Chris Mason39279cc2007-06-12 06:35:45 -04004269 * this can truncate away extent items, csum items and directory items.
4270 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004271 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004272 *
4273 * csum items that cross the new i_size are truncated to the new size
4274 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004275 *
4276 * min_type is the minimum key type to truncate down to. If set to 0, this
4277 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004278 */
Yan, Zheng80825102009-11-12 09:35:36 +00004279int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4280 struct btrfs_root *root,
4281 struct inode *inode,
4282 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004283{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004284 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004285 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004286 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004287 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004288 struct btrfs_key key;
4289 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004290 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004291 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004292 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004293 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004294 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004295 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004296 int found_extent;
4297 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004298 int pending_del_nr = 0;
4299 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004300 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004301 int ret;
4302 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004303 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004304 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004305 bool be_nice = 0;
4306 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004307 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004308
4309 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004310
Chris Mason28ed1342014-12-17 09:41:04 -08004311 /*
4312 * for non-free space inodes and ref cows, we want to back off from
4313 * time to time
4314 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004315 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004316 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4317 be_nice = 1;
4318
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004319 path = btrfs_alloc_path();
4320 if (!path)
4321 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004322 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004323
Josef Bacik5dc562c2012-08-17 13:14:17 -04004324 /*
4325 * We want to drop from the next block forward in case this new size is
4326 * not block aligned since we will be keeping the last block of the
4327 * extent just the way it is.
4328 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004329 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004330 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004331 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004332 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004333 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004334
Miao Xie16cdcec2011-04-22 18:12:22 +08004335 /*
4336 * This function is also used to drop the items in the log tree before
4337 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4338 * it is used to drop the loged items. So we shouldn't kill the delayed
4339 * items.
4340 */
4341 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004342 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004343
Li Zefan33345d012011-04-20 10:31:50 +08004344 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004345 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004346 key.type = (u8)-1;
4347
Chris Mason85e21ba2008-01-29 15:11:36 -05004348search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004349 /*
4350 * with a 16K leaf size and 128MB extents, you can actually queue
4351 * up a huge file in a single leaf. Most of the time that
4352 * bytes_deleted is > 0, it will be huge by the time we get here
4353 */
Byongho Leeee221842015-12-15 01:42:10 +09004354 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004355 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004356 err = -EAGAIN;
4357 goto error;
4358 }
4359 }
4360
4361
Chris Masonb9473432009-03-13 11:00:37 -04004362 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004363 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004364 if (ret < 0) {
4365 err = ret;
4366 goto out;
4367 }
Chris Masond3977122009-01-05 21:25:51 -05004368
Chris Mason85e21ba2008-01-29 15:11:36 -05004369 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004370 /* there are no items in the tree for us to truncate, we're
4371 * done
4372 */
Yan, Zheng80825102009-11-12 09:35:36 +00004373 if (path->slots[0] == 0)
4374 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004375 path->slots[0]--;
4376 }
4377
Chris Masond3977122009-01-05 21:25:51 -05004378 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004379 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004380 leaf = path->nodes[0];
4381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004382 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004383
Li Zefan33345d012011-04-20 10:31:50 +08004384 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004385 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004386
Chris Mason85e21ba2008-01-29 15:11:36 -05004387 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004388 break;
4389
Chris Mason5f39d392007-10-15 16:14:19 -04004390 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004391 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004392 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004393 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004394 extent_type = btrfs_file_extent_type(leaf, fi);
4395 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004396 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004397 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004398 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004399 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004400 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004401 }
Yan008630c2007-11-07 13:31:09 -05004402 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004403 }
Yan, Zheng80825102009-11-12 09:35:36 +00004404 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004405 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004406 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004407 if (item_end < new_size) {
4408 /*
4409 * With NO_HOLES mode, for the following mapping
4410 *
4411 * [0-4k][hole][8k-12k]
4412 *
4413 * if truncating isize down to 6k, it ends up
4414 * isize being 8k.
4415 */
4416 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4417 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004418 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004419 }
Yan, Zheng80825102009-11-12 09:35:36 +00004420 if (found_key.offset >= new_size)
4421 del_item = 1;
4422 else
4423 del_item = 0;
4424 }
Chris Mason39279cc2007-06-12 06:35:45 -04004425 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004426 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004427 if (found_type != BTRFS_EXTENT_DATA_KEY)
4428 goto delete;
4429
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004430 if (del_item)
4431 last_size = found_key.offset;
4432 else
4433 last_size = new_size;
4434
Chris Mason179e29e2007-11-01 11:28:41 -04004435 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004436 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004437 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004438 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004439 u64 orig_num_bytes =
4440 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004441 extent_num_bytes = ALIGN(new_size -
4442 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004443 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004444 btrfs_set_file_extent_num_bytes(leaf, fi,
4445 extent_num_bytes);
4446 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004447 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004448 if (test_bit(BTRFS_ROOT_REF_COWS,
4449 &root->state) &&
4450 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004451 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004452 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004453 } else {
Chris Masondb945352007-10-15 16:15:53 -04004454 extent_num_bytes =
4455 btrfs_file_extent_disk_num_bytes(leaf,
4456 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004457 extent_offset = found_key.offset -
4458 btrfs_file_extent_offset(leaf, fi);
4459
Chris Mason39279cc2007-06-12 06:35:45 -04004460 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004461 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004462 if (extent_start != 0) {
4463 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004464 if (test_bit(BTRFS_ROOT_REF_COWS,
4465 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004466 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004467 }
4468 }
Chris Mason90692182008-02-08 13:49:28 -05004469 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004470 /*
4471 * we can't truncate inline items that have had
4472 * special encodings
4473 */
4474 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004475 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4476 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004477
4478 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004479 * Need to release path in order to truncate a
4480 * compressed extent. So delete any accumulated
4481 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004482 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004483 if (btrfs_file_extent_compression(leaf, fi) !=
4484 BTRFS_COMPRESS_NONE && pending_del_nr) {
4485 err = btrfs_del_items(trans, root, path,
4486 pending_del_slot,
4487 pending_del_nr);
4488 if (err) {
4489 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004490 err);
4491 goto error;
4492 }
4493 pending_del_nr = 0;
4494 }
4495
4496 err = truncate_inline_extent(inode, path,
4497 &found_key,
4498 item_end,
4499 new_size);
4500 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004501 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004502 goto error;
4503 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004504 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4505 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004506 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004507 }
Chris Mason39279cc2007-06-12 06:35:45 -04004508 }
Chris Mason179e29e2007-11-01 11:28:41 -04004509delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004510 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004511 if (!pending_del_nr) {
4512 /* no pending yet, add ourselves */
4513 pending_del_slot = path->slots[0];
4514 pending_del_nr = 1;
4515 } else if (pending_del_nr &&
4516 path->slots[0] + 1 == pending_del_slot) {
4517 /* hop on the pending chunk */
4518 pending_del_nr++;
4519 pending_del_slot = path->slots[0];
4520 } else {
Chris Masond3977122009-01-05 21:25:51 -05004521 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004522 }
Chris Mason39279cc2007-06-12 06:35:45 -04004523 } else {
4524 break;
4525 }
Chris Mason28f75a02015-02-04 06:59:29 -08004526 should_throttle = 0;
4527
Miao Xie27cdeb72014-04-02 19:51:05 +08004528 if (found_extent &&
4529 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004530 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004531 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004532 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004533 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004534 extent_num_bytes, 0,
4535 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004536 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004537 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004538 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4539 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004540 trans->delayed_ref_updates * 2,
4541 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004542 if (be_nice) {
4543 if (truncate_space_check(trans, root,
4544 extent_num_bytes)) {
4545 should_end = 1;
4546 }
4547 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004548 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004549 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004550 }
Chris Mason39279cc2007-06-12 06:35:45 -04004551 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004552
Yan, Zheng80825102009-11-12 09:35:36 +00004553 if (found_type == BTRFS_INODE_ITEM_KEY)
4554 break;
4555
4556 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004557 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004558 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004559 if (pending_del_nr) {
4560 ret = btrfs_del_items(trans, root, path,
4561 pending_del_slot,
4562 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004563 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004564 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004565 goto error;
4566 }
Yan, Zheng80825102009-11-12 09:35:36 +00004567 pending_del_nr = 0;
4568 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004569 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004570 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004571 unsigned long updates = trans->delayed_ref_updates;
4572 if (updates) {
4573 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004574 ret = btrfs_run_delayed_refs(trans,
4575 fs_info,
4576 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004577 if (ret && !err)
4578 err = ret;
4579 }
4580 }
Chris Mason28f75a02015-02-04 06:59:29 -08004581 /*
4582 * if we failed to refill our space rsv, bail out
4583 * and let the transaction restart
4584 */
4585 if (should_end) {
4586 err = -EAGAIN;
4587 goto error;
4588 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004589 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004590 } else {
4591 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004592 }
Chris Mason39279cc2007-06-12 06:35:45 -04004593 }
Yan, Zheng80825102009-11-12 09:35:36 +00004594out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004595 if (pending_del_nr) {
4596 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4597 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004598 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004599 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004600 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004601error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004602 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004603 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004604
Chris Mason39279cc2007-06-12 06:35:45 -04004605 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004606
Liu Bo19fd2df2016-12-01 13:01:02 -08004607 if (err == 0) {
4608 /* only inline file may have last_size != new_size */
4609 if (new_size >= fs_info->sectorsize ||
4610 new_size > fs_info->max_inline)
4611 ASSERT(last_size == new_size);
4612 }
4613
Byongho Leeee221842015-12-15 01:42:10 +09004614 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004615 unsigned long updates = trans->delayed_ref_updates;
4616 if (updates) {
4617 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004618 ret = btrfs_run_delayed_refs(trans, fs_info,
4619 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004620 if (ret && !err)
4621 err = ret;
4622 }
4623 }
Yan, Zheng80825102009-11-12 09:35:36 +00004624 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004625}
4626
Chris Masona52d9a82007-08-27 16:49:44 -04004627/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304628 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004629 * @inode - inode that we're zeroing
4630 * @from - the offset to start zeroing
4631 * @len - the length to zero, 0 to zero the entire range respective to the
4632 * offset
4633 * @front - zero up to the offset instead of from the offset on
4634 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304635 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004636 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004637 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304638int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004639 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004640{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004641 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004642 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004643 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4644 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004645 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004646 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004647 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004648 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304649 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004650 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004651 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004652 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304653 u64 block_start;
4654 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004655
Josef Bacik2aaa6652012-08-29 14:27:18 -04004656 if ((offset & (blocksize - 1)) == 0 &&
4657 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004658 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304659
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004660 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304661 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004662 if (ret)
4663 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004664
Chris Mason211c17f2008-05-15 09:13:45 -04004665again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004666 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004667 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004668 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304669 round_down(from, blocksize),
4670 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004671 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004672 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004673 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004674
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304675 block_start = round_down(from, blocksize);
4676 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004677
Chris Masona52d9a82007-08-27 16:49:44 -04004678 if (!PageUptodate(page)) {
4679 ret = btrfs_readpage(NULL, page);
4680 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004681 if (page->mapping != mapping) {
4682 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004683 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004684 goto again;
4685 }
Chris Masona52d9a82007-08-27 16:49:44 -04004686 if (!PageUptodate(page)) {
4687 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004688 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004689 }
4690 }
Chris Mason211c17f2008-05-15 09:13:45 -04004691 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004692
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304693 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004694 set_page_extent_mapped(page);
4695
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304696 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004697 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304698 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004699 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004700 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004701 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004702 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004703 btrfs_put_ordered_extent(ordered);
4704 goto again;
4705 }
4706
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304707 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004708 EXTENT_DIRTY | EXTENT_DELALLOC |
4709 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004710 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004711
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304712 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004713 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004714 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304715 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004716 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004717 goto out_unlock;
4718 }
4719
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304720 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004721 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304722 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004723 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004724 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304725 memset(kaddr + (block_start - page_offset(page)),
4726 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004727 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304728 memset(kaddr + (block_start - page_offset(page)) + offset,
4729 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004730 flush_dcache_page(page);
4731 kunmap(page);
4732 }
Chris Mason247e7432008-07-17 12:53:51 -04004733 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004734 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304735 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004736 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004737
Chris Mason89642222008-07-24 09:41:53 -04004738out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004739 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304740 btrfs_delalloc_release_space(inode, block_start,
4741 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004742 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004743 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004744out:
4745 return ret;
4746}
4747
Josef Bacik16e75492013-10-22 12:18:51 -04004748static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4749 u64 offset, u64 len)
4750{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004751 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004752 struct btrfs_trans_handle *trans;
4753 int ret;
4754
4755 /*
4756 * Still need to make sure the inode looks like it's been updated so
4757 * that any holes get logged if we fsync.
4758 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004759 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4760 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004761 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4762 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4763 return 0;
4764 }
4765
4766 /*
4767 * 1 - for the one we're dropping
4768 * 1 - for the one we're adding
4769 * 1 - for updating the inode.
4770 */
4771 trans = btrfs_start_transaction(root, 3);
4772 if (IS_ERR(trans))
4773 return PTR_ERR(trans);
4774
4775 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4776 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004777 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004778 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004779 return ret;
4780 }
4781
David Sterbaf85b7372017-01-20 14:54:07 +01004782 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4783 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004784 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004785 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004786 else
4787 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004788 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004789 return ret;
4790}
4791
Josef Bacik695a0d02011-03-04 15:46:53 -05004792/*
4793 * This function puts in dummy file extents for the area we're creating a hole
4794 * for. So if we are truncating this file to a larger size we need to insert
4795 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4796 * the range between oldsize and size
4797 */
Josef Bacika41ad392011-01-31 15:30:16 -05004798int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004799{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004800 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004801 struct btrfs_root *root = BTRFS_I(inode)->root;
4802 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004803 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004804 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004805 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004806 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4807 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004808 u64 last_byte;
4809 u64 cur_offset;
4810 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004811 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004812
Josef Bacika71754f2013-06-17 17:14:39 -04004813 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304814 * If our size started in the middle of a block we need to zero out the
4815 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004816 * expose stale data.
4817 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304818 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004819 if (err)
4820 return err;
4821
Yan Zheng9036c102008-10-30 14:19:41 -04004822 if (size <= hole_start)
4823 return 0;
4824
Yan Zheng9036c102008-10-30 14:19:41 -04004825 while (1) {
4826 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004827
David Sterbaff13db42015-12-03 14:30:40 +01004828 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004829 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004830 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004831 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004832 if (!ordered)
4833 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004834 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4835 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004836 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004837 btrfs_put_ordered_extent(ordered);
4838 }
4839
Yan Zheng9036c102008-10-30 14:19:41 -04004840 cur_offset = hole_start;
4841 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004842 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004843 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004844 if (IS_ERR(em)) {
4845 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004846 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004847 break;
4848 }
Yan Zheng9036c102008-10-30 14:19:41 -04004849 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004850 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004851 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004852 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004853 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004854
Josef Bacik16e75492013-10-22 12:18:51 -04004855 err = maybe_insert_hole(root, inode, cur_offset,
4856 hole_size);
4857 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004858 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004859 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004860 cur_offset + hole_size - 1, 0);
4861 hole_em = alloc_extent_map();
4862 if (!hole_em) {
4863 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4864 &BTRFS_I(inode)->runtime_flags);
4865 goto next;
4866 }
4867 hole_em->start = cur_offset;
4868 hole_em->len = hole_size;
4869 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004870
Josef Bacik5dc562c2012-08-17 13:14:17 -04004871 hole_em->block_start = EXTENT_MAP_HOLE;
4872 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004873 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004874 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004875 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004876 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004877 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004878
4879 while (1) {
4880 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004881 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004882 write_unlock(&em_tree->lock);
4883 if (err != -EEXIST)
4884 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004885 btrfs_drop_extent_cache(BTRFS_I(inode),
4886 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004887 cur_offset +
4888 hole_size - 1, 0);
4889 }
4890 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004891 }
Josef Bacik16e75492013-10-22 12:18:51 -04004892next:
Yan Zheng9036c102008-10-30 14:19:41 -04004893 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004894 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004895 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004896 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004897 break;
4898 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004899 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004900 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4901 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004902 return err;
4903}
4904
Eric Sandeen3972f262013-01-12 02:57:22 +00004905static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004906{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004907 struct btrfs_root *root = BTRFS_I(inode)->root;
4908 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004909 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004910 loff_t newsize = attr->ia_size;
4911 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004912 int ret;
4913
Eric Sandeen3972f262013-01-12 02:57:22 +00004914 /*
4915 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4916 * special case where we need to update the times despite not having
4917 * these flags set. For all other operations the VFS set these flags
4918 * explicitly if it wants a timestamp update.
4919 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004920 if (newsize != oldsize) {
4921 inode_inc_iversion(inode);
4922 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4923 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004924 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004925 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004926
Josef Bacika41ad392011-01-31 15:30:16 -05004927 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004928 /*
4929 * Don't do an expanding truncate while snapshoting is ongoing.
4930 * This is to ensure the snapshot captures a fully consistent
4931 * state of this file - if the snapshot captures this expanding
4932 * truncation, it must capture all writes that happened before
4933 * this truncation.
4934 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004935 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004936 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004937 if (ret) {
4938 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004939 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004940 }
Yan, Zheng80825102009-11-12 09:35:36 +00004941
Miao Xief4a2f4c2011-12-14 20:12:01 -05004942 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004943 if (IS_ERR(trans)) {
4944 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004945 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004946 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004947
4948 i_size_write(inode, newsize);
4949 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304950 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004951 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004952 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004953 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004954 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004955
Josef Bacika41ad392011-01-31 15:30:16 -05004956 /*
4957 * We're truncating a file that used to have good data down to
4958 * zero. Make sure it gets into the ordered flush list so that
4959 * any new writes get down to disk quickly.
4960 */
4961 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004962 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4963 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004964
Josef Bacikf3fe8202013-01-07 17:03:21 -05004965 /*
4966 * 1 for the orphan item we're going to add
4967 * 1 for the orphan item deletion.
4968 */
4969 trans = btrfs_start_transaction(root, 2);
4970 if (IS_ERR(trans))
4971 return PTR_ERR(trans);
4972
4973 /*
4974 * We need to do this in case we fail at _any_ point during the
4975 * actual truncate. Once we do the truncate_setsize we could
4976 * invalidate pages which forces any outstanding ordered io to
4977 * be instantly completed which will give us extents that need
4978 * to be truncated. If we fail to get an orphan inode down we
4979 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004980 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05004981 * will be consistent.
4982 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004983 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004984 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004985 if (ret)
4986 return ret;
4987
Josef Bacika41ad392011-01-31 15:30:16 -05004988 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4989 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004990
4991 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02004992 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004993 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02004994 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004995
Josef Bacika41ad392011-01-31 15:30:16 -05004996 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004997 if (ret && inode->i_nlink) {
4998 int err;
4999
Liu Bo19fd2df2016-12-01 13:01:02 -08005000 /* To get a stable disk_i_size */
5001 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5002 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005003 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005004 return err;
5005 }
5006
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005007 /*
5008 * failed to truncate, disk_i_size is only adjusted down
5009 * as we remove extents, so it should represent the true
5010 * size of the inode, so reset the in memory size and
5011 * delete our orphan entry.
5012 */
5013 trans = btrfs_join_transaction(root);
5014 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005015 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005016 return ret;
5017 }
5018 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005019 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005020 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005021 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005022 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005023 }
Yan, Zheng80825102009-11-12 09:35:36 +00005024 }
5025
Josef Bacika41ad392011-01-31 15:30:16 -05005026 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005027}
5028
Chris Mason39279cc2007-06-12 06:35:45 -04005029static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5030{
David Howells2b0143b2015-03-17 22:25:59 +00005031 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005032 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005033 int err;
5034
Li Zefanb83cc962010-12-20 16:04:08 +08005035 if (btrfs_root_readonly(root))
5036 return -EROFS;
5037
Jan Kara31051c82016-05-26 16:55:18 +02005038 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005039 if (err)
5040 return err;
5041
Chris Mason5a3f23d2009-03-31 13:27:11 -04005042 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005043 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005044 if (err)
5045 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005046 }
Yan Zheng9036c102008-10-30 14:19:41 -04005047
Christoph Hellwig10257742010-06-04 11:30:02 +02005048 if (attr->ia_valid) {
5049 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005050 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005051 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005052
Josef Bacik22c44fe2011-11-30 10:45:38 -05005053 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005054 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005055 }
5056
Chris Mason39279cc2007-06-12 06:35:45 -04005057 return err;
5058}
Chris Mason61295eb2008-01-14 16:24:38 -05005059
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005060/*
5061 * While truncating the inode pages during eviction, we get the VFS calling
5062 * btrfs_invalidatepage() against each page of the inode. This is slow because
5063 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5064 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5065 * extent_state structures over and over, wasting lots of time.
5066 *
5067 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5068 * those expensive operations on a per page basis and do only the ordered io
5069 * finishing, while we release here the extent_map and extent_state structures,
5070 * without the excessive merging and splitting.
5071 */
5072static void evict_inode_truncate_pages(struct inode *inode)
5073{
5074 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5075 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5076 struct rb_node *node;
5077
5078 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005079 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005080
5081 write_lock(&map_tree->lock);
5082 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5083 struct extent_map *em;
5084
5085 node = rb_first(&map_tree->map);
5086 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005087 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5088 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005089 remove_extent_mapping(map_tree, em);
5090 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005091 if (need_resched()) {
5092 write_unlock(&map_tree->lock);
5093 cond_resched();
5094 write_lock(&map_tree->lock);
5095 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005096 }
5097 write_unlock(&map_tree->lock);
5098
Filipe Manana6ca07092015-05-26 00:55:42 +01005099 /*
5100 * Keep looping until we have no more ranges in the io tree.
5101 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005102 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5103 * still in progress (unlocked the pages in the bio but did not yet
5104 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005105 * ranges can still be locked and eviction started because before
5106 * submitting those bios, which are executed by a separate task (work
5107 * queue kthread), inode references (inode->i_count) were not taken
5108 * (which would be dropped in the end io callback of each bio).
5109 * Therefore here we effectively end up waiting for those bios and
5110 * anyone else holding locked ranges without having bumped the inode's
5111 * reference count - if we don't do it, when they access the inode's
5112 * io_tree to unlock a range it may be too late, leading to an
5113 * use-after-free issue.
5114 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005115 spin_lock(&io_tree->lock);
5116 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5117 struct extent_state *state;
5118 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005119 u64 start;
5120 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005121
5122 node = rb_first(&io_tree->state);
5123 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005124 start = state->start;
5125 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005126 spin_unlock(&io_tree->lock);
5127
David Sterbaff13db42015-12-03 14:30:40 +01005128 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005129
5130 /*
5131 * If still has DELALLOC flag, the extent didn't reach disk,
5132 * and its reserved space won't be freed by delayed_ref.
5133 * So we need to free its reserved space here.
5134 * (Refer to comment in btrfs_invalidatepage, case 2)
5135 *
5136 * Note, end is the bytenr of last byte, so we need + 1 here.
5137 */
5138 if (state->state & EXTENT_DELALLOC)
5139 btrfs_qgroup_free_data(inode, start, end - start + 1);
5140
Filipe Manana6ca07092015-05-26 00:55:42 +01005141 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005142 EXTENT_LOCKED | EXTENT_DIRTY |
5143 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5144 EXTENT_DEFRAG, 1, 1,
5145 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005146
Filipe Manana7064dd52014-08-08 02:47:05 +01005147 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005148 spin_lock(&io_tree->lock);
5149 }
5150 spin_unlock(&io_tree->lock);
5151}
5152
Al Virobd555972010-06-07 11:35:40 -04005153void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005154{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005155 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005156 struct btrfs_trans_handle *trans;
5157 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005158 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005159 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005160 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005161 int ret;
5162
liubo1abe9b82011-03-24 11:18:59 +00005163 trace_btrfs_inode_evict(inode);
5164
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005165 if (!root) {
5166 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5167 return;
5168 }
5169
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005170 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005171
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005172 evict_inode_truncate_pages(inode);
5173
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005174 if (inode->i_nlink &&
5175 ((btrfs_root_refs(&root->root_item) != 0 &&
5176 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005177 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005178 goto no_delete;
5179
Chris Mason39279cc2007-06-12 06:35:45 -04005180 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005181 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005182 goto no_delete;
5183 }
Al Virobd555972010-06-07 11:35:40 -04005184 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005185 if (!special_file(inode->i_mode))
5186 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005187
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005188 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005189
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005190 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005191 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005192 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005193 goto no_delete;
5194 }
5195
Yan, Zheng76dda932009-09-21 16:00:26 -04005196 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005197 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5198 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005199 goto no_delete;
5200 }
5201
Nikolay Borisovaa790212017-01-10 20:35:40 +02005202 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005203 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005204 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005205 goto no_delete;
5206 }
5207
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005208 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005209 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005210 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005211 goto no_delete;
5212 }
Josef Bacik4a338542011-08-29 11:01:31 -04005213 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005214 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005215 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005216
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005217 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005218
Josef Bacik4289a662011-08-05 13:22:24 -04005219 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005220 * This is a bit simpler than btrfs_truncate since we've already
5221 * reserved our space for our orphan item in the unlink, so we just
5222 * need to reserve some slack space in case we add bytes and update
5223 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005224 */
Yan, Zheng80825102009-11-12 09:35:36 +00005225 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005226 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5227 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005228
Josef Bacik726c35f2011-09-26 15:46:06 -04005229 /*
5230 * Try and steal from the global reserve since we will
5231 * likely not use this space anyway, we want to try as
5232 * hard as possible to get this to work.
5233 */
5234 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005235 steal_from_global++;
5236 else
5237 steal_from_global = 0;
5238 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005239
Josef Bacik3bce8762015-02-24 12:35:51 -08005240 /*
5241 * steal_from_global == 0: we reserved stuff, hooray!
5242 * steal_from_global == 1: we didn't reserve stuff, boo!
5243 * steal_from_global == 2: we've committed, still not a lot of
5244 * room but maybe we'll have room in the global reserve this
5245 * time.
5246 * steal_from_global == 3: abandon all hope!
5247 */
5248 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005249 btrfs_warn(fs_info,
5250 "Could not get space for a delete, will truncate on mount %d",
5251 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005252 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005253 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005254 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005255 }
5256
Miao Xie0e8c36a2012-12-19 06:59:51 +00005257 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005258 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005259 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005260 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005261 goto no_delete;
5262 }
5263
Josef Bacik3bce8762015-02-24 12:35:51 -08005264 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005265 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005266 * sure there is room to do it, if not we need to commit and try
5267 * again.
5268 */
5269 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005270 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005271 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005272 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005273 else
5274 ret = -ENOSPC;
5275 }
5276
5277 /*
5278 * Couldn't steal from the global reserve, we have too much
5279 * pending stuff built up, commit the transaction and try it
5280 * again.
5281 */
5282 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005283 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005284 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005285 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005286 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005287 goto no_delete;
5288 }
5289 continue;
5290 } else {
5291 steal_from_global = 0;
5292 }
5293
Josef Bacik4289a662011-08-05 13:22:24 -04005294 trans->block_rsv = rsv;
5295
Yan, Zhengd68fc572010-05-16 10:49:58 -04005296 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005297 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005298 break;
5299
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005300 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005301 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005302 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005303 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005304 }
5305
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005306 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005307
Josef Bacik4ef31a42013-08-13 14:10:08 -04005308 /*
5309 * Errors here aren't a big deal, it just means we leave orphan items
5310 * in the tree. They will be cleaned up on the next mount.
5311 */
Yan, Zheng80825102009-11-12 09:35:36 +00005312 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005313 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005314 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005315 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005316 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005317 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005318
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005319 trans->block_rsv = &fs_info->trans_block_rsv;
5320 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005321 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005322 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005323
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005324 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005325 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005326no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005327 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005328 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005329}
5330
5331/*
5332 * this returns the key found in the dir entry in the location pointer.
5333 * If no dir entries were found, location->objectid is 0.
5334 */
5335static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5336 struct btrfs_key *location)
5337{
5338 const char *name = dentry->d_name.name;
5339 int namelen = dentry->d_name.len;
5340 struct btrfs_dir_item *di;
5341 struct btrfs_path *path;
5342 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005343 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005344
5345 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005346 if (!path)
5347 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005348
David Sterbaf85b7372017-01-20 14:54:07 +01005349 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5350 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005351 if (IS_ERR(di))
5352 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005353
David Sterbac7040052011-04-19 18:00:01 +02005354 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005355 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005356
Chris Mason5f39d392007-10-15 16:14:19 -04005357 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005358out:
Chris Mason39279cc2007-06-12 06:35:45 -04005359 btrfs_free_path(path);
5360 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005361out_err:
5362 location->objectid = 0;
5363 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005364}
5365
5366/*
5367 * when we hit a tree root in a directory, the btrfs part of the inode
5368 * needs to be changed to reflect the root directory of the tree root. This
5369 * is kind of like crossing a mount point.
5370 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005371static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005372 struct inode *dir,
5373 struct dentry *dentry,
5374 struct btrfs_key *location,
5375 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005376{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005377 struct btrfs_path *path;
5378 struct btrfs_root *new_root;
5379 struct btrfs_root_ref *ref;
5380 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005381 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005382 int ret;
5383 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005384
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005385 path = btrfs_alloc_path();
5386 if (!path) {
5387 err = -ENOMEM;
5388 goto out;
5389 }
Chris Mason39279cc2007-06-12 06:35:45 -04005390
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005391 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005392 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5393 key.type = BTRFS_ROOT_REF_KEY;
5394 key.offset = location->objectid;
5395
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005396 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005397 if (ret) {
5398 if (ret < 0)
5399 err = ret;
5400 goto out;
5401 }
Chris Mason39279cc2007-06-12 06:35:45 -04005402
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005403 leaf = path->nodes[0];
5404 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005405 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005406 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5407 goto out;
5408
5409 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5410 (unsigned long)(ref + 1),
5411 dentry->d_name.len);
5412 if (ret)
5413 goto out;
5414
David Sterbab3b4aa72011-04-21 01:20:15 +02005415 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005416
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005417 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005418 if (IS_ERR(new_root)) {
5419 err = PTR_ERR(new_root);
5420 goto out;
5421 }
5422
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005423 *sub_root = new_root;
5424 location->objectid = btrfs_root_dirid(&new_root->root_item);
5425 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005426 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005427 err = 0;
5428out:
5429 btrfs_free_path(path);
5430 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005431}
5432
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005433static void inode_tree_add(struct inode *inode)
5434{
5435 struct btrfs_root *root = BTRFS_I(inode)->root;
5436 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005437 struct rb_node **p;
5438 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005439 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005440 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005441
Al Viro1d3382cb2010-10-23 15:19:20 -04005442 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005443 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005444 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005445 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005446 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005447 while (*p) {
5448 parent = *p;
5449 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5450
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005451 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005452 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005453 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005454 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005455 else {
5456 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005457 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005458 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005459 RB_CLEAR_NODE(parent);
5460 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005461 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005462 }
5463 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005464 rb_link_node(new, parent, p);
5465 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005466 spin_unlock(&root->inode_lock);
5467}
5468
5469static void inode_tree_del(struct inode *inode)
5470{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005471 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005472 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005473 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005474
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005475 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005476 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005477 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005478 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005479 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005480 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005481 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005482
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005483 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005484 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005485 spin_lock(&root->inode_lock);
5486 empty = RB_EMPTY_ROOT(&root->inode_tree);
5487 spin_unlock(&root->inode_lock);
5488 if (empty)
5489 btrfs_add_dead_root(root);
5490 }
5491}
5492
Jeff Mahoney143bede2012-03-01 14:56:26 +01005493void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005494{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005495 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005496 struct rb_node *node;
5497 struct rb_node *prev;
5498 struct btrfs_inode *entry;
5499 struct inode *inode;
5500 u64 objectid = 0;
5501
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005502 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005503 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005504
5505 spin_lock(&root->inode_lock);
5506again:
5507 node = root->inode_tree.rb_node;
5508 prev = NULL;
5509 while (node) {
5510 prev = node;
5511 entry = rb_entry(node, struct btrfs_inode, rb_node);
5512
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005513 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005514 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005515 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005516 node = node->rb_right;
5517 else
5518 break;
5519 }
5520 if (!node) {
5521 while (prev) {
5522 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005523 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005524 node = prev;
5525 break;
5526 }
5527 prev = rb_next(prev);
5528 }
5529 }
5530 while (node) {
5531 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005532 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005533 inode = igrab(&entry->vfs_inode);
5534 if (inode) {
5535 spin_unlock(&root->inode_lock);
5536 if (atomic_read(&inode->i_count) > 1)
5537 d_prune_aliases(inode);
5538 /*
Al Viro45321ac2010-06-07 13:43:19 -04005539 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005540 * the inode cache when its usage count
5541 * hits zero.
5542 */
5543 iput(inode);
5544 cond_resched();
5545 spin_lock(&root->inode_lock);
5546 goto again;
5547 }
5548
5549 if (cond_resched_lock(&root->inode_lock))
5550 goto again;
5551
5552 node = rb_next(node);
5553 }
5554 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005555}
5556
Chris Masone02119d2008-09-05 16:13:11 -04005557static int btrfs_init_locked_inode(struct inode *inode, void *p)
5558{
5559 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005560 inode->i_ino = args->location->objectid;
5561 memcpy(&BTRFS_I(inode)->location, args->location,
5562 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005563 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005564 return 0;
5565}
5566
5567static int btrfs_find_actor(struct inode *inode, void *opaque)
5568{
5569 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005570 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005571 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005572}
5573
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005574static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005575 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005576 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005577{
5578 struct inode *inode;
5579 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005580 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005581
Chris Mason90d3e592014-01-09 17:28:00 -08005582 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005583 args.root = root;
5584
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005585 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005586 btrfs_init_locked_inode,
5587 (void *)&args);
5588 return inode;
5589}
5590
Balaji Rao1a54ef82008-07-21 02:01:04 +05305591/* Get an inode object given its location and corresponding root.
5592 * Returns in *is_new if the inode was read from disk
5593 */
5594struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005595 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305596{
5597 struct inode *inode;
5598
Chris Mason90d3e592014-01-09 17:28:00 -08005599 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305600 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305602
5603 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005604 int ret;
5605
5606 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005607 if (!is_bad_inode(inode)) {
5608 inode_tree_add(inode);
5609 unlock_new_inode(inode);
5610 if (new)
5611 *new = 1;
5612 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005613 unlock_new_inode(inode);
5614 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005615 ASSERT(ret < 0);
5616 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005617 }
5618 }
5619
Balaji Rao1a54ef82008-07-21 02:01:04 +05305620 return inode;
5621}
5622
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005623static struct inode *new_simple_dir(struct super_block *s,
5624 struct btrfs_key *key,
5625 struct btrfs_root *root)
5626{
5627 struct inode *inode = new_inode(s);
5628
5629 if (!inode)
5630 return ERR_PTR(-ENOMEM);
5631
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005632 BTRFS_I(inode)->root = root;
5633 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005634 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005635
5636 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005637 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005638 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005639 inode->i_fop = &simple_dir_operations;
5640 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005641 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305642 inode->i_atime = inode->i_mtime;
5643 inode->i_ctime = inode->i_mtime;
5644 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005645
5646 return inode;
5647}
5648
Chris Mason3de45862008-11-17 21:02:50 -05005649struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005650{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005651 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005652 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005653 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005654 struct btrfs_root *sub_root = root;
5655 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005656 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005657 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005658
5659 if (dentry->d_name.len > BTRFS_NAME_LEN)
5660 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005661
Jeff Layton39e3c952012-11-28 11:30:53 -05005662 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005663 if (ret < 0)
5664 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005665
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005666 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005667 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005668
5669 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005670 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005671 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005672 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005673
5674 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5675
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005676 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005677 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005678 &location, &sub_root);
5679 if (ret < 0) {
5680 if (ret != -ENOENT)
5681 inode = ERR_PTR(ret);
5682 else
5683 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5684 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005685 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005686 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005687 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005688
Julia Lawall34d19ba2011-01-24 19:55:19 +00005689 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005690 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005691 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005692 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005693 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005694 if (ret) {
5695 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005696 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005697 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005698 }
5699
Chris Mason3de45862008-11-17 21:02:50 -05005700 return inode;
5701}
5702
Nick Pigginfe15ce42011-01-07 17:49:23 +11005703static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005704{
5705 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005706 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005707
Li Zefan848cce02012-02-21 17:04:28 +08005708 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005709 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005710
Li Zefan848cce02012-02-21 17:04:28 +08005711 if (inode) {
5712 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005713 if (btrfs_root_refs(&root->root_item) == 0)
5714 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005715
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005716 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005717 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005718 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005719 return 0;
5720}
5721
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005722static void btrfs_dentry_release(struct dentry *dentry)
5723{
Daeseok Youn944a4512014-04-14 15:37:02 +09005724 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005725}
5726
Chris Mason3de45862008-11-17 21:02:50 -05005727static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005728 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005729{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005730 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005731
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005732 inode = btrfs_lookup_dentry(dir, dentry);
5733 if (IS_ERR(inode)) {
5734 if (PTR_ERR(inode) == -ENOENT)
5735 inode = NULL;
5736 else
5737 return ERR_CAST(inode);
5738 }
5739
Al Viro41d28bc2014-10-12 22:24:21 -04005740 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005741}
5742
Miao Xie16cdcec2011-04-22 18:12:22 +08005743unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005744 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5745};
5746
Al Viro9cdda8d2013-05-22 16:48:09 -04005747static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005748{
Al Viro9cdda8d2013-05-22 16:48:09 -04005749 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005750 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005751 struct btrfs_root *root = BTRFS_I(inode)->root;
5752 struct btrfs_item *item;
5753 struct btrfs_dir_item *di;
5754 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005755 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005756 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005757 struct list_head ins_list;
5758 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005759 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005760 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005761 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005762 unsigned char d_type;
5763 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005764 char tmp_name[32];
5765 char *name_ptr;
5766 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005767 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005768 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005769
Al Viro9cdda8d2013-05-22 16:48:09 -04005770 if (!dir_emit_dots(file, ctx))
5771 return 0;
5772
David Woodhouse49593bf2008-08-17 17:08:36 +01005773 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005774 if (!path)
5775 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005776
David Sterbae4058b52015-11-27 16:31:35 +01005777 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005778
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005779 INIT_LIST_HEAD(&ins_list);
5780 INIT_LIST_HEAD(&del_list);
5781 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005782
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005783 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005784 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005785 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005786
Chris Mason39279cc2007-06-12 06:35:45 -04005787 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5788 if (ret < 0)
5789 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005790
5791 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005792 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005793 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005794 if (slot >= btrfs_header_nritems(leaf)) {
5795 ret = btrfs_next_leaf(root, path);
5796 if (ret < 0)
5797 goto err;
5798 else if (ret > 0)
5799 break;
5800 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005801 }
Chris Mason3de45862008-11-17 21:02:50 -05005802
Ross Kirkdd3cc162013-09-16 15:58:09 +01005803 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005804 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5805
5806 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005807 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005808 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005809 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005810 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005811 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005812 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005813 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005814
Al Viro9cdda8d2013-05-22 16:48:09 -04005815 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005816
Chris Mason39279cc2007-06-12 06:35:45 -04005817 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005818 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005819 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005820
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005821 name_len = btrfs_dir_name_len(leaf, di);
5822 if (name_len <= sizeof(tmp_name)) {
5823 name_ptr = tmp_name;
5824 } else {
5825 name_ptr = kmalloc(name_len, GFP_KERNEL);
5826 if (!name_ptr) {
5827 ret = -ENOMEM;
5828 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005829 }
Chris Mason39279cc2007-06-12 06:35:45 -04005830 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005831 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5832 name_len);
5833
5834 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5835 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5836
5837 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5838 d_type);
5839
5840 if (name_ptr != tmp_name)
5841 kfree(name_ptr);
5842
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005843 if (over)
5844 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005845 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005846next:
5847 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005848 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005849
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005850 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005851 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005852 goto nopos;
5853
Zach Browndb62efb2013-07-11 16:19:42 -07005854 /*
5855 * Stop new entries from being returned after we return the last
5856 * entry.
5857 *
5858 * New directory entries are assigned a strictly increasing
5859 * offset. This means that new entries created during readdir
5860 * are *guaranteed* to be seen in the future by that readdir.
5861 * This has broken buggy programs which operate on names as
5862 * they're returned by readdir. Until we re-use freed offsets
5863 * we have this hack to stop new entries from being returned
5864 * under the assumption that they'll never reach this huge
5865 * offset.
5866 *
5867 * This is being careful not to overflow 32bit loff_t unless the
5868 * last entry requires it because doing so has broken 32bit apps
5869 * in the past.
5870 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005871 if (ctx->pos >= INT_MAX)
5872 ctx->pos = LLONG_MAX;
5873 else
5874 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005875nopos:
5876 ret = 0;
5877err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005878 if (put)
5879 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005880 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005881 return ret;
5882}
5883
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005884int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005885{
5886 struct btrfs_root *root = BTRFS_I(inode)->root;
5887 struct btrfs_trans_handle *trans;
5888 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005889 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005890
Josef Bacik72ac3c02012-05-23 14:13:11 -04005891 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005892 return 0;
5893
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005894 if (btrfs_fs_closing(root->fs_info) &&
5895 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08005896 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005897
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005898 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005899 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005900 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005901 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005902 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005903 if (IS_ERR(trans))
5904 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005905 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005906 }
5907 return ret;
5908}
5909
5910/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005911 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005912 * inode changes. But, it is most likely to find the inode in cache.
5913 * FIXME, needs more benchmarking...there are no reasons other than performance
5914 * to keep or drop this code.
5915 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005916static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005917{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005918 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005919 struct btrfs_root *root = BTRFS_I(inode)->root;
5920 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005921 int ret;
5922
Josef Bacik72ac3c02012-05-23 14:13:11 -04005923 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005924 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005925
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005926 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005927 if (IS_ERR(trans))
5928 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005929
5930 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005931 if (ret && ret == -ENOSPC) {
5932 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005933 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005934 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005935 if (IS_ERR(trans))
5936 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005937
Chris Mason94b60442010-05-26 11:02:00 -04005938 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005939 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005940 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005941 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005942 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005943
5944 return ret;
5945}
5946
5947/*
5948 * This is a copy of file_update_time. We need this so we can return error on
5949 * ENOSPC for updating the inode in the case of file write and mmap writes.
5950 */
Josef Bacike41f9412012-03-26 09:46:47 -04005951static int btrfs_update_time(struct inode *inode, struct timespec *now,
5952 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005953{
Alexander Block2bc5565282012-06-15 09:49:33 +02005954 struct btrfs_root *root = BTRFS_I(inode)->root;
5955
5956 if (btrfs_root_readonly(root))
5957 return -EROFS;
5958
Josef Bacike41f9412012-03-26 09:46:47 -04005959 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005960 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005961 if (flags & S_CTIME)
5962 inode->i_ctime = *now;
5963 if (flags & S_MTIME)
5964 inode->i_mtime = *now;
5965 if (flags & S_ATIME)
5966 inode->i_atime = *now;
5967 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005968}
5969
Chris Masond352ac62008-09-29 15:18:18 -04005970/*
5971 * find the highest existing sequence number in a directory
5972 * and then set the in-memory index_cnt variable to reflect
5973 * free sequence numbers
5974 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005975static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005976{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005977 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005978 struct btrfs_key key, found_key;
5979 struct btrfs_path *path;
5980 struct extent_buffer *leaf;
5981 int ret;
5982
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005983 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005984 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005985 key.offset = (u64)-1;
5986
5987 path = btrfs_alloc_path();
5988 if (!path)
5989 return -ENOMEM;
5990
5991 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5992 if (ret < 0)
5993 goto out;
5994 /* FIXME: we should be able to handle this */
5995 if (ret == 0)
5996 goto out;
5997 ret = 0;
5998
5999 /*
6000 * MAGIC NUMBER EXPLANATION:
6001 * since we search a directory based on f_pos we have to start at 2
6002 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6003 * else has to start at 2
6004 */
6005 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006006 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006007 goto out;
6008 }
6009
6010 path->slots[0]--;
6011
6012 leaf = path->nodes[0];
6013 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6014
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006015 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006016 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006017 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006018 goto out;
6019 }
6020
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006021 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006022out:
6023 btrfs_free_path(path);
6024 return ret;
6025}
6026
Chris Masond352ac62008-09-29 15:18:18 -04006027/*
6028 * helper to find a free sequence number in a given directory. This current
6029 * code is very simple, later versions will do smarter things in the btree
6030 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006031int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006032{
6033 int ret = 0;
6034
Nikolay Borisov877574e2017-02-20 13:50:33 +02006035 if (dir->index_cnt == (u64)-1) {
6036 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006037 if (ret) {
Nikolay Borisov877574e2017-02-20 13:50:33 +02006038 ret = btrfs_set_inode_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006039 if (ret)
6040 return ret;
6041 }
Josef Bacikaec74772008-07-24 12:12:38 -04006042 }
6043
Nikolay Borisov877574e2017-02-20 13:50:33 +02006044 *index = dir->index_cnt;
6045 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006046
6047 return ret;
6048}
6049
Chris Masonb0d5d102014-09-08 13:08:51 -07006050static int btrfs_insert_inode_locked(struct inode *inode)
6051{
6052 struct btrfs_iget_args args;
6053 args.location = &BTRFS_I(inode)->location;
6054 args.root = BTRFS_I(inode)->root;
6055
6056 return insert_inode_locked4(inode,
6057 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6058 btrfs_find_actor, &args);
6059}
6060
Chris Mason39279cc2007-06-12 06:35:45 -04006061static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6062 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006063 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006064 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006065 u64 ref_objectid, u64 objectid,
6066 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006067{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006068 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006069 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006070 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006071 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006072 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006073 struct btrfs_inode_ref *ref;
6074 struct btrfs_key key[2];
6075 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006076 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006077 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006078 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006079
Chris Mason5f39d392007-10-15 16:14:19 -04006080 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006081 if (!path)
6082 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006083
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006084 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006085 if (!inode) {
6086 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006087 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006088 }
Chris Mason39279cc2007-06-12 06:35:45 -04006089
Li Zefan581bb052011-04-20 10:06:11 +08006090 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006091 * O_TMPFILE, set link count to 0, so that after this point,
6092 * we fill in an inode item with the correct link count.
6093 */
6094 if (!name)
6095 set_nlink(inode, 0);
6096
6097 /*
Li Zefan581bb052011-04-20 10:06:11 +08006098 * we have to initialize this early, so we can reclaim the inode
6099 * number if we fail afterwards in this function.
6100 */
6101 inode->i_ino = objectid;
6102
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006103 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006104 trace_btrfs_inode_request(dir);
6105
Nikolay Borisov877574e2017-02-20 13:50:33 +02006106 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006107 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006108 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006109 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006110 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006111 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006112 } else if (dir) {
6113 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006114 }
6115 /*
6116 * index_cnt is ignored for everything but a dir,
6117 * btrfs_get_inode_index_count has an explanation for the magic
6118 * number
6119 */
6120 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006121 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006122 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006123 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006124 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006125
Josef Bacik5dc562c2012-08-17 13:14:17 -04006126 /*
6127 * We could have gotten an inode number from somebody who was fsynced
6128 * and then removed in this same transaction, so let's just set full
6129 * sync since it will be a full sync anyway and this will blow away the
6130 * old info in the log.
6131 */
6132 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6133
Chris Mason9c583092008-01-29 15:15:18 -05006134 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006135 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006136 key[0].offset = 0;
6137
Chris Mason9c583092008-01-29 15:15:18 -05006138 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006139
6140 if (name) {
6141 /*
6142 * Start new inodes with an inode_ref. This is slightly more
6143 * efficient for small numbers of hard links since they will
6144 * be packed into one item. Extended refs will kick in if we
6145 * add more hard links than can fit in the ref item.
6146 */
6147 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006148 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006149 key[1].offset = ref_objectid;
6150
6151 sizes[1] = name_len + sizeof(*ref);
6152 }
Chris Mason9c583092008-01-29 15:15:18 -05006153
Chris Masonb0d5d102014-09-08 13:08:51 -07006154 location = &BTRFS_I(inode)->location;
6155 location->objectid = objectid;
6156 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006157 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006158
6159 ret = btrfs_insert_inode_locked(inode);
6160 if (ret < 0)
6161 goto fail;
6162
Chris Masonb9473432009-03-13 11:00:37 -04006163 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006164 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006165 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006166 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006167
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006168 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006169 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306170
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006171 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306172 inode->i_atime = inode->i_mtime;
6173 inode->i_ctime = inode->i_mtime;
6174 BTRFS_I(inode)->i_otime = inode->i_mtime;
6175
Chris Mason5f39d392007-10-15 16:14:19 -04006176 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6177 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006178 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006179 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006180 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006181
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006182 if (name) {
6183 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6184 struct btrfs_inode_ref);
6185 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6186 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6187 ptr = (unsigned long)(ref + 1);
6188 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6189 }
Chris Mason9c583092008-01-29 15:15:18 -05006190
Chris Mason5f39d392007-10-15 16:14:19 -04006191 btrfs_mark_buffer_dirty(path->nodes[0]);
6192 btrfs_free_path(path);
6193
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006194 btrfs_inherit_iflags(inode, dir);
6195
Al Viro569254b2011-07-24 17:08:40 -04006196 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006197 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006198 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006199 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006200 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6201 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006202 }
6203
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006204 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006205
6206 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006207 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006208
Alexander Block8ea05e32012-07-25 17:35:53 +02006209 btrfs_update_root_times(trans, root);
6210
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006211 ret = btrfs_inode_inherit_props(trans, inode, dir);
6212 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006213 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006214 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006215 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006216
Chris Mason39279cc2007-06-12 06:35:45 -04006217 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006218
6219fail_unlock:
6220 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006221fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006222 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006223 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006224 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006225 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006226 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006227}
6228
6229static inline u8 btrfs_inode_type(struct inode *inode)
6230{
6231 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6232}
6233
Chris Masond352ac62008-09-29 15:18:18 -04006234/*
6235 * utility function to add 'inode' into 'parent_inode' with
6236 * a give name and a given sequence number.
6237 * if 'add_backref' is true, also insert a backref from the
6238 * inode to the parent directory.
6239 */
Chris Masone02119d2008-09-05 16:13:11 -04006240int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006241 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006242 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006243{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006244 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006245 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006246 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006247 struct btrfs_root *root = parent_inode->root;
6248 u64 ino = btrfs_ino(inode);
6249 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006250
Li Zefan33345d012011-04-20 10:31:50 +08006251 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006252 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006253 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006254 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006255 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006256 key.offset = 0;
6257 }
Chris Mason39279cc2007-06-12 06:35:45 -04006258
Li Zefan33345d012011-04-20 10:31:50 +08006259 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006260 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6261 root->root_key.objectid, parent_ino,
6262 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006263 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006264 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6265 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006266 }
6267
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006268 /* Nothing to clean up yet */
6269 if (ret)
6270 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006271
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006272 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006273 parent_inode, &key,
6274 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006275 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006276 goto fail_dir_item;
6277 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006278 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006279 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006280 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006281
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006282 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006283 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006284 inode_inc_iversion(&parent_inode->vfs_inode);
6285 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6286 current_time(&parent_inode->vfs_inode);
6287 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006288 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006289 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006290 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006291
6292fail_dir_item:
6293 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6294 u64 local_index;
6295 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006296 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6297 root->root_key.objectid, parent_ino,
6298 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006299
6300 } else if (add_backref) {
6301 u64 local_index;
6302 int err;
6303
6304 err = btrfs_del_inode_ref(trans, root, name, name_len,
6305 ino, parent_ino, &local_index);
6306 }
6307 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006308}
6309
6310static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006311 struct btrfs_inode *dir, struct dentry *dentry,
6312 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006313{
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006314 int err = btrfs_add_link(trans, dir, inode,
Josef Bacika1b075d2010-11-19 20:36:11 +00006315 dentry->d_name.name, dentry->d_name.len,
6316 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006317 if (err > 0)
6318 err = -EEXIST;
6319 return err;
6320}
6321
Josef Bacik618e21d2007-07-11 10:18:17 -04006322static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006323 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006324{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006325 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006326 struct btrfs_trans_handle *trans;
6327 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006328 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006329 int err;
6330 int drop_inode = 0;
6331 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006332 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006333
Josef Bacik9ed74f22009-09-11 16:12:44 -04006334 /*
6335 * 2 for inode item and ref
6336 * 2 for dir items
6337 * 1 for xattr if selinux is on
6338 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006339 trans = btrfs_start_transaction(root, 5);
6340 if (IS_ERR(trans))
6341 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006342
Li Zefan581bb052011-04-20 10:06:11 +08006343 err = btrfs_find_free_ino(root, &objectid);
6344 if (err)
6345 goto out_unlock;
6346
Josef Bacikaec74772008-07-24 12:12:38 -04006347 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006348 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6349 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006350 if (IS_ERR(inode)) {
6351 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006352 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006353 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006354
Casey Schauflerad19db72011-12-15 10:09:07 -05006355 /*
6356 * If the active LSM wants to access the inode during
6357 * d_instantiate it needs these. Smack checks to see
6358 * if the filesystem supports xattrs by looking at the
6359 * ops vector.
6360 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006361 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006362 init_special_inode(inode, inode->i_mode, rdev);
6363
6364 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006365 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006366 goto out_unlock_inode;
6367
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006368 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6369 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006370 if (err) {
6371 goto out_unlock_inode;
6372 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006373 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006374 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006375 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006376 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006377
Josef Bacik618e21d2007-07-11 10:18:17 -04006378out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006379 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006380 btrfs_balance_delayed_items(fs_info);
6381 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006382 if (drop_inode) {
6383 inode_dec_link_count(inode);
6384 iput(inode);
6385 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006386 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006387
6388out_unlock_inode:
6389 drop_inode = 1;
6390 unlock_new_inode(inode);
6391 goto out_unlock;
6392
Josef Bacik618e21d2007-07-11 10:18:17 -04006393}
6394
Chris Mason39279cc2007-06-12 06:35:45 -04006395static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006396 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006397{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006398 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006399 struct btrfs_trans_handle *trans;
6400 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006401 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006402 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006403 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006404 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006405 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006406
Josef Bacik9ed74f22009-09-11 16:12:44 -04006407 /*
6408 * 2 for inode item and ref
6409 * 2 for dir items
6410 * 1 for xattr if selinux is on
6411 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006412 trans = btrfs_start_transaction(root, 5);
6413 if (IS_ERR(trans))
6414 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006415
Li Zefan581bb052011-04-20 10:06:11 +08006416 err = btrfs_find_free_ino(root, &objectid);
6417 if (err)
6418 goto out_unlock;
6419
Josef Bacikaec74772008-07-24 12:12:38 -04006420 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006421 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6422 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006423 if (IS_ERR(inode)) {
6424 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006425 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006426 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006427 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006428 /*
6429 * If the active LSM wants to access the inode during
6430 * d_instantiate it needs these. Smack checks to see
6431 * if the filesystem supports xattrs by looking at the
6432 * ops vector.
6433 */
6434 inode->i_fop = &btrfs_file_operations;
6435 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006436 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006437
6438 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6439 if (err)
6440 goto out_unlock_inode;
6441
6442 err = btrfs_update_inode(trans, root, inode);
6443 if (err)
6444 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006445
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006446 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6447 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006448 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006449 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006450
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006451 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006452 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006453 d_instantiate(dentry, inode);
6454
Chris Mason39279cc2007-06-12 06:35:45 -04006455out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006456 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006457 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006458 inode_dec_link_count(inode);
6459 iput(inode);
6460 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006461 btrfs_balance_delayed_items(fs_info);
6462 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006463 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006464
6465out_unlock_inode:
6466 unlock_new_inode(inode);
6467 goto out_unlock;
6468
Chris Mason39279cc2007-06-12 06:35:45 -04006469}
6470
6471static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6472 struct dentry *dentry)
6473{
Filipe Manana271dba452016-01-05 16:24:05 +00006474 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006475 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006476 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006477 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006478 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006479 int err;
6480 int drop_inode = 0;
6481
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006482 /* do not allow sys_link's with other subvols of the same device */
6483 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006484 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006485
Mark Fashehf1863732012-08-08 11:32:27 -07006486 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006487 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006488
Nikolay Borisov877574e2017-02-20 13:50:33 +02006489 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006490 if (err)
6491 goto fail;
6492
Yan, Zhenga22285a2010-05-16 10:48:46 -04006493 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006494 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006495 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006496 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006497 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006498 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006499 if (IS_ERR(trans)) {
6500 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006501 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006502 goto fail;
6503 }
Chris Mason5f39d392007-10-15 16:14:19 -04006504
Miao Xie67de1172013-12-26 13:07:06 +08006505 /* There are several dir indexes for this inode, clear the cache. */
6506 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006507 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006508 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006509 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006510 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006511 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006512
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006513 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6514 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006515
Yan, Zhenga5719522009-09-24 09:17:31 -04006516 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006517 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006518 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006519 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006520 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006521 if (err)
6522 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006523 if (inode->i_nlink == 1) {
6524 /*
6525 * If new hard link count is 1, it's a file created
6526 * with open(2) O_TMPFILE flag.
6527 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006528 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006529 if (err)
6530 goto fail;
6531 }
Al Viro08c422c2011-12-23 07:58:13 -05006532 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006533 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006534 }
Chris Mason39279cc2007-06-12 06:35:45 -04006535
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006536 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006537fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006538 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006539 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006540 if (drop_inode) {
6541 inode_dec_link_count(inode);
6542 iput(inode);
6543 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006544 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006545 return err;
6546}
6547
Al Viro18bb1db2011-07-26 01:41:39 -04006548static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006549{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006550 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006551 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006552 struct btrfs_trans_handle *trans;
6553 struct btrfs_root *root = BTRFS_I(dir)->root;
6554 int err = 0;
6555 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006556 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006557 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006558
Josef Bacik9ed74f22009-09-11 16:12:44 -04006559 /*
6560 * 2 items for inode and ref
6561 * 2 items for dir items
6562 * 1 for xattr if selinux is on
6563 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006564 trans = btrfs_start_transaction(root, 5);
6565 if (IS_ERR(trans))
6566 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006567
Li Zefan581bb052011-04-20 10:06:11 +08006568 err = btrfs_find_free_ino(root, &objectid);
6569 if (err)
6570 goto out_fail;
6571
Josef Bacikaec74772008-07-24 12:12:38 -04006572 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006573 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6574 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006575 if (IS_ERR(inode)) {
6576 err = PTR_ERR(inode);
6577 goto out_fail;
6578 }
Chris Mason5f39d392007-10-15 16:14:19 -04006579
Chris Mason39279cc2007-06-12 06:35:45 -04006580 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006581 /* these must be set before we unlock the inode */
6582 inode->i_op = &btrfs_dir_inode_operations;
6583 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006584
Eric Paris2a7dba32011-02-01 11:05:39 -05006585 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006586 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006587 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006588
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006589 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006590 err = btrfs_update_inode(trans, root, inode);
6591 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006592 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006593
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006594 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6595 dentry->d_name.name,
6596 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006597 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006598 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006599
Chris Mason39279cc2007-06-12 06:35:45 -04006600 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006601 /*
6602 * mkdir is special. We're unlocking after we call d_instantiate
6603 * to avoid a race with nfsd calling d_instantiate.
6604 */
6605 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006606 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006607
6608out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006609 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006610 if (drop_on_err) {
6611 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006612 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006613 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006614 btrfs_balance_delayed_items(fs_info);
6615 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006616 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006617
6618out_fail_inode:
6619 unlock_new_inode(inode);
6620 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006621}
6622
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006623/* Find next extent map of a given extent map, caller needs to ensure locks */
6624static struct extent_map *next_extent_map(struct extent_map *em)
6625{
6626 struct rb_node *next;
6627
6628 next = rb_next(&em->rb_node);
6629 if (!next)
6630 return NULL;
6631 return container_of(next, struct extent_map, rb_node);
6632}
6633
6634static struct extent_map *prev_extent_map(struct extent_map *em)
6635{
6636 struct rb_node *prev;
6637
6638 prev = rb_prev(&em->rb_node);
6639 if (!prev)
6640 return NULL;
6641 return container_of(prev, struct extent_map, rb_node);
6642}
6643
Chris Masond352ac62008-09-29 15:18:18 -04006644/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006645 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006646 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006647 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006648 */
Chris Mason3b951512008-04-17 11:29:12 -04006649static int merge_extent_mapping(struct extent_map_tree *em_tree,
6650 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006651 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006652 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006653{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006654 struct extent_map *prev;
6655 struct extent_map *next;
6656 u64 start;
6657 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006658 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006659
Chris Masone6dcd2d2008-07-17 12:53:50 -04006660 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006661
6662 if (existing->start > map_start) {
6663 next = existing;
6664 prev = prev_extent_map(next);
6665 } else {
6666 prev = existing;
6667 next = next_extent_map(prev);
6668 }
6669
6670 start = prev ? extent_map_end(prev) : em->start;
6671 start = max_t(u64, start, em->start);
6672 end = next ? next->start : extent_map_end(em);
6673 end = min_t(u64, end, extent_map_end(em));
6674 start_diff = start - em->start;
6675 em->start = start;
6676 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006677 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6678 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006679 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006680 em->block_len -= start_diff;
6681 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006682 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006683}
6684
Chris Masonc8b97812008-10-29 14:49:59 -04006685static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006686 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006687 size_t pg_offset, u64 extent_offset,
6688 struct btrfs_file_extent_item *item)
6689{
6690 int ret;
6691 struct extent_buffer *leaf = path->nodes[0];
6692 char *tmp;
6693 size_t max_size;
6694 unsigned long inline_size;
6695 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006696 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006697
6698 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006699 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006700 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6701 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006702 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006703 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006704 if (!tmp)
6705 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006706 ptr = btrfs_file_extent_inline_start(item);
6707
6708 read_extent_buffer(leaf, tmp, ptr, inline_size);
6709
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006710 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006711 ret = btrfs_decompress(compress_type, tmp, page,
6712 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006713 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006714 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006715}
6716
Chris Masond352ac62008-09-29 15:18:18 -04006717/*
6718 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006719 * the ugly parts come from merging extents from the disk with the in-ram
6720 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006721 * where the in-ram extents might be locked pending data=ordered completion.
6722 *
6723 * This also copies inline extents directly into the page.
6724 */
Chris Masond3977122009-01-05 21:25:51 -05006725
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006726struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6727 struct page *page,
6728 size_t pg_offset, u64 start, u64 len,
6729 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006730{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006731 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006732 int ret;
6733 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006734 u64 extent_start = 0;
6735 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006736 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006737 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006738 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006739 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006740 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006741 struct extent_buffer *leaf;
6742 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006743 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006744 struct extent_map_tree *em_tree = &inode->extent_tree;
6745 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006746 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006747 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006748
Chris Masona52d9a82007-08-27 16:49:44 -04006749again:
Chris Mason890871b2009-09-02 16:24:52 -04006750 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006751 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006752 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006753 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006754 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006755
Chris Masona52d9a82007-08-27 16:49:44 -04006756 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006757 if (em->start > start || em->start + em->len <= start)
6758 free_extent_map(em);
6759 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006760 free_extent_map(em);
6761 else
6762 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006763 }
David Sterba172ddd62011-04-21 00:48:27 +02006764 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006765 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006766 err = -ENOMEM;
6767 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006768 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006769 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006770 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006771 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006772 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006773 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006774
6775 if (!path) {
6776 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006777 if (!path) {
6778 err = -ENOMEM;
6779 goto out;
6780 }
6781 /*
6782 * Chances are we'll be called again, so go ahead and do
6783 * readahead
6784 */
David Sterbae4058b52015-11-27 16:31:35 +01006785 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006786 }
6787
Chris Mason179e29e2007-11-01 11:28:41 -04006788 ret = btrfs_lookup_file_extent(trans, root, path,
6789 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006790 if (ret < 0) {
6791 err = ret;
6792 goto out;
6793 }
6794
6795 if (ret != 0) {
6796 if (path->slots[0] == 0)
6797 goto not_found;
6798 path->slots[0]--;
6799 }
6800
Chris Mason5f39d392007-10-15 16:14:19 -04006801 leaf = path->nodes[0];
6802 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006803 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006804 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006805 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006806 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006807 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006808 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006809 /*
6810 * If we backup past the first extent we want to move forward
6811 * and see if there is an extent in front of us, otherwise we'll
6812 * say there is a hole for our whole search range which can
6813 * cause problems.
6814 */
6815 extent_end = start;
6816 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006817 }
6818
Chris Mason5f39d392007-10-15 16:14:19 -04006819 found_type = btrfs_file_extent_type(leaf, item);
6820 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006821 if (found_type == BTRFS_FILE_EXTENT_REG ||
6822 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006823 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006824 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006825 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6826 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006827 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006828 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006829 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006830 }
Josef Bacik25a50342013-10-14 12:08:38 -04006831next:
Yan Zheng9036c102008-10-30 14:19:41 -04006832 if (start >= extent_end) {
6833 path->slots[0]++;
6834 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6835 ret = btrfs_next_leaf(root, path);
6836 if (ret < 0) {
6837 err = ret;
6838 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006839 }
Yan Zheng9036c102008-10-30 14:19:41 -04006840 if (ret > 0)
6841 goto not_found;
6842 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006843 }
Yan Zheng9036c102008-10-30 14:19:41 -04006844 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6845 if (found_key.objectid != objectid ||
6846 found_key.type != BTRFS_EXTENT_DATA_KEY)
6847 goto not_found;
6848 if (start + len <= found_key.offset)
6849 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006850 if (start > found_key.offset)
6851 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006852 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006853 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006854 em->len = found_key.offset - start;
6855 goto not_found_em;
6856 }
6857
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006858 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006859 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006860
Yan Zhengd899e052008-10-30 14:25:28 -04006861 if (found_type == BTRFS_FILE_EXTENT_REG ||
6862 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006863 goto insert;
6864 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006865 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006866 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006867 size_t size;
6868 size_t extent_offset;
6869 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006870
Filipe Manana7ffbb592014-06-09 03:48:05 +01006871 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006872 goto out;
Yan689f9342007-10-29 11:41:07 -04006873
Chris Mason514ac8a2014-01-03 21:07:00 -08006874 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006875 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006876 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6877 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006878 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006879 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006880 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006881 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006882 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006883 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006884 if (btrfs_file_extent_compression(leaf, item) !=
6885 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006886 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006887 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006888 if (ret) {
6889 err = ret;
6890 goto out;
6891 }
Chris Masonc8b97812008-10-29 14:49:59 -04006892 } else {
6893 map = kmap(page);
6894 read_extent_buffer(leaf, map + pg_offset, ptr,
6895 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006896 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006897 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006898 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006899 copy_size);
6900 }
Chris Masonc8b97812008-10-29 14:49:59 -04006901 kunmap(page);
6902 }
Chris Mason179e29e2007-11-01 11:28:41 -04006903 flush_dcache_page(page);
6904 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006905 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006906 if (!trans) {
6907 kunmap(page);
6908 free_extent_map(em);
6909 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006910
David Sterbab3b4aa72011-04-21 01:20:15 +02006911 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006912 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006913
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006914 if (IS_ERR(trans))
6915 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006916 goto again;
6917 }
Chris Masonc8b97812008-10-29 14:49:59 -04006918 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006919 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006920 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006921 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006922 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006923 }
Chris Masond1310b22008-01-24 16:13:08 -05006924 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006925 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006926 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006927 }
6928not_found:
6929 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006930 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006931 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006932not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006933 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006934 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006935insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006936 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006937 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006938 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006939 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6940 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006941 err = -EIO;
6942 goto out;
6943 }
Chris Masond1310b22008-01-24 16:13:08 -05006944
6945 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006946 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006947 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006948 /* it is possible that someone inserted the extent into the tree
6949 * while we had the lock dropped. It is also possible that
6950 * an overlapping map exists in the tree
6951 */
Chris Masona52d9a82007-08-27 16:49:44 -04006952 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006953 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006954
6955 ret = 0;
6956
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006957 existing = search_extent_mapping(em_tree, start, len);
6958 /*
6959 * existing will always be non-NULL, since there must be
6960 * extent causing the -EEXIST.
6961 */
Chris Mason8dff9c82015-09-19 11:28:25 -07006962 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006963 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07006964 em->block_start == existing->block_start) {
6965 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006966 * The existing extent map already encompasses the
6967 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07006968 */
6969 free_extent_map(em);
6970 em = existing;
6971 err = 0;
6972
6973 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006974 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006975 /*
6976 * The existing extent map is the one nearest to
6977 * the [start, start + len) range which overlaps
6978 */
6979 err = merge_extent_mapping(em_tree, existing,
6980 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006981 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006982 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006983 free_extent_map(em);
6984 em = NULL;
6985 }
6986 } else {
6987 free_extent_map(em);
6988 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006989 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006990 }
Chris Masona52d9a82007-08-27 16:49:44 -04006991 }
Chris Mason890871b2009-09-02 16:24:52 -04006992 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006993out:
liubo1abe9b82011-03-24 11:18:59 +00006994
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006995 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006996
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006997 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006998 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006999 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007000 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007001 err = ret;
7002 }
Chris Masona52d9a82007-08-27 16:49:44 -04007003 if (err) {
7004 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007005 return ERR_PTR(err);
7006 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007007 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007008 return em;
7009}
7010
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007011struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7012 struct page *page,
7013 size_t pg_offset, u64 start, u64 len,
7014 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007015{
7016 struct extent_map *em;
7017 struct extent_map *hole_em = NULL;
7018 u64 range_start = start;
7019 u64 end;
7020 u64 found;
7021 u64 found_end;
7022 int err = 0;
7023
7024 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7025 if (IS_ERR(em))
7026 return em;
7027 if (em) {
7028 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007029 * if our em maps to
7030 * - a hole or
7031 * - a pre-alloc extent,
7032 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007033 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007034 if (em->block_start != EXTENT_MAP_HOLE &&
7035 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007036 return em;
7037 else
7038 hole_em = em;
7039 }
7040
7041 /* check to see if we've wrapped (len == -1 or similar) */
7042 end = start + len;
7043 if (end < start)
7044 end = (u64)-1;
7045 else
7046 end -= 1;
7047
7048 em = NULL;
7049
7050 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007051 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007052 end, len, EXTENT_DELALLOC, 1);
7053 found_end = range_start + found;
7054 if (found_end < range_start)
7055 found_end = (u64)-1;
7056
7057 /*
7058 * we didn't find anything useful, return
7059 * the original results from get_extent()
7060 */
7061 if (range_start > end || found_end <= start) {
7062 em = hole_em;
7063 hole_em = NULL;
7064 goto out;
7065 }
7066
7067 /* adjust the range_start to make sure it doesn't
7068 * go backwards from the start they passed in
7069 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307070 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007071 found = found_end - range_start;
7072
7073 if (found > 0) {
7074 u64 hole_start = start;
7075 u64 hole_len = len;
7076
David Sterba172ddd62011-04-21 00:48:27 +02007077 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007078 if (!em) {
7079 err = -ENOMEM;
7080 goto out;
7081 }
7082 /*
7083 * when btrfs_get_extent can't find anything it
7084 * returns one huge hole
7085 *
7086 * make sure what it found really fits our range, and
7087 * adjust to make sure it is based on the start from
7088 * the caller
7089 */
7090 if (hole_em) {
7091 u64 calc_end = extent_map_end(hole_em);
7092
7093 if (calc_end <= start || (hole_em->start > end)) {
7094 free_extent_map(hole_em);
7095 hole_em = NULL;
7096 } else {
7097 hole_start = max(hole_em->start, start);
7098 hole_len = calc_end - hole_start;
7099 }
7100 }
7101 em->bdev = NULL;
7102 if (hole_em && range_start > hole_start) {
7103 /* our hole starts before our delalloc, so we
7104 * have to return just the parts of the hole
7105 * that go until the delalloc starts
7106 */
7107 em->len = min(hole_len,
7108 range_start - hole_start);
7109 em->start = hole_start;
7110 em->orig_start = hole_start;
7111 /*
7112 * don't adjust block start at all,
7113 * it is fixed at EXTENT_MAP_HOLE
7114 */
7115 em->block_start = hole_em->block_start;
7116 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007117 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7118 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007119 } else {
7120 em->start = range_start;
7121 em->len = found;
7122 em->orig_start = range_start;
7123 em->block_start = EXTENT_MAP_DELALLOC;
7124 em->block_len = found;
7125 }
7126 } else if (hole_em) {
7127 return hole_em;
7128 }
7129out:
7130
7131 free_extent_map(hole_em);
7132 if (err) {
7133 free_extent_map(em);
7134 return ERR_PTR(err);
7135 }
7136 return em;
7137}
7138
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007139static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7140 const u64 start,
7141 const u64 len,
7142 const u64 orig_start,
7143 const u64 block_start,
7144 const u64 block_len,
7145 const u64 orig_block_len,
7146 const u64 ram_bytes,
7147 const int type)
7148{
7149 struct extent_map *em = NULL;
7150 int ret;
7151
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007152 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007153 em = create_io_em(inode, start, len, orig_start,
7154 block_start, block_len, orig_block_len,
7155 ram_bytes,
7156 BTRFS_COMPRESS_NONE, /* compress_type */
7157 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007158 if (IS_ERR(em))
7159 goto out;
7160 }
7161 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7162 len, block_len, type);
7163 if (ret) {
7164 if (em) {
7165 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007166 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007167 start + len - 1, 0);
7168 }
7169 em = ERR_PTR(ret);
7170 }
7171 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007172
7173 return em;
7174}
7175
Josef Bacik4b46fce2010-05-23 11:00:55 -04007176static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7177 u64 start, u64 len)
7178{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007179 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007180 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007181 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007182 struct btrfs_key ins;
7183 u64 alloc_hint;
7184 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007185
Josef Bacik4b46fce2010-05-23 11:00:55 -04007186 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007187 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007188 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007189 if (ret)
7190 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007191
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007192 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7193 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007194 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007195 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007196 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007197 btrfs_free_reserved_extent(fs_info, ins.objectid,
7198 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007199
Josef Bacik4b46fce2010-05-23 11:00:55 -04007200 return em;
7201}
7202
Chris Mason46bfbb52010-05-26 11:04:10 -04007203/*
7204 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7205 * block must be cow'd
7206 */
Josef Bacik00361582013-08-14 14:02:47 -04007207noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007208 u64 *orig_start, u64 *orig_block_len,
7209 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007210{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007211 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007212 struct btrfs_path *path;
7213 int ret;
7214 struct extent_buffer *leaf;
7215 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007216 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007217 struct btrfs_file_extent_item *fi;
7218 struct btrfs_key key;
7219 u64 disk_bytenr;
7220 u64 backref_offset;
7221 u64 extent_end;
7222 u64 num_bytes;
7223 int slot;
7224 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007225 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007226
Chris Mason46bfbb52010-05-26 11:04:10 -04007227 path = btrfs_alloc_path();
7228 if (!path)
7229 return -ENOMEM;
7230
David Sterbaf85b7372017-01-20 14:54:07 +01007231 ret = btrfs_lookup_file_extent(NULL, root, path,
7232 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007233 if (ret < 0)
7234 goto out;
7235
7236 slot = path->slots[0];
7237 if (ret == 1) {
7238 if (slot == 0) {
7239 /* can't find the item, must cow */
7240 ret = 0;
7241 goto out;
7242 }
7243 slot--;
7244 }
7245 ret = 0;
7246 leaf = path->nodes[0];
7247 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007248 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007249 key.type != BTRFS_EXTENT_DATA_KEY) {
7250 /* not our file or wrong item type, must cow */
7251 goto out;
7252 }
7253
7254 if (key.offset > offset) {
7255 /* Wrong offset, must cow */
7256 goto out;
7257 }
7258
7259 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7260 found_type = btrfs_file_extent_type(leaf, fi);
7261 if (found_type != BTRFS_FILE_EXTENT_REG &&
7262 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7263 /* not a regular extent, must cow */
7264 goto out;
7265 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007266
7267 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7268 goto out;
7269
Miao Xiee77751a2013-12-27 21:11:50 +08007270 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7271 if (extent_end <= offset)
7272 goto out;
7273
Chris Mason46bfbb52010-05-26 11:04:10 -04007274 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007275 if (disk_bytenr == 0)
7276 goto out;
7277
7278 if (btrfs_file_extent_compression(leaf, fi) ||
7279 btrfs_file_extent_encryption(leaf, fi) ||
7280 btrfs_file_extent_other_encoding(leaf, fi))
7281 goto out;
7282
Chris Mason46bfbb52010-05-26 11:04:10 -04007283 backref_offset = btrfs_file_extent_offset(leaf, fi);
7284
Josef Bacik7ee9e442013-06-21 16:37:03 -04007285 if (orig_start) {
7286 *orig_start = key.offset - backref_offset;
7287 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7288 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7289 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007290
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007291 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007292 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007293
7294 num_bytes = min(offset + *len, extent_end) - offset;
7295 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7296 u64 range_end;
7297
Jeff Mahoneyda170662016-06-15 09:22:56 -04007298 range_end = round_up(offset + num_bytes,
7299 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007300 ret = test_range_bit(io_tree, offset, range_end,
7301 EXTENT_DELALLOC, 0, NULL);
7302 if (ret) {
7303 ret = -EAGAIN;
7304 goto out;
7305 }
7306 }
7307
Josef Bacik1bda19e2013-10-18 12:10:36 -04007308 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007309
7310 /*
7311 * look for other files referencing this extent, if we
7312 * find any we must cow
7313 */
Josef Bacik00361582013-08-14 14:02:47 -04007314
Liu Boe4c3b2d2017-01-30 12:25:28 -08007315 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007316 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007317 if (ret) {
7318 ret = 0;
7319 goto out;
7320 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007321
7322 /*
7323 * adjust disk_bytenr and num_bytes to cover just the bytes
7324 * in this extent we are about to write. If there
7325 * are any csums in that range we have to cow in order
7326 * to keep the csums correct
7327 */
7328 disk_bytenr += backref_offset;
7329 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007330 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7331 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007332 /*
7333 * all of the above have passed, it is safe to overwrite this extent
7334 * without cow
7335 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007336 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007337 ret = 1;
7338out:
7339 btrfs_free_path(path);
7340 return ret;
7341}
7342
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007343bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7344{
7345 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7346 int found = false;
7347 void **pagep = NULL;
7348 struct page *page = NULL;
7349 int start_idx;
7350 int end_idx;
7351
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007352 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007353
7354 /*
7355 * end is the last byte in the last page. end == start is legal
7356 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007357 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007358
7359 rcu_read_lock();
7360
7361 /* Most of the code in this while loop is lifted from
7362 * find_get_page. It's been modified to begin searching from a
7363 * page and return just the first page found in that range. If the
7364 * found idx is less than or equal to the end idx then we know that
7365 * a page exists. If no pages are found or if those pages are
7366 * outside of the range then we're fine (yay!) */
7367 while (page == NULL &&
7368 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7369 page = radix_tree_deref_slot(pagep);
7370 if (unlikely(!page))
7371 break;
7372
7373 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007374 if (radix_tree_deref_retry(page)) {
7375 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007376 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007377 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007378 /*
7379 * Otherwise, shmem/tmpfs must be storing a swap entry
7380 * here as an exceptional entry: so return it without
7381 * attempting to raise page count.
7382 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007383 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007384 break; /* TODO: Is this relevant for this use case? */
7385 }
7386
Filipe Manana91405152014-06-05 13:22:24 +01007387 if (!page_cache_get_speculative(page)) {
7388 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007389 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007390 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007391
7392 /*
7393 * Has the page moved?
7394 * This is part of the lockless pagecache protocol. See
7395 * include/linux/pagemap.h for details.
7396 */
7397 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007398 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007399 page = NULL;
7400 }
7401 }
7402
7403 if (page) {
7404 if (page->index <= end_idx)
7405 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007406 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007407 }
7408
7409 rcu_read_unlock();
7410 return found;
7411}
7412
Josef Bacikeb838e72012-07-31 16:28:48 -04007413static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7414 struct extent_state **cached_state, int writing)
7415{
7416 struct btrfs_ordered_extent *ordered;
7417 int ret = 0;
7418
7419 while (1) {
7420 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007421 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007422 /*
7423 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007424 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007425 * extents in this range.
7426 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007427 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007428 lockend - lockstart + 1);
7429
7430 /*
7431 * We need to make sure there are no buffered pages in this
7432 * range either, we could have raced between the invalidate in
7433 * generic_file_direct_write and locking the extent. The
7434 * invalidate needs to happen so that reads after a write do not
7435 * get stale data.
7436 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007437 if (!ordered &&
7438 (!writing ||
7439 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007440 break;
7441
7442 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7443 cached_state, GFP_NOFS);
7444
7445 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007446 /*
7447 * If we are doing a DIO read and the ordered extent we
7448 * found is for a buffered write, we can not wait for it
7449 * to complete and retry, because if we do so we can
7450 * deadlock with concurrent buffered writes on page
7451 * locks. This happens only if our DIO read covers more
7452 * than one extent map, if at this point has already
7453 * created an ordered extent for a previous extent map
7454 * and locked its range in the inode's io tree, and a
7455 * concurrent write against that previous extent map's
7456 * range and this range started (we unlock the ranges
7457 * in the io tree only when the bios complete and
7458 * buffered writes always lock pages before attempting
7459 * to lock range in the io tree).
7460 */
7461 if (writing ||
7462 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7463 btrfs_start_ordered_extent(inode, ordered, 1);
7464 else
7465 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007466 btrfs_put_ordered_extent(ordered);
7467 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007468 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007469 * We could trigger writeback for this range (and wait
7470 * for it to complete) and then invalidate the pages for
7471 * this range (through invalidate_inode_pages2_range()),
7472 * but that can lead us to a deadlock with a concurrent
7473 * call to readpages() (a buffered read or a defrag call
7474 * triggered a readahead) on a page lock due to an
7475 * ordered dio extent we created before but did not have
7476 * yet a corresponding bio submitted (whence it can not
7477 * complete), which makes readpages() wait for that
7478 * ordered extent to complete while holding a lock on
7479 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007480 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007481 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007482 }
7483
Filipe Mananaade77022016-02-18 14:28:55 +00007484 if (ret)
7485 break;
7486
Josef Bacikeb838e72012-07-31 16:28:48 -04007487 cond_resched();
7488 }
7489
7490 return ret;
7491}
7492
Liu Bo6f9994d2017-01-31 07:50:22 -08007493/* The callers of this must take lock_extent() */
7494static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7495 u64 orig_start, u64 block_start,
7496 u64 block_len, u64 orig_block_len,
7497 u64 ram_bytes, int compress_type,
7498 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007499{
7500 struct extent_map_tree *em_tree;
7501 struct extent_map *em;
7502 struct btrfs_root *root = BTRFS_I(inode)->root;
7503 int ret;
7504
Liu Bo6f9994d2017-01-31 07:50:22 -08007505 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7506 type == BTRFS_ORDERED_COMPRESSED ||
7507 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007508 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007509
Josef Bacik69ffb542012-09-11 15:40:07 -04007510 em_tree = &BTRFS_I(inode)->extent_tree;
7511 em = alloc_extent_map();
7512 if (!em)
7513 return ERR_PTR(-ENOMEM);
7514
7515 em->start = start;
7516 em->orig_start = orig_start;
7517 em->len = len;
7518 em->block_len = block_len;
7519 em->block_start = block_start;
7520 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007521 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007522 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007523 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007524 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007525 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007526 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007527 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007528 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7529 em->compress_type = compress_type;
7530 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007531
7532 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007533 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007534 em->start + em->len - 1, 0);
7535 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007536 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007537 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007538 /*
7539 * The caller has taken lock_extent(), who could race with us
7540 * to add em?
7541 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007542 } while (ret == -EEXIST);
7543
7544 if (ret) {
7545 free_extent_map(em);
7546 return ERR_PTR(ret);
7547 }
7548
Liu Bo6f9994d2017-01-31 07:50:22 -08007549 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007550 return em;
7551}
7552
Filipe Manana9c9464c2015-11-04 09:52:04 +00007553static void adjust_dio_outstanding_extents(struct inode *inode,
7554 struct btrfs_dio_data *dio_data,
7555 const u64 len)
7556{
David Sterba823bb202017-01-04 11:09:51 +01007557 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007558
Filipe Manana9c9464c2015-11-04 09:52:04 +00007559 /*
7560 * If we have an outstanding_extents count still set then we're
7561 * within our reservation, otherwise we need to adjust our inode
7562 * counter appropriately.
7563 */
Liu Boc2931662016-12-22 17:13:54 -08007564 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007565 dio_data->outstanding_extents -= num_extents;
7566 } else {
Liu Boc2931662016-12-22 17:13:54 -08007567 /*
7568 * If dio write length has been split due to no large enough
7569 * contiguous space, we need to compensate our inode counter
7570 * appropriately.
7571 */
7572 u64 num_needed = num_extents - dio_data->outstanding_extents;
7573
Filipe Manana9c9464c2015-11-04 09:52:04 +00007574 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007575 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007576 spin_unlock(&BTRFS_I(inode)->lock);
7577 }
7578}
7579
Josef Bacik4b46fce2010-05-23 11:00:55 -04007580static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7581 struct buffer_head *bh_result, int create)
7582{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007583 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007584 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007585 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307586 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007587 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007588 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007589 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007590 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007591 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007592
Miao Xie172a5042013-02-21 02:48:22 -07007593 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007594 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007595 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007596 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007597
Josef Bacikc3298612012-08-03 16:49:19 -04007598 lockstart = start;
7599 lockend = start + len - 1;
7600
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007601 if (current->journal_info) {
7602 /*
7603 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007604 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007605 * confused.
7606 */
chandan50745b02015-08-28 21:10:13 +05307607 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007608 current->journal_info = NULL;
7609 }
7610
Josef Bacikeb838e72012-07-31 16:28:48 -04007611 /*
7612 * If this errors out it's because we couldn't invalidate pagecache for
7613 * this range and we need to fallback to buffered.
7614 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007615 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7616 create)) {
7617 ret = -ENOTBLK;
7618 goto err;
7619 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007620
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007621 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007622 if (IS_ERR(em)) {
7623 ret = PTR_ERR(em);
7624 goto unlock_err;
7625 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007626
7627 /*
7628 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7629 * io. INLINE is special, and we could probably kludge it in here, but
7630 * it's still buffered so for safety lets just fall back to the generic
7631 * buffered path.
7632 *
7633 * For COMPRESSED we _have_ to read the entire extent in so we can
7634 * decompress it, so there will be buffering required no matter what we
7635 * do, so go ahead and fallback to buffered.
7636 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007637 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007638 * to buffered IO. Don't blame me, this is the price we pay for using
7639 * the generic code.
7640 */
7641 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7642 em->block_start == EXTENT_MAP_INLINE) {
7643 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007644 ret = -ENOTBLK;
7645 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007646 }
7647
7648 /* Just a good old fashioned hole, return */
7649 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7650 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7651 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007652 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007653 }
7654
7655 /*
7656 * We don't allocate a new extent in the following cases
7657 *
7658 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7659 * existing extent.
7660 * 2) The extent is marked as PREALLOC. We're good to go here and can
7661 * just use the extent.
7662 *
7663 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007664 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007665 len = min(len, em->len - (start - em->start));
7666 lockstart = start + len;
7667 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007668 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007669
7670 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7671 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7672 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007673 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007674 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007675
7676 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7677 type = BTRFS_ORDERED_PREALLOC;
7678 else
7679 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007680 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007682
Josef Bacik00361582013-08-14 14:02:47 -04007683 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007684 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007685 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007686 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007687
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007688 em2 = btrfs_create_dio_extent(inode, start, len,
7689 orig_start, block_start,
7690 len, orig_block_len,
7691 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007692 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007693 if (type == BTRFS_ORDERED_PREALLOC) {
7694 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007695 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007696 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007697 if (em2 && IS_ERR(em2)) {
7698 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007699 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007700 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007701 /*
7702 * For inode marked NODATACOW or extent marked PREALLOC,
7703 * use the existing or preallocated extent, so does not
7704 * need to adjust btrfs_space_info's bytes_may_use.
7705 */
7706 btrfs_free_reserved_data_space_noquota(inode,
7707 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007708 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007709 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007710 }
Josef Bacik00361582013-08-14 14:02:47 -04007711
Chris Mason46bfbb52010-05-26 11:04:10 -04007712 /*
7713 * this will cow the extent, reset the len in case we changed
7714 * it above
7715 */
7716 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007717 free_extent_map(em);
7718 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007719 if (IS_ERR(em)) {
7720 ret = PTR_ERR(em);
7721 goto unlock_err;
7722 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007723 len = min(len, em->len - (start - em->start));
7724unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007725 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7726 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007727 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007728 bh_result->b_bdev = em->bdev;
7729 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007730 if (create) {
7731 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7732 set_buffer_new(bh_result);
7733
7734 /*
7735 * Need to update the i_size under the extent lock so buffered
7736 * readers will get the updated i_size when we unlock.
7737 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007738 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007739 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007740
Filipe Manana9c9464c2015-11-04 09:52:04 +00007741 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307742 WARN_ON(dio_data->reserve < len);
7743 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007744 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307745 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007746 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007747
Josef Bacikeb838e72012-07-31 16:28:48 -04007748 /*
7749 * In the case of write we need to clear and unlock the entire range,
7750 * in the case of read we need to unlock only the end area that we
7751 * aren't using if there is any left over space.
7752 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007753 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007754 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7755 lockend, unlock_bits, 1, 0,
7756 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007757 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007758 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007759 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007760
Josef Bacik4b46fce2010-05-23 11:00:55 -04007761 free_extent_map(em);
7762
7763 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007764
7765unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007766 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7767 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007768err:
chandan50745b02015-08-28 21:10:13 +05307769 if (dio_data)
7770 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007771 /*
7772 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7773 * write less data then expected, so that we don't underflow our inode's
7774 * outstanding extents counter.
7775 */
7776 if (create && dio_data)
7777 adjust_dio_outstanding_extents(inode, dio_data, len);
7778
Josef Bacikeb838e72012-07-31 16:28:48 -04007779 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007780}
7781
Miao Xie8b110e32014-09-12 18:44:03 +08007782static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007783 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007784{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007785 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007786 int ret;
7787
Mike Christie37226b22016-06-05 14:31:52 -05007788 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007789
7790 bio_get(bio);
7791
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007792 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007793 if (ret)
7794 goto err;
7795
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007796 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007797err:
7798 bio_put(bio);
7799 return ret;
7800}
7801
7802static int btrfs_check_dio_repairable(struct inode *inode,
7803 struct bio *failed_bio,
7804 struct io_failure_record *failrec,
7805 int failed_mirror)
7806{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007807 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007808 int num_copies;
7809
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007810 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007811 if (num_copies == 1) {
7812 /*
7813 * we only have a single copy of the data, so don't bother with
7814 * all the retry and error correction code that follows. no
7815 * matter what the error is, it is very likely to persist.
7816 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007817 btrfs_debug(fs_info,
7818 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7819 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007820 return 0;
7821 }
7822
7823 failrec->failed_mirror = failed_mirror;
7824 failrec->this_mirror++;
7825 if (failrec->this_mirror == failed_mirror)
7826 failrec->this_mirror++;
7827
7828 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007829 btrfs_debug(fs_info,
7830 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7831 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007832 return 0;
7833 }
7834
7835 return 1;
7836}
7837
7838static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307839 struct page *page, unsigned int pgoff,
7840 u64 start, u64 end, int failed_mirror,
7841 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007842{
7843 struct io_failure_record *failrec;
7844 struct bio *bio;
7845 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007846 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007847 int ret;
7848
Mike Christie37226b22016-06-05 14:31:52 -05007849 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007850
7851 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7852 if (ret)
7853 return ret;
7854
7855 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7856 failed_mirror);
7857 if (!ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007858 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007859 return -EIO;
7860 }
7861
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307862 if ((failed_bio->bi_vcnt > 1)
7863 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007864 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007865 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007866
7867 isector = start - btrfs_io_bio(failed_bio)->logical;
7868 isector >>= inode->i_sb->s_blocksize_bits;
7869 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307870 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007871 if (!bio) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007872 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007873 return -EIO;
7874 }
Mike Christie37226b22016-06-05 14:31:52 -05007875 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007876
7877 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7878 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7879 read_mode, failrec->this_mirror, failrec->in_validation);
7880
Mike Christie81a75f672016-06-05 14:31:54 -05007881 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007882 if (ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007883 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007884 bio_put(bio);
7885 }
7886
7887 return ret;
7888}
7889
7890struct btrfs_retry_complete {
7891 struct completion done;
7892 struct inode *inode;
7893 u64 start;
7894 int uptodate;
7895};
7896
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007897static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007898{
7899 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307900 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007901 struct bio_vec *bvec;
7902 int i;
7903
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007904 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007905 goto end;
7906
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307907 ASSERT(bio->bi_vcnt == 1);
7908 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007909 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307910
Miao Xie8b110e32014-09-12 18:44:03 +08007911 done->uptodate = 1;
7912 bio_for_each_segment_all(bvec, bio, i)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02007913 clean_io_failure(BTRFS_I(done->inode), done->start, bvec->bv_page, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007914end:
7915 complete(&done->done);
7916 bio_put(bio);
7917}
7918
7919static int __btrfs_correct_data_nocsum(struct inode *inode,
7920 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007921{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307922 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007923 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007924 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007925 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307926 unsigned int pgoff;
7927 u32 sectorsize;
7928 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007929 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007930 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007931
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307932 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007933 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307934
Miao Xiec1dc0892014-09-12 18:43:56 +08007935 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007936 done.inode = inode;
7937
7938 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307939 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7940 pgoff = bvec->bv_offset;
7941
7942next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007943 done.uptodate = 0;
7944 done.start = start;
7945 init_completion(&done.done);
7946
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307947 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7948 pgoff, start, start + sectorsize - 1,
7949 io_bio->mirror_num,
7950 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007951 if (ret)
7952 return ret;
7953
7954 wait_for_completion(&done.done);
7955
7956 if (!done.uptodate) {
7957 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307958 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007959 }
7960
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307961 start += sectorsize;
7962
7963 if (nr_sectors--) {
7964 pgoff += sectorsize;
7965 goto next_block_or_try_again;
7966 }
Miao Xie8b110e32014-09-12 18:44:03 +08007967 }
7968
7969 return 0;
7970}
7971
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007972static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007973{
7974 struct btrfs_retry_complete *done = bio->bi_private;
7975 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307976 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007977 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307978 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08007979 int uptodate;
7980 int ret;
7981 int i;
7982
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007983 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007984 goto end;
7985
7986 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307987
7988 start = done->start;
7989
7990 ASSERT(bio->bi_vcnt == 1);
7991 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007992 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307993
Miao Xie8b110e32014-09-12 18:44:03 +08007994 bio_for_each_segment_all(bvec, bio, i) {
7995 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307996 bvec->bv_page, bvec->bv_offset,
7997 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007998 if (!ret)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02007999 clean_io_failure(BTRFS_I(done->inode), done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308000 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008001 else
8002 uptodate = 0;
8003 }
8004
8005 done->uptodate = uptodate;
8006end:
8007 complete(&done->done);
8008 bio_put(bio);
8009}
8010
8011static int __btrfs_subio_endio_read(struct inode *inode,
8012 struct btrfs_io_bio *io_bio, int err)
8013{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308014 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008015 struct bio_vec *bvec;
8016 struct btrfs_retry_complete done;
8017 u64 start;
8018 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308019 u32 sectorsize;
8020 int nr_sectors;
8021 unsigned int pgoff;
8022 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008023 int i;
8024 int ret;
8025
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308026 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008027 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308028
Miao Xie8b110e32014-09-12 18:44:03 +08008029 err = 0;
8030 start = io_bio->logical;
8031 done.inode = inode;
8032
Miao Xiec1dc0892014-09-12 18:43:56 +08008033 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308034 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8035
8036 pgoff = bvec->bv_offset;
8037next_block:
8038 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8039 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8040 bvec->bv_page, pgoff, start,
8041 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008042 if (likely(!ret))
8043 goto next;
8044try_again:
8045 done.uptodate = 0;
8046 done.start = start;
8047 init_completion(&done.done);
8048
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308049 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8050 pgoff, start, start + sectorsize - 1,
8051 io_bio->mirror_num,
8052 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008053 if (ret) {
8054 err = ret;
8055 goto next;
8056 }
8057
8058 wait_for_completion(&done.done);
8059
8060 if (!done.uptodate) {
8061 /* We might have another mirror, so try again */
8062 goto try_again;
8063 }
8064next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308065 offset += sectorsize;
8066 start += sectorsize;
8067
8068 ASSERT(nr_sectors);
8069
8070 if (--nr_sectors) {
8071 pgoff += sectorsize;
8072 goto next_block;
8073 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008074 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008075
8076 return err;
8077}
8078
Miao Xie8b110e32014-09-12 18:44:03 +08008079static int btrfs_subio_endio_read(struct inode *inode,
8080 struct btrfs_io_bio *io_bio, int err)
8081{
8082 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8083
8084 if (skip_csum) {
8085 if (unlikely(err))
8086 return __btrfs_correct_data_nocsum(inode, io_bio);
8087 else
8088 return 0;
8089 } else {
8090 return __btrfs_subio_endio_read(inode, io_bio, err);
8091 }
8092}
8093
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008094static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008095{
8096 struct btrfs_dio_private *dip = bio->bi_private;
8097 struct inode *inode = dip->inode;
8098 struct bio *dio_bio;
8099 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008100 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008101
Miao Xie8b110e32014-09-12 18:44:03 +08008102 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8103 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008104
Josef Bacik4b46fce2010-05-23 11:00:55 -04008105 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008106 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008107 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008108
Josef Bacik4b46fce2010-05-23 11:00:55 -04008109 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008110
Filipe Manana1636d1d2016-02-15 16:20:26 +00008111 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008112 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008113
8114 if (io_bio->end_io)
8115 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008116 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008117}
8118
Filipe Manana14543772015-11-24 16:23:54 +00008119static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8120 const u64 offset,
8121 const u64 bytes,
8122 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008123{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008124 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008125 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008126 u64 ordered_offset = offset;
8127 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008128 int ret;
8129
Chris Mason163cf092010-11-28 19:56:33 -05008130again:
8131 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8132 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008133 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008134 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008135 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008136 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008137
Liu Bo9e0af232014-08-15 23:36:53 +08008138 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8139 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008140 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008141out_test:
8142 /*
8143 * our bio might span multiple ordered extents. If we haven't
8144 * completed the accounting for the whole dio, go back and try again
8145 */
Filipe Manana14543772015-11-24 16:23:54 +00008146 if (ordered_offset < offset + bytes) {
8147 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008148 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008149 goto again;
8150 }
Filipe Manana14543772015-11-24 16:23:54 +00008151}
8152
8153static void btrfs_endio_direct_write(struct bio *bio)
8154{
8155 struct btrfs_dio_private *dip = bio->bi_private;
8156 struct bio *dio_bio = dip->dio_bio;
8157
8158 btrfs_endio_direct_write_update_ordered(dip->inode,
8159 dip->logical_offset,
8160 dip->bytes,
8161 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008162
Josef Bacik4b46fce2010-05-23 11:00:55 -04008163 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008164
Filipe Manana1636d1d2016-02-15 16:20:26 +00008165 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008166 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008167 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008168}
8169
Mike Christie81a75f672016-06-05 14:31:54 -05008170static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008171 struct bio *bio, int mirror_num,
8172 unsigned long bio_flags, u64 offset)
8173{
8174 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008175 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008176 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008177 return 0;
8178}
8179
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008180static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008181{
8182 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008183 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008184
Miao Xie8b110e32014-09-12 18:44:03 +08008185 if (err)
8186 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008187 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008188 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8189 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008190 (unsigned long long)bio->bi_iter.bi_sector,
8191 bio->bi_iter.bi_size, err);
8192
8193 if (dip->subio_endio)
8194 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008195
8196 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008197 dip->errors = 1;
8198
8199 /*
8200 * before atomic variable goto zero, we must make sure
8201 * dip->errors is perceived to be set.
8202 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008203 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008204 }
8205
8206 /* if there are more bios still pending for this dio, just exit */
8207 if (!atomic_dec_and_test(&dip->pending_bios))
8208 goto out;
8209
Chris Mason9be33952013-05-17 18:30:14 -04008210 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008211 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008212 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008213 dip->dio_bio->bi_error = 0;
8214 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008215 }
8216out:
8217 bio_put(bio);
8218}
8219
8220static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8221 u64 first_sector, gfp_t gfp_flags)
8222{
Chris Masonda2f0f72015-07-02 13:57:22 -07008223 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008224 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008225 if (bio)
8226 bio_associate_current(bio);
8227 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008228}
8229
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008230static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008231 struct btrfs_dio_private *dip,
8232 struct bio *bio,
8233 u64 file_offset)
8234{
8235 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8236 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8237 int ret;
8238
8239 /*
8240 * We load all the csum data we need when we submit
8241 * the first bio to reduce the csum tree search and
8242 * contention.
8243 */
8244 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008245 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008246 file_offset);
8247 if (ret)
8248 return ret;
8249 }
8250
8251 if (bio == dip->orig_bio)
8252 return 0;
8253
8254 file_offset -= dip->logical_offset;
8255 file_offset >>= inode->i_sb->s_blocksize_bits;
8256 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8257
8258 return 0;
8259}
8260
Miao Xiee65e1532010-11-22 03:04:43 +00008261static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008262 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008263 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008264{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008265 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008266 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008267 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008268 int ret;
8269
Josef Bacikb812ce22012-11-16 13:56:32 -05008270 if (async_submit)
8271 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8272
Miao Xiee65e1532010-11-22 03:04:43 +00008273 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008274
8275 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008276 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008277 if (ret)
8278 goto err;
8279 }
Miao Xiee65e1532010-11-22 03:04:43 +00008280
Josef Bacik1ae39932011-04-06 14:41:34 -04008281 if (skip_sum)
8282 goto map;
8283
8284 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008285 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8286 file_offset,
8287 __btrfs_submit_bio_start_direct_io,
8288 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008289 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008290 } else if (write) {
8291 /*
8292 * If we aren't doing async submit, calculate the csum of the
8293 * bio now.
8294 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008295 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008296 if (ret)
8297 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008298 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008299 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008300 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008301 if (ret)
8302 goto err;
8303 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008304map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008305 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008306err:
8307 bio_put(bio);
8308 return ret;
8309}
8310
Mike Christie81a75f672016-06-05 14:31:54 -05008311static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008312 int skip_sum)
8313{
8314 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008315 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008316 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008317 struct bio *bio;
8318 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008319 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008320 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008321 u64 file_offset = dip->logical_offset;
8322 u64 submit_len = 0;
8323 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008324 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008325 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308326 int nr_sectors;
8327 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008328 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008329
Kent Overstreet4f024f32013-10-11 15:44:27 -07008330 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008331 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8332 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008333 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008334 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008335
Kent Overstreet4f024f32013-10-11 15:44:27 -07008336 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008337 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008338 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008339 goto submit;
8340 }
8341
David Woodhouse53b381b2013-01-29 18:40:14 -05008342 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008343 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008344 async_submit = 0;
8345 else
8346 async_submit = 1;
8347
Josef Bacik02f57c72011-04-06 14:25:44 -04008348 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8349 if (!bio)
8350 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008351
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008352 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008353 bio->bi_private = dip;
8354 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008355 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008356 atomic_inc(&dip->pending_bios);
8357
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008358 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008359 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308360 i = 0;
8361next_block:
8362 if (unlikely(map_length < submit_len + blocksize ||
8363 bio_add_page(bio, bvec->bv_page, blocksize,
8364 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008365 /*
8366 * inc the count before we submit the bio so
8367 * we know the end IO handler won't happen before
8368 * we inc the count. Otherwise, the dip might get freed
8369 * before we're done setting it up
8370 */
8371 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008372 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008373 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008374 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008375 if (ret) {
8376 bio_put(bio);
8377 atomic_dec(&dip->pending_bios);
8378 goto out_err;
8379 }
8380
Miao Xiee65e1532010-11-22 03:04:43 +00008381 start_sector += submit_len >> 9;
8382 file_offset += submit_len;
8383
8384 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008385
8386 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8387 start_sector, GFP_NOFS);
8388 if (!bio)
8389 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008390 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008391 bio->bi_private = dip;
8392 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008393 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008394
Kent Overstreet4f024f32013-10-11 15:44:27 -07008395 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008396 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008397 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008398 &map_length, NULL, 0);
8399 if (ret) {
8400 bio_put(bio);
8401 goto out_err;
8402 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308403
8404 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008405 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308406 submit_len += blocksize;
8407 if (--nr_sectors) {
8408 i++;
8409 goto next_block;
8410 }
Miao Xiee65e1532010-11-22 03:04:43 +00008411 }
8412 }
8413
Josef Bacik02f57c72011-04-06 14:25:44 -04008414submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008415 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008416 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008417 if (!ret)
8418 return 0;
8419
8420 bio_put(bio);
8421out_err:
8422 dip->errors = 1;
8423 /*
8424 * before atomic variable goto zero, we must
8425 * make sure dip->errors is perceived to be set.
8426 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008427 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008428 if (atomic_dec_and_test(&dip->pending_bios))
8429 bio_io_error(dip->orig_bio);
8430
8431 /* bio_end_io() will handle error, so we needn't return it */
8432 return 0;
8433}
8434
Mike Christie8a4c1e42016-06-05 14:31:50 -05008435static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8436 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008437{
Filipe Manana61de7182015-07-01 12:13:10 +01008438 struct btrfs_dio_private *dip = NULL;
8439 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008440 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008441 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008442 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008443 int ret = 0;
8444
8445 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8446
Chris Mason9be33952013-05-17 18:30:14 -04008447 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008448 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008449 ret = -ENOMEM;
8450 goto free_ordered;
8451 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008452
Miao Xiec1dc0892014-09-12 18:43:56 +08008453 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008454 if (!dip) {
8455 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008456 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008457 }
8458
8459 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008460 dip->inode = inode;
8461 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008462 dip->bytes = dio_bio->bi_iter.bi_size;
8463 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008464 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008465 dip->orig_bio = io_bio;
8466 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008467 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008468 btrfs_bio = btrfs_io_bio(io_bio);
8469 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008470
Miao Xiec1dc0892014-09-12 18:43:56 +08008471 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008472 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008473 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008474 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008475 dip->subio_endio = btrfs_subio_endio_read;
8476 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008477
Filipe Mananaf28a4922015-12-08 19:23:20 +00008478 /*
8479 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8480 * even if we fail to submit a bio, because in such case we do the
8481 * corresponding error handling below and it must not be done a second
8482 * time by btrfs_direct_IO().
8483 */
8484 if (write) {
8485 struct btrfs_dio_data *dio_data = current->journal_info;
8486
8487 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8488 dip->bytes;
8489 dio_data->unsubmitted_oe_range_start =
8490 dio_data->unsubmitted_oe_range_end;
8491 }
8492
Mike Christie81a75f672016-06-05 14:31:54 -05008493 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008494 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008495 return;
Chris Mason9be33952013-05-17 18:30:14 -04008496
Miao Xie23ea8e52014-09-12 18:43:54 +08008497 if (btrfs_bio->end_io)
8498 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008499
Josef Bacik4b46fce2010-05-23 11:00:55 -04008500free_ordered:
8501 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008502 * If we arrived here it means either we failed to submit the dip
8503 * or we either failed to clone the dio_bio or failed to allocate the
8504 * dip. If we cloned the dio_bio and allocated the dip, we can just
8505 * call bio_endio against our io_bio so that we get proper resource
8506 * cleanup if we fail to submit the dip, otherwise, we must do the
8507 * same as btrfs_endio_direct_[write|read] because we can't call these
8508 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008509 */
Filipe Manana61de7182015-07-01 12:13:10 +01008510 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008511 io_bio->bi_error = -EIO;
8512 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008513 /*
8514 * The end io callbacks free our dip, do the final put on io_bio
8515 * and all the cleanup and final put for dio_bio (through
8516 * dio_end_io()).
8517 */
8518 dip = NULL;
8519 io_bio = NULL;
8520 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008521 if (write)
8522 btrfs_endio_direct_write_update_ordered(inode,
8523 file_offset,
8524 dio_bio->bi_iter.bi_size,
8525 0);
8526 else
Filipe Manana61de7182015-07-01 12:13:10 +01008527 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8528 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008529
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008530 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008531 /*
8532 * Releases and cleans up our dio_bio, no need to bio_put()
8533 * nor bio_endio()/bio_io_error() against dio_bio.
8534 */
8535 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008536 }
Filipe Manana61de7182015-07-01 12:13:10 +01008537 if (io_bio)
8538 bio_put(io_bio);
8539 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008540}
8541
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008542static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8543 struct kiocb *iocb,
8544 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008545{
8546 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008547 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008548 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008549 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008550
8551 if (offset & blocksize_mask)
8552 goto out;
8553
Al Viro28060d52014-03-22 05:15:17 -04008554 if (iov_iter_alignment(iter) & blocksize_mask)
8555 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008556
Al Viro28060d52014-03-22 05:15:17 -04008557 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008558 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008559 return 0;
8560 /*
8561 * Check to make sure we don't have duplicate iov_base's in this
8562 * iovec, if so return EINVAL, otherwise we'll get csum errors
8563 * when reading back.
8564 */
8565 for (seg = 0; seg < iter->nr_segs; seg++) {
8566 for (i = seg + 1; i < iter->nr_segs; i++) {
8567 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008568 goto out;
8569 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008570 }
8571 retval = 0;
8572out:
8573 return retval;
8574}
Josef Bacikeb838e72012-07-31 16:28:48 -04008575
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008576static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008577{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008578 struct file *file = iocb->ki_filp;
8579 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008580 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308581 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008582 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008583 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008584 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008585 bool wakeup = true;
8586 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008587 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008588
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008589 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008590 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008591
Jens Axboefe0f07d2015-04-15 17:05:48 -06008592 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008593 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008594
Josef Bacik0e267c42013-07-02 10:38:02 -04008595 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008596 * The generic stuff only does filemap_write_and_wait_range, which
8597 * isn't enough if we've written compressed pages to this area, so
8598 * we need to flush the dirty pages again to make absolutely sure
8599 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008600 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008601 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008602 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8603 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008604 filemap_fdatawrite_range(inode->i_mapping, offset,
8605 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008606
Omar Sandoval6f673762015-03-16 04:33:52 -07008607 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008608 /*
8609 * If the write DIO is beyond the EOF, we need update
8610 * the isize, but it is protected by i_mutex. So we can
8611 * not unlock the i_mutex at this case.
8612 */
8613 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008614 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008615 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008616 relock = true;
8617 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008618 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008619 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008620 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008621 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008622
8623 /*
8624 * We need to know how many extents we reserved so that we can
8625 * do the accounting properly if we go over the number we
8626 * originally calculated. Abuse current->journal_info for this.
8627 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008628 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008629 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008630 dio_data.unsubmitted_oe_range_start = (u64)offset;
8631 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308632 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308633 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008634 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8635 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008636 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008637 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8638 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008639 }
8640
Omar Sandoval17f8c842015-03-16 04:33:50 -07008641 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008642 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008643 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008644 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008645 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308646 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008647 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008648 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308649 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008650 btrfs_delalloc_release_space(inode, offset,
8651 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008652 /*
8653 * On error we might have left some ordered extents
8654 * without submitting corresponding bios for them, so
8655 * cleanup them up to avoid other tasks getting them
8656 * and waiting for them to complete forever.
8657 */
8658 if (dio_data.unsubmitted_oe_range_start <
8659 dio_data.unsubmitted_oe_range_end)
8660 btrfs_endio_direct_write_update_ordered(inode,
8661 dio_data.unsubmitted_oe_range_start,
8662 dio_data.unsubmitted_oe_range_end -
8663 dio_data.unsubmitted_oe_range_start,
8664 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008665 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008666 btrfs_delalloc_release_space(inode, offset,
8667 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008668 }
Miao Xie38851cc2013-02-08 07:04:11 +00008669out:
Miao Xie2e60a512013-02-08 07:01:08 +00008670 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008671 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008672 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008673 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008674
8675 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008676}
8677
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008678#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8679
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008680static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8681 __u64 start, __u64 len)
8682{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008683 int ret;
8684
8685 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8686 if (ret)
8687 return ret;
8688
Chris Masonec29ed52011-02-23 16:23:20 -05008689 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008690}
8691
Chris Mason9ebefb182007-06-15 13:50:00 -04008692int btrfs_readpage(struct file *file, struct page *page)
8693{
Chris Masond1310b22008-01-24 16:13:08 -05008694 struct extent_io_tree *tree;
8695 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008696 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008697}
Chris Mason1832a6d2007-12-21 16:27:21 -05008698
Chris Mason39279cc2007-06-12 06:35:45 -04008699static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8700{
Chris Masond1310b22008-01-24 16:13:08 -05008701 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008702 struct inode *inode = page->mapping->host;
8703 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008704
8705 if (current->flags & PF_MEMALLOC) {
8706 redirty_page_for_writepage(wbc, page);
8707 unlock_page(page);
8708 return 0;
8709 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008710
8711 /*
8712 * If we are under memory pressure we will call this directly from the
8713 * VM, we need to make sure we have the inode referenced for the ordered
8714 * extent. If not just return like we didn't do anything.
8715 */
8716 if (!igrab(inode)) {
8717 redirty_page_for_writepage(wbc, page);
8718 return AOP_WRITEPAGE_ACTIVATE;
8719 }
Chris Masond1310b22008-01-24 16:13:08 -05008720 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008721 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8722 btrfs_add_delayed_iput(inode);
8723 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008724}
Chris Mason39279cc2007-06-12 06:35:45 -04008725
Eric Sandeen48a3b632013-04-25 20:41:01 +00008726static int btrfs_writepages(struct address_space *mapping,
8727 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008728{
Chris Masond1310b22008-01-24 16:13:08 -05008729 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008730
Chris Masond1310b22008-01-24 16:13:08 -05008731 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008732 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8733}
8734
Chris Mason3ab2fb52007-11-08 10:59:22 -05008735static int
8736btrfs_readpages(struct file *file, struct address_space *mapping,
8737 struct list_head *pages, unsigned nr_pages)
8738{
Chris Masond1310b22008-01-24 16:13:08 -05008739 struct extent_io_tree *tree;
8740 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008741 return extent_readpages(tree, mapping, pages, nr_pages,
8742 btrfs_get_extent);
8743}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008744static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008745{
Chris Masond1310b22008-01-24 16:13:08 -05008746 struct extent_io_tree *tree;
8747 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008748 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008749
Chris Masond1310b22008-01-24 16:13:08 -05008750 tree = &BTRFS_I(page->mapping->host)->io_tree;
8751 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008752 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008753 if (ret == 1) {
8754 ClearPagePrivate(page);
8755 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008756 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008757 }
8758 return ret;
8759}
Chris Mason39279cc2007-06-12 06:35:45 -04008760
Chris Masone6dcd2d2008-07-17 12:53:50 -04008761static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8762{
Chris Mason98509cf2008-09-11 15:51:43 -04008763 if (PageWriteback(page) || PageDirty(page))
8764 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008765 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008766}
8767
Lukas Czernerd47992f2013-05-21 23:17:23 -04008768static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8769 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008770{
Josef Bacik5fd02042012-05-02 14:00:54 -04008771 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008772 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008773 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008774 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008775 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008776 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308777 u64 start;
8778 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008779 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008780
Chris Mason8b62b722009-09-02 16:53:46 -04008781 /*
8782 * we have the page locked, so new writeback can't start,
8783 * and the dirty bit won't be cleared while we are here.
8784 *
8785 * Wait for IO on this page so that we can safely clear
8786 * the PagePrivate2 bit and do ordered accounting
8787 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008788 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008789
Josef Bacik5fd02042012-05-02 14:00:54 -04008790 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008791 if (offset) {
8792 btrfs_releasepage(page, GFP_NOFS);
8793 return;
8794 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008795
8796 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008797 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308798again:
8799 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008800 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308801 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008802 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308803 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008804 /*
8805 * IO on this page will never be started, so we need
8806 * to account for any ordered extents now
8807 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008808 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308809 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008810 EXTENT_DIRTY | EXTENT_DELALLOC |
8811 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8812 EXTENT_DEFRAG, 1, 0, &cached_state,
8813 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008814 /*
8815 * whoever cleared the private bit is responsible
8816 * for the finish_ordered_io
8817 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008818 if (TestClearPagePrivate2(page)) {
8819 struct btrfs_ordered_inode_tree *tree;
8820 u64 new_len;
8821
8822 tree = &BTRFS_I(inode)->ordered_tree;
8823
8824 spin_lock_irq(&tree->lock);
8825 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308826 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008827 if (new_len < ordered->truncated_len)
8828 ordered->truncated_len = new_len;
8829 spin_unlock_irq(&tree->lock);
8830
8831 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308832 start,
8833 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008834 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008835 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008836 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008837 if (!inode_evicting) {
8838 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308839 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008840 &cached_state);
8841 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308842
8843 start = end + 1;
8844 if (start < page_end)
8845 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008846 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008847
Qu Wenruob9d0b382015-09-29 10:35:16 +08008848 /*
8849 * Qgroup reserved space handler
8850 * Page here will be either
8851 * 1) Already written to disk
8852 * In this case, its reserved space is released from data rsv map
8853 * and will be freed by delayed_ref handler finally.
8854 * So even we call qgroup_free_data(), it won't decrease reserved
8855 * space.
8856 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008857 * This means the reserved space should be freed here. However,
8858 * if a truncate invalidates the page (by clearing PageDirty)
8859 * and the page is accounted for while allocating extent
8860 * in btrfs_check_data_free_space() we let delayed_ref to
8861 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008862 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008863 if (PageDirty(page))
8864 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008865 if (!inode_evicting) {
8866 clear_extent_bit(tree, page_start, page_end,
8867 EXTENT_LOCKED | EXTENT_DIRTY |
8868 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8869 EXTENT_DEFRAG, 1, 1,
8870 &cached_state, GFP_NOFS);
8871
8872 __btrfs_releasepage(page, GFP_NOFS);
8873 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008874
Chris Mason4a096752008-07-21 10:29:44 -04008875 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008876 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008877 ClearPagePrivate(page);
8878 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008879 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008880 }
Chris Mason39279cc2007-06-12 06:35:45 -04008881}
8882
Chris Mason9ebefb182007-06-15 13:50:00 -04008883/*
8884 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8885 * called from a page fault handler when a page is first dirtied. Hence we must
8886 * be careful to check for EOF conditions here. We set the page up correctly
8887 * for a written page which means we get ENOSPC checking when writing into
8888 * holes and correct delalloc and unwritten extent mapping on filesystems that
8889 * support these features.
8890 *
8891 * We are not allowed to take the i_mutex here so we have to play games to
8892 * protect against truncate races as the page could now be beyond EOF. Because
8893 * vmtruncate() writes the inode size before removing pages, once we have the
8894 * page lock we can determine safely if the page is beyond EOF. If it is not
8895 * beyond EOF, then the page is guaranteed safe against truncation until we
8896 * unlock the page.
8897 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008898int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008899{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008900 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008901 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008902 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008903 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8904 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008905 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008906 char *kaddr;
8907 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008908 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008909 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008910 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308911 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008912 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008913 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308914 u64 end;
8915
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008916 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008917
Jan Karab2b5ef52012-06-12 16:20:45 +02008918 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008919 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008920 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308921 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008922
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308923 /*
8924 * Reserving delalloc space after obtaining the page lock can lead to
8925 * deadlock. For example, if a dirty page is locked by this function
8926 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8927 * dirty page write out, then the btrfs_writepage() function could
8928 * end up waiting indefinitely to get a lock on the page currently
8929 * being processed by btrfs_page_mkwrite() function.
8930 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008931 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308932 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008933 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008934 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008935 reserved = 1;
8936 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008937 if (ret) {
8938 if (ret == -ENOMEM)
8939 ret = VM_FAULT_OOM;
8940 else /* -ENOSPC, -EIO, etc */
8941 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008942 if (reserved)
8943 goto out;
8944 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008945 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008946
Nick Piggin56a76f82009-03-31 15:23:23 -07008947 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008948again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008949 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008950 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008951
Chris Mason9ebefb182007-06-15 13:50:00 -04008952 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008953 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008954 /* page got truncated out from underneath us */
8955 goto out_unlock;
8956 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008957 wait_on_page_writeback(page);
8958
David Sterbaff13db42015-12-03 14:30:40 +01008959 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008960 set_page_extent_mapped(page);
8961
Chris Masoneb84ae02008-07-17 13:53:27 -04008962 /*
8963 * we can't set the delalloc bits if there are pending ordered
8964 * extents. Drop our locks and wait for them to finish
8965 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008966 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8967 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008968 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008969 unlock_extent_cached(io_tree, page_start, page_end,
8970 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008971 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008972 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008973 btrfs_put_ordered_extent(ordered);
8974 goto again;
8975 }
8976
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008977 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008978 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008979 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008980 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308981 end = page_start + reserved_space - 1;
8982 spin_lock(&BTRFS_I(inode)->lock);
8983 BTRFS_I(inode)->outstanding_extents++;
8984 spin_unlock(&BTRFS_I(inode)->lock);
8985 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008986 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308987 }
8988 }
8989
Josef Bacikfbf19082009-10-01 17:10:23 -04008990 /*
Liu Bo54160342016-12-13 12:15:19 -08008991 * page_mkwrite gets called when the page is firstly dirtied after it's
8992 * faulted in, but write(2) could also dirty a page and set delalloc
8993 * bits, thus in this case for space account reason, we still need to
8994 * clear any delalloc bits within this page range since we have to
8995 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008996 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308997 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008998 EXTENT_DIRTY | EXTENT_DELALLOC |
8999 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009000 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009001
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309002 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009003 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009004 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009005 unlock_extent_cached(io_tree, page_start, page_end,
9006 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009007 ret = VM_FAULT_SIGBUS;
9008 goto out_unlock;
9009 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009010 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009011
9012 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009013 if (page_start + PAGE_SIZE > size)
9014 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009015 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009016 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009017
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009018 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009019 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009020 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009021 flush_dcache_page(page);
9022 kunmap(page);
9023 }
Chris Mason247e7432008-07-17 12:53:51 -04009024 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009025 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009026 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009027
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009028 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009029 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009030 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009031
Josef Bacik2ac55d42010-02-03 19:33:23 +00009032 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009033
9034out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009035 if (!ret) {
9036 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009037 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009038 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009039 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009040out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309041 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009042out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009043 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009044 return ret;
9045}
9046
Josef Bacika41ad392011-01-31 15:30:16 -05009047static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009048{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009049 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009050 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009051 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009052 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009053 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009054 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009055 u64 mask = fs_info->sectorsize - 1;
9056 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009057
Josef Bacik0ef8b722013-10-25 16:13:35 -04009058 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9059 (u64)-1);
9060 if (ret)
9061 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009062
Josef Bacikfcb80c22011-05-03 10:40:22 -04009063 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009064 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009065 * 3 things going on here
9066 *
9067 * 1) We need to reserve space for our orphan item and the space to
9068 * delete our orphan item. Lord knows we don't want to have a dangling
9069 * orphan item because we didn't reserve space to remove it.
9070 *
9071 * 2) We need to reserve space to update our inode.
9072 *
9073 * 3) We need to have something to cache all the space that is going to
9074 * be free'd up by the truncate operation, but also have some slack
9075 * space reserved in case it uses space during the truncate (thank you
9076 * very much snapshotting).
9077 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009078 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009079 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009080 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009081 * doesn't end up using space reserved for updating the inode or
9082 * removing the orphan item. We also need to be able to stop the
9083 * transaction and start a new one, which means we need to be able to
9084 * update the inode several times, and we have no idea of knowing how
9085 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009086 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009087 * Then there is the orphan item, which does indeed need to be held on
9088 * to for the whole operation, and we need nobody to touch this reserved
9089 * space except the orphan code.
9090 *
9091 * So that leaves us with
9092 *
9093 * 1) root->orphan_block_rsv - for the orphan deletion.
9094 * 2) rsv - for the truncate reservation, which we will steal from the
9095 * transaction reservation.
9096 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9097 * updating the inode.
9098 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009099 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009100 if (!rsv)
9101 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009102 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009103 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009104
Josef Bacik907cbce2011-08-08 13:46:15 -04009105 /*
Josef Bacik07127182011-08-19 10:29:59 -04009106 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009107 * 1 for updating the inode.
9108 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009109 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009110 if (IS_ERR(trans)) {
9111 err = PTR_ERR(trans);
9112 goto out;
9113 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009114
Josef Bacik907cbce2011-08-08 13:46:15 -04009115 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009116 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009117 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009118 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009119
Chris Mason5a3f23d2009-03-31 13:27:11 -04009120 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009121 * So if we truncate and then write and fsync we normally would just
9122 * write the extents that changed, which is a problem if we need to
9123 * first truncate that entire inode. So set this flag so we write out
9124 * all of the extents in the inode to the sync log so we're completely
9125 * safe.
9126 */
9127 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009128 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009129
Yan, Zheng80825102009-11-12 09:35:36 +00009130 while (1) {
9131 ret = btrfs_truncate_inode_items(trans, root, inode,
9132 inode->i_size,
9133 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009134 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009135 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009136 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009137 }
Chris Mason39279cc2007-06-12 06:35:45 -04009138
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009139 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009140 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009141 if (ret) {
9142 err = ret;
9143 break;
9144 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009145
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009146 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009147 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009148
9149 trans = btrfs_start_transaction(root, 2);
9150 if (IS_ERR(trans)) {
9151 ret = err = PTR_ERR(trans);
9152 trans = NULL;
9153 break;
9154 }
9155
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009156 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009157 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009158 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009159 BUG_ON(ret); /* shouldn't happen */
9160 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009161 }
9162
9163 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009164 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009165 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009166 if (ret)
9167 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009168 }
9169
Chris Mason917c16b2011-11-08 14:49:59 -05009170 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009171 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009172 ret = btrfs_update_inode(trans, root, inode);
9173 if (ret && !err)
9174 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009175
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009176 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009177 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009178 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009179out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009180 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009181
Josef Bacik3893e332011-01-31 16:03:11 -05009182 if (ret && !err)
9183 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009184
Josef Bacik3893e332011-01-31 16:03:11 -05009185 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009186}
9187
Sven Wegener3b963622008-06-09 21:57:42 -04009188/*
Chris Masond352ac62008-09-29 15:18:18 -04009189 * create a new subvolume directory/inode (helper for the ioctl).
9190 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009191int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009192 struct btrfs_root *new_root,
9193 struct btrfs_root *parent_root,
9194 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009195{
Chris Mason39279cc2007-06-12 06:35:45 -04009196 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009197 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009198 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009199
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009200 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9201 new_dirid, new_dirid,
9202 S_IFDIR | (~current_umask() & S_IRWXUGO),
9203 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009204 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009205 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009206 inode->i_op = &btrfs_dir_inode_operations;
9207 inode->i_fop = &btrfs_dir_file_operations;
9208
Miklos Szeredibfe86842011-10-28 14:13:29 +02009209 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009210 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009211 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009212
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009213 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9214 if (err)
9215 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009216 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009217 new_root->root_key.objectid, err);
9218
Yan, Zheng76dda932009-09-21 16:00:26 -04009219 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009220
Yan, Zheng76dda932009-09-21 16:00:26 -04009221 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009222 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009223}
9224
Chris Mason39279cc2007-06-12 06:35:45 -04009225struct inode *btrfs_alloc_inode(struct super_block *sb)
9226{
9227 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009228 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009229
9230 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9231 if (!ei)
9232 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009233
9234 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009235 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009236 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009237 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009238 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009239 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009240 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009241 ei->disk_i_size = 0;
9242 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009243 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009244 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009245 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009246 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009247 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009248 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009249
Josef Bacik9e0baf62011-07-15 15:16:44 +00009250 spin_lock_init(&ei->lock);
9251 ei->outstanding_extents = 0;
9252 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009253
Josef Bacik72ac3c02012-05-23 14:13:11 -04009254 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009255 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009256
Miao Xie16cdcec2011-04-22 18:12:22 +08009257 ei->delayed_node = NULL;
9258
chandan r9cc97d62012-07-04 12:48:07 +05309259 ei->i_otime.tv_sec = 0;
9260 ei->i_otime.tv_nsec = 0;
9261
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009262 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009263 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009264 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9265 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009266 ei->io_tree.track_uptodate = 1;
9267 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009268 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009269 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009270 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009271 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009272 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009273 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009274 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009275 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009276
9277 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009278}
9279
Josef Bacikaaedb552013-10-11 14:44:09 -04009280#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9281void btrfs_test_destroy_inode(struct inode *inode)
9282{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009283 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009284 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9285}
9286#endif
9287
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009288static void btrfs_i_callback(struct rcu_head *head)
9289{
9290 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009291 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9292}
9293
Chris Mason39279cc2007-06-12 06:35:45 -04009294void btrfs_destroy_inode(struct inode *inode)
9295{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009296 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009297 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009298 struct btrfs_root *root = BTRFS_I(inode)->root;
9299
Al Virob3d9b7a2012-06-09 13:51:19 -04009300 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009301 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009302 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9303 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009304 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9305 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009306 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009307
Chris Mason5a3f23d2009-03-31 13:27:11 -04009308 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009309 * This can happen where we create an inode, but somebody else also
9310 * created the same inode and we need to destroy the one we already
9311 * created.
9312 */
9313 if (!root)
9314 goto free;
9315
Josef Bacik8a35d952012-05-23 14:26:42 -04009316 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9317 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009318 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009319 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009320 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009321 }
Josef Bacik7b128762008-07-24 12:17:14 -04009322
Chris Masond3977122009-01-05 21:25:51 -05009323 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009324 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9325 if (!ordered)
9326 break;
9327 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009328 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009329 "found ordered extent %llu %llu on inode cleanup",
9330 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009331 btrfs_remove_ordered_extent(inode, ordered);
9332 btrfs_put_ordered_extent(ordered);
9333 btrfs_put_ordered_extent(ordered);
9334 }
9335 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009336 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009337 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009338 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009339free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009340 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009341}
9342
Al Viro45321ac2010-06-07 13:43:19 -04009343int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009344{
9345 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009346
Naohiro Aota6379ef92013-06-06 09:56:34 +00009347 if (root == NULL)
9348 return 1;
9349
Liu Bofa6ac872013-02-20 14:10:23 +00009350 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009351 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009352 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009353 else
Al Viro45321ac2010-06-07 13:43:19 -04009354 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009355}
9356
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009357static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009358{
9359 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9360
9361 inode_init_once(&ei->vfs_inode);
9362}
9363
9364void btrfs_destroy_cachep(void)
9365{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009366 /*
9367 * Make sure all delayed rcu free inodes are flushed before we
9368 * destroy cache.
9369 */
9370 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009371 kmem_cache_destroy(btrfs_inode_cachep);
9372 kmem_cache_destroy(btrfs_trans_handle_cachep);
9373 kmem_cache_destroy(btrfs_transaction_cachep);
9374 kmem_cache_destroy(btrfs_path_cachep);
9375 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009376}
9377
9378int btrfs_init_cachep(void)
9379{
David Sterba837e1972012-09-07 03:00:48 -06009380 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009381 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009382 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9383 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009384 if (!btrfs_inode_cachep)
9385 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009386
David Sterba837e1972012-09-07 03:00:48 -06009387 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009388 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009389 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009390 if (!btrfs_trans_handle_cachep)
9391 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009392
David Sterba837e1972012-09-07 03:00:48 -06009393 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009394 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009395 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009396 if (!btrfs_transaction_cachep)
9397 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009398
David Sterba837e1972012-09-07 03:00:48 -06009399 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009400 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009401 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009402 if (!btrfs_path_cachep)
9403 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009404
David Sterba837e1972012-09-07 03:00:48 -06009405 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009406 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009407 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009408 if (!btrfs_free_space_cachep)
9409 goto fail;
9410
Chris Mason39279cc2007-06-12 06:35:45 -04009411 return 0;
9412fail:
9413 btrfs_destroy_cachep();
9414 return -ENOMEM;
9415}
9416
9417static int btrfs_getattr(struct vfsmount *mnt,
9418 struct dentry *dentry, struct kstat *stat)
9419{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009420 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009421 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009422 u32 blocksize = inode->i_sb->s_blocksize;
9423
Chris Mason39279cc2007-06-12 06:35:45 -04009424 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009425 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009426
9427 spin_lock(&BTRFS_I(inode)->lock);
9428 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9429 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009430 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009431 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009432 return 0;
9433}
9434
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009435static int btrfs_rename_exchange(struct inode *old_dir,
9436 struct dentry *old_dentry,
9437 struct inode *new_dir,
9438 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009439{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009440 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009441 struct btrfs_trans_handle *trans;
9442 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009443 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009444 struct inode *new_inode = new_dentry->d_inode;
9445 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009446 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009447 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009448 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9449 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009450 u64 old_idx = 0;
9451 u64 new_idx = 0;
9452 u64 root_objectid;
9453 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009454 bool root_log_pinned = false;
9455 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009456
9457 /* we only allow rename subvolume link between subvolumes */
9458 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9459 return -EXDEV;
9460
9461 /* close the race window with snapshot create/destroy ioctl */
9462 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009463 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009464 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009465 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009466
9467 /*
9468 * We want to reserve the absolute worst case amount of items. So if
9469 * both inodes are subvols and we need to unlink them then that would
9470 * require 4 item modifications, but if they are both normal inodes it
9471 * would require 5 item modifications, so we'll assume their normal
9472 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9473 * should cover the worst case number of items we'll modify.
9474 */
9475 trans = btrfs_start_transaction(root, 12);
9476 if (IS_ERR(trans)) {
9477 ret = PTR_ERR(trans);
9478 goto out_notrans;
9479 }
9480
9481 /*
9482 * We need to find a free sequence number both in the source and
9483 * in the destination directory for the exchange.
9484 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009485 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009486 if (ret)
9487 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009488 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009489 if (ret)
9490 goto out_fail;
9491
9492 BTRFS_I(old_inode)->dir_index = 0ULL;
9493 BTRFS_I(new_inode)->dir_index = 0ULL;
9494
9495 /* Reference for the source. */
9496 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9497 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009498 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009499 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009500 btrfs_pin_log_trans(root);
9501 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009502 ret = btrfs_insert_inode_ref(trans, dest,
9503 new_dentry->d_name.name,
9504 new_dentry->d_name.len,
9505 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009506 btrfs_ino(BTRFS_I(new_dir)),
9507 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508 if (ret)
9509 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009510 }
9511
9512 /* And now for the dest. */
9513 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9514 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009515 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009516 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009517 btrfs_pin_log_trans(dest);
9518 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009519 ret = btrfs_insert_inode_ref(trans, root,
9520 old_dentry->d_name.name,
9521 old_dentry->d_name.len,
9522 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009523 btrfs_ino(BTRFS_I(old_dir)),
9524 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009525 if (ret)
9526 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009527 }
9528
9529 /* Update inode version and ctime/mtime. */
9530 inode_inc_iversion(old_dir);
9531 inode_inc_iversion(new_dir);
9532 inode_inc_iversion(old_inode);
9533 inode_inc_iversion(new_inode);
9534 old_dir->i_ctime = old_dir->i_mtime = ctime;
9535 new_dir->i_ctime = new_dir->i_mtime = ctime;
9536 old_inode->i_ctime = ctime;
9537 new_inode->i_ctime = ctime;
9538
9539 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009540 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9541 BTRFS_I(old_inode), 1);
9542 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9543 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009544 }
9545
9546 /* src is a subvolume */
9547 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9548 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9549 ret = btrfs_unlink_subvol(trans, root, old_dir,
9550 root_objectid,
9551 old_dentry->d_name.name,
9552 old_dentry->d_name.len);
9553 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009554 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9555 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009556 old_dentry->d_name.name,
9557 old_dentry->d_name.len);
9558 if (!ret)
9559 ret = btrfs_update_inode(trans, root, old_inode);
9560 }
9561 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009562 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009563 goto out_fail;
9564 }
9565
9566 /* dest is a subvolume */
9567 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9568 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9569 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9570 root_objectid,
9571 new_dentry->d_name.name,
9572 new_dentry->d_name.len);
9573 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009574 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9575 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009576 new_dentry->d_name.name,
9577 new_dentry->d_name.len);
9578 if (!ret)
9579 ret = btrfs_update_inode(trans, dest, new_inode);
9580 }
9581 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009582 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009583 goto out_fail;
9584 }
9585
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009586 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009587 new_dentry->d_name.name,
9588 new_dentry->d_name.len, 0, old_idx);
9589 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009590 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009591 goto out_fail;
9592 }
9593
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009594 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009595 old_dentry->d_name.name,
9596 old_dentry->d_name.len, 0, new_idx);
9597 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009598 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009599 goto out_fail;
9600 }
9601
9602 if (old_inode->i_nlink == 1)
9603 BTRFS_I(old_inode)->dir_index = old_idx;
9604 if (new_inode->i_nlink == 1)
9605 BTRFS_I(new_inode)->dir_index = new_idx;
9606
Filipe Manana86e8aa02016-05-05 02:02:27 +01009607 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009608 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009609 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9610 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009611 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009612 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009614 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009615 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009616 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9617 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009619 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 }
9621out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009622 /*
9623 * If we have pinned a log and an error happened, we unpin tasks
9624 * trying to sync the log and force them to fallback to a transaction
9625 * commit if the log currently contains any of the inodes involved in
9626 * this rename operation (to ensure we do not persist a log with an
9627 * inconsistent state for any of these inodes or leading to any
9628 * inconsistencies when replayed). If the transaction was aborted, the
9629 * abortion reason is propagated to userspace when attempting to commit
9630 * the transaction. If the log does not contain any of these inodes, we
9631 * allow the tasks to sync it.
9632 */
9633 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009634 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9635 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9636 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009637 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009638 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009639 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009640
9641 if (root_log_pinned) {
9642 btrfs_end_log_trans(root);
9643 root_log_pinned = false;
9644 }
9645 if (dest_log_pinned) {
9646 btrfs_end_log_trans(dest);
9647 dest_log_pinned = false;
9648 }
9649 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009650 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009651out_notrans:
9652 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009653 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009655 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656
9657 return ret;
9658}
9659
9660static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9661 struct btrfs_root *root,
9662 struct inode *dir,
9663 struct dentry *dentry)
9664{
9665 int ret;
9666 struct inode *inode;
9667 u64 objectid;
9668 u64 index;
9669
9670 ret = btrfs_find_free_ino(root, &objectid);
9671 if (ret)
9672 return ret;
9673
9674 inode = btrfs_new_inode(trans, root, dir,
9675 dentry->d_name.name,
9676 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009677 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 objectid,
9679 S_IFCHR | WHITEOUT_MODE,
9680 &index);
9681
9682 if (IS_ERR(inode)) {
9683 ret = PTR_ERR(inode);
9684 return ret;
9685 }
9686
9687 inode->i_op = &btrfs_special_inode_operations;
9688 init_special_inode(inode, inode->i_mode,
9689 WHITEOUT_DEV);
9690
9691 ret = btrfs_init_inode_security(trans, inode, dir,
9692 &dentry->d_name);
9693 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009694 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009695
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009696 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9697 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009699 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700
9701 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009702out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009703 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009704 if (ret)
9705 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009706 iput(inode);
9707
Filipe Mananac9901612016-05-05 01:41:57 +01009708 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709}
9710
Chris Mason39279cc2007-06-12 06:35:45 -04009711static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712 struct inode *new_dir, struct dentry *new_dentry,
9713 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009714{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009715 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009716 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009717 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009718 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9719 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009720 struct inode *new_inode = d_inode(new_dentry);
9721 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009722 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009723 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009724 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009725 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009726 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009727
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009728 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009729 return -EPERM;
9730
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009731 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009732 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009733 return -EXDEV;
9734
Li Zefan33345d012011-04-20 10:31:50 +08009735 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009736 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009737 return -ENOTEMPTY;
9738
Chris Mason39279cc2007-06-12 06:35:45 -04009739 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009740 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009741 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009742
9743
9744 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009745 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009746 new_dentry->d_name.name,
9747 new_dentry->d_name.len);
9748
9749 if (ret) {
9750 if (ret == -EEXIST) {
9751 /* we shouldn't get
9752 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309753 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009754 return ret;
9755 }
9756 } else {
9757 /* maybe -EOVERFLOW */
9758 return ret;
9759 }
9760 }
9761 ret = 0;
9762
Chris Mason5a3f23d2009-03-31 13:27:11 -04009763 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009764 * we're using rename to replace one file with another. Start IO on it
9765 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009766 */
Chris Mason8d875f92014-08-12 10:47:42 -07009767 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009768 filemap_flush(old_inode->i_mapping);
9769
Yan, Zheng76dda932009-09-21 16:00:26 -04009770 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009771 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009772 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009773 /*
9774 * We want to reserve the absolute worst case amount of items. So if
9775 * both inodes are subvols and we need to unlink them then that would
9776 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009777 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009778 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9779 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009780 * If our rename has the whiteout flag, we need more 5 units for the
9781 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9782 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009783 */
Filipe Manana5062af32016-05-05 10:26:26 +01009784 trans_num_items = 11;
9785 if (flags & RENAME_WHITEOUT)
9786 trans_num_items += 5;
9787 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009788 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009789 ret = PTR_ERR(trans);
9790 goto out_notrans;
9791 }
Chris Mason5f39d392007-10-15 16:14:19 -04009792
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009793 if (dest != root)
9794 btrfs_record_root_in_trans(trans, dest);
9795
Nikolay Borisov877574e2017-02-20 13:50:33 +02009796 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009797 if (ret)
9798 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009799
Miao Xie67de1172013-12-26 13:07:06 +08009800 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009801 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009802 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009803 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009804 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009805 btrfs_pin_log_trans(root);
9806 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009807 ret = btrfs_insert_inode_ref(trans, dest,
9808 new_dentry->d_name.name,
9809 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009810 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009811 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009812 if (ret)
9813 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009814 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009815
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009816 inode_inc_iversion(old_dir);
9817 inode_inc_iversion(new_dir);
9818 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009819 old_dir->i_ctime = old_dir->i_mtime =
9820 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009821 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009822
Chris Mason12fcfd22009-03-24 10:24:20 -04009823 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009824 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9825 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009826
Li Zefan33345d012011-04-20 10:31:50 +08009827 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009828 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9829 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9830 old_dentry->d_name.name,
9831 old_dentry->d_name.len);
9832 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009833 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9834 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009835 old_dentry->d_name.name,
9836 old_dentry->d_name.len);
9837 if (!ret)
9838 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009839 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009840 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009841 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009842 goto out_fail;
9843 }
Chris Mason39279cc2007-06-12 06:35:45 -04009844
9845 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009846 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009847 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009848 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009849 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9850 root_objectid = BTRFS_I(new_inode)->location.objectid;
9851 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9852 root_objectid,
9853 new_dentry->d_name.name,
9854 new_dentry->d_name.len);
9855 BUG_ON(new_inode->i_nlink == 0);
9856 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009857 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9858 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009859 new_dentry->d_name.name,
9860 new_dentry->d_name.len);
9861 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009862 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009863 ret = btrfs_orphan_add(trans,
9864 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009865 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009866 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009867 goto out_fail;
9868 }
Chris Mason39279cc2007-06-12 06:35:45 -04009869 }
Josef Bacikaec74772008-07-24 12:12:38 -04009870
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009871 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009872 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009873 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009874 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009875 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009876 goto out_fail;
9877 }
Chris Mason39279cc2007-06-12 06:35:45 -04009878
Miao Xie67de1172013-12-26 13:07:06 +08009879 if (old_inode->i_nlink == 1)
9880 BTRFS_I(old_inode)->dir_index = index;
9881
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009882 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009883 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009884
David Sterbaf85b7372017-01-20 14:54:07 +01009885 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9886 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009887 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009888 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009889 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009890
9891 if (flags & RENAME_WHITEOUT) {
9892 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9893 old_dentry);
9894
9895 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009896 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009897 goto out_fail;
9898 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009899 }
Chris Mason39279cc2007-06-12 06:35:45 -04009900out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009901 /*
9902 * If we have pinned the log and an error happened, we unpin tasks
9903 * trying to sync the log and force them to fallback to a transaction
9904 * commit if the log currently contains any of the inodes involved in
9905 * this rename operation (to ensure we do not persist a log with an
9906 * inconsistent state for any of these inodes or leading to any
9907 * inconsistencies when replayed). If the transaction was aborted, the
9908 * abortion reason is propagated to userspace when attempting to commit
9909 * the transaction. If the log does not contain any of these inodes, we
9910 * allow the tasks to sync it.
9911 */
9912 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009913 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9914 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9915 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009916 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009917 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009918 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009919
9920 btrfs_end_log_trans(root);
9921 log_pinned = false;
9922 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009923 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009924out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009925 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009926 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009927
Chris Mason39279cc2007-06-12 06:35:45 -04009928 return ret;
9929}
9930
Miklos Szeredi80ace852014-07-23 15:15:32 +02009931static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9932 struct inode *new_dir, struct dentry *new_dentry,
9933 unsigned int flags)
9934{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009935 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009936 return -EINVAL;
9937
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009938 if (flags & RENAME_EXCHANGE)
9939 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9940 new_dentry);
9941
9942 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009943}
9944
Miao Xie8ccf6f192012-10-25 09:28:04 +00009945static void btrfs_run_delalloc_work(struct btrfs_work *work)
9946{
9947 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009948 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009949
9950 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9951 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009952 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009953 filemap_flush(inode->i_mapping);
9954 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9955 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009956 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009957
9958 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009959 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009960 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009961 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009962 complete(&delalloc_work->completion);
9963}
9964
9965struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009966 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009967{
9968 struct btrfs_delalloc_work *work;
9969
David Sterba100d5702015-12-08 14:39:32 +01009970 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009971 if (!work)
9972 return NULL;
9973
9974 init_completion(&work->completion);
9975 INIT_LIST_HEAD(&work->list);
9976 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009977 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009978 WARN_ON_ONCE(!inode);
9979 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9980 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009981
9982 return work;
9983}
9984
9985void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9986{
9987 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009988 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009989}
9990
Chris Masond352ac62008-09-29 15:18:18 -04009991/*
9992 * some fairly slow code that needs optimization. This walks the list
9993 * of all the inodes with pending delalloc and forces them to disk.
9994 */
Miao Xie6c255e62014-03-06 13:55:01 +08009995static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9996 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009997{
Chris Masonea8c2812008-08-04 23:17:27 -04009998 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009999 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010000 struct btrfs_delalloc_work *work, *next;
10001 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010002 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010003 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010004
Miao Xie8ccf6f192012-10-25 09:28:04 +000010005 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010006 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010007
Miao Xie573bfb72014-03-06 13:55:03 +080010008 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010009 spin_lock(&root->delalloc_lock);
10010 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010011 while (!list_empty(&splice)) {
10012 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010013 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010014
Miao Xieeb73c1b2013-05-15 07:48:22 +000010015 list_move_tail(&binode->delalloc_inodes,
10016 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010017 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010018 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010019 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010020 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010021 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010022 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010023
David Sterba651d4942015-11-27 19:24:16 +010010024 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010025 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010026 if (delay_iput)
10027 btrfs_add_delayed_iput(inode);
10028 else
10029 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010030 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010031 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010032 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010033 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010034 btrfs_queue_work(root->fs_info->flush_workers,
10035 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010036 ret++;
10037 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010038 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010039 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010040 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010041 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010042 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010043
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010044out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010045 list_for_each_entry_safe(work, next, &works, list) {
10046 list_del_init(&work->list);
10047 btrfs_wait_and_free_delalloc_work(work);
10048 }
10049
Miao Xieeb73c1b2013-05-15 07:48:22 +000010050 if (!list_empty_careful(&splice)) {
10051 spin_lock(&root->delalloc_lock);
10052 list_splice_tail(&splice, &root->delalloc_inodes);
10053 spin_unlock(&root->delalloc_lock);
10054 }
Miao Xie573bfb72014-03-06 13:55:03 +080010055 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010056 return ret;
10057}
10058
10059int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10060{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010061 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010062 int ret;
10063
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010064 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010065 return -EROFS;
10066
Miao Xie6c255e62014-03-06 13:55:01 +080010067 ret = __start_delalloc_inodes(root, delay_iput, -1);
10068 if (ret > 0)
10069 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010070 /*
10071 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010072 * we have to make sure the IO is actually started and that
10073 * ordered extents get created before we return
10074 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010075 atomic_inc(&fs_info->async_submit_draining);
10076 while (atomic_read(&fs_info->nr_async_submits) ||
10077 atomic_read(&fs_info->async_delalloc_pages)) {
10078 wait_event(fs_info->async_submit_wait,
10079 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10080 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010081 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010082 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010083 return ret;
10084}
10085
Miao Xie6c255e62014-03-06 13:55:01 +080010086int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10087 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010088{
10089 struct btrfs_root *root;
10090 struct list_head splice;
10091 int ret;
10092
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010093 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010094 return -EROFS;
10095
10096 INIT_LIST_HEAD(&splice);
10097
Miao Xie573bfb72014-03-06 13:55:03 +080010098 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010099 spin_lock(&fs_info->delalloc_root_lock);
10100 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010101 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010102 root = list_first_entry(&splice, struct btrfs_root,
10103 delalloc_root);
10104 root = btrfs_grab_fs_root(root);
10105 BUG_ON(!root);
10106 list_move_tail(&root->delalloc_root,
10107 &fs_info->delalloc_roots);
10108 spin_unlock(&fs_info->delalloc_root_lock);
10109
Miao Xie6c255e62014-03-06 13:55:01 +080010110 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010111 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010112 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010113 goto out;
10114
Miao Xie6c255e62014-03-06 13:55:01 +080010115 if (nr != -1) {
10116 nr -= ret;
10117 WARN_ON(nr < 0);
10118 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010119 spin_lock(&fs_info->delalloc_root_lock);
10120 }
10121 spin_unlock(&fs_info->delalloc_root_lock);
10122
Miao Xie6c255e62014-03-06 13:55:01 +080010123 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010124 atomic_inc(&fs_info->async_submit_draining);
10125 while (atomic_read(&fs_info->nr_async_submits) ||
10126 atomic_read(&fs_info->async_delalloc_pages)) {
10127 wait_event(fs_info->async_submit_wait,
10128 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10129 atomic_read(&fs_info->async_delalloc_pages) == 0));
10130 }
10131 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010132out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010133 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010134 spin_lock(&fs_info->delalloc_root_lock);
10135 list_splice_tail(&splice, &fs_info->delalloc_roots);
10136 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010137 }
Miao Xie573bfb72014-03-06 13:55:03 +080010138 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010139 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010140}
10141
Chris Mason39279cc2007-06-12 06:35:45 -040010142static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10143 const char *symname)
10144{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010145 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010146 struct btrfs_trans_handle *trans;
10147 struct btrfs_root *root = BTRFS_I(dir)->root;
10148 struct btrfs_path *path;
10149 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010150 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010151 int err;
10152 int drop_inode = 0;
10153 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010154 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010155 int name_len;
10156 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010157 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010158 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010159 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010160
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010161 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010162 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010163 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010164
Josef Bacik9ed74f22009-09-11 16:12:44 -040010165 /*
10166 * 2 items for inode item and ref
10167 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010168 * 1 item for updating parent inode item
10169 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010170 * 1 item for xattr if selinux is on
10171 */
Filipe Manana9269d122015-12-31 18:16:29 +000010172 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010173 if (IS_ERR(trans))
10174 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010175
Li Zefan581bb052011-04-20 10:06:11 +080010176 err = btrfs_find_free_ino(root, &objectid);
10177 if (err)
10178 goto out_unlock;
10179
Josef Bacikaec74772008-07-24 12:12:38 -040010180 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010181 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10182 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010183 if (IS_ERR(inode)) {
10184 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010185 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010186 }
Chris Mason39279cc2007-06-12 06:35:45 -040010187
Casey Schauflerad19db72011-12-15 10:09:07 -050010188 /*
10189 * If the active LSM wants to access the inode during
10190 * d_instantiate it needs these. Smack checks to see
10191 * if the filesystem supports xattrs by looking at the
10192 * ops vector.
10193 */
10194 inode->i_fop = &btrfs_file_operations;
10195 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010196 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010197 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10198
10199 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10200 if (err)
10201 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010202
Chris Mason39279cc2007-06-12 06:35:45 -040010203 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010204 if (!path) {
10205 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010206 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010207 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010208 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010209 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010210 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010211 datasize = btrfs_file_extent_calc_inline_size(name_len);
10212 err = btrfs_insert_empty_item(trans, root, path, &key,
10213 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010214 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010215 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010216 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010217 }
Chris Mason5f39d392007-10-15 16:14:19 -040010218 leaf = path->nodes[0];
10219 ei = btrfs_item_ptr(leaf, path->slots[0],
10220 struct btrfs_file_extent_item);
10221 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10222 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010223 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010224 btrfs_set_file_extent_encryption(leaf, ei, 0);
10225 btrfs_set_file_extent_compression(leaf, ei, 0);
10226 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10227 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10228
Chris Mason39279cc2007-06-12 06:35:45 -040010229 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010230 write_extent_buffer(leaf, symname, ptr, name_len);
10231 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010232 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010233
Chris Mason39279cc2007-06-12 06:35:45 -040010234 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010235 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010236 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010237 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010238 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010239 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010240 /*
10241 * Last step, add directory indexes for our symlink inode. This is the
10242 * last step to avoid extra cleanup of these indexes if an error happens
10243 * elsewhere above.
10244 */
10245 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010246 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10247 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010248 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010249 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010250 goto out_unlock_inode;
10251 }
10252
10253 unlock_new_inode(inode);
10254 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010255
10256out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010257 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010258 if (drop_inode) {
10259 inode_dec_link_count(inode);
10260 iput(inode);
10261 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010262 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010263 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010264
10265out_unlock_inode:
10266 drop_inode = 1;
10267 unlock_new_inode(inode);
10268 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010269}
Chris Mason16432982008-04-10 10:23:21 -040010270
Josef Bacik0af3d002010-06-21 14:48:16 -040010271static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10272 u64 start, u64 num_bytes, u64 min_size,
10273 loff_t actual_len, u64 *alloc_hint,
10274 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010275{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010276 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010277 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10278 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010279 struct btrfs_root *root = BTRFS_I(inode)->root;
10280 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010281 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010282 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010283 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010284 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010285 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010286 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010287 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010288
Josef Bacik0af3d002010-06-21 14:48:16 -040010289 if (trans)
10290 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010291 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010292 if (own_trans) {
10293 trans = btrfs_start_transaction(root, 3);
10294 if (IS_ERR(trans)) {
10295 ret = PTR_ERR(trans);
10296 break;
10297 }
Yan Zhengd899e052008-10-30 14:25:28 -040010298 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010299
Byongho Leeee221842015-12-15 01:42:10 +090010300 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010301 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010302 /*
10303 * If we are severely fragmented we could end up with really
10304 * small allocations, so if the allocator is returning small
10305 * chunks lets make its job easier by only searching for those
10306 * sized chunks.
10307 */
10308 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010309 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10310 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010311 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010312 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010313 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010314 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010315 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010316 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010317
Josef Bacik0b670dc2015-09-23 17:11:16 -040010318 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010319 ret = insert_reserved_file_extent(trans, inode,
10320 cur_offset, ins.objectid,
10321 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010322 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010323 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010324 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010325 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010326 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010327 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010328 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010329 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010330 break;
10331 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010332
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010333 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010334 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010335
Josef Bacik5dc562c2012-08-17 13:14:17 -040010336 em = alloc_extent_map();
10337 if (!em) {
10338 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10339 &BTRFS_I(inode)->runtime_flags);
10340 goto next;
10341 }
10342
10343 em->start = cur_offset;
10344 em->orig_start = cur_offset;
10345 em->len = ins.offset;
10346 em->block_start = ins.objectid;
10347 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010348 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010349 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010350 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010351 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10352 em->generation = trans->transid;
10353
10354 while (1) {
10355 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010356 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010357 write_unlock(&em_tree->lock);
10358 if (ret != -EEXIST)
10359 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010360 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010361 cur_offset + ins.offset - 1,
10362 0);
10363 }
10364 free_extent_map(em);
10365next:
Yan Zhengd899e052008-10-30 14:25:28 -040010366 num_bytes -= ins.offset;
10367 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010368 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010369
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010370 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010371 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010372 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010373 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010374 (actual_len > inode->i_size) &&
10375 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010376 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010377 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010378 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010379 i_size = cur_offset;
10380 i_size_write(inode, i_size);
10381 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010382 }
10383
Yan Zhengd899e052008-10-30 14:25:28 -040010384 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010385
10386 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010387 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010388 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010389 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010390 break;
10391 }
Yan Zhengd899e052008-10-30 14:25:28 -040010392
Josef Bacik0af3d002010-06-21 14:48:16 -040010393 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010394 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010395 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010396 if (cur_offset < end)
10397 btrfs_free_reserved_data_space(inode, cur_offset,
10398 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010399 return ret;
10400}
10401
Josef Bacik0af3d002010-06-21 14:48:16 -040010402int btrfs_prealloc_file_range(struct inode *inode, int mode,
10403 u64 start, u64 num_bytes, u64 min_size,
10404 loff_t actual_len, u64 *alloc_hint)
10405{
10406 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10407 min_size, actual_len, alloc_hint,
10408 NULL);
10409}
10410
10411int btrfs_prealloc_file_range_trans(struct inode *inode,
10412 struct btrfs_trans_handle *trans, int mode,
10413 u64 start, u64 num_bytes, u64 min_size,
10414 loff_t actual_len, u64 *alloc_hint)
10415{
10416 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10417 min_size, actual_len, alloc_hint, trans);
10418}
10419
Chris Masone6dcd2d2008-07-17 12:53:50 -040010420static int btrfs_set_page_dirty(struct page *page)
10421{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010422 return __set_page_dirty_nobuffers(page);
10423}
10424
Al Viro10556cb2011-06-20 19:28:19 -040010425static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010426{
Li Zefanb83cc962010-12-20 16:04:08 +080010427 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010428 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010429
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010430 if (mask & MAY_WRITE &&
10431 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10432 if (btrfs_root_readonly(root))
10433 return -EROFS;
10434 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10435 return -EACCES;
10436 }
Al Viro2830ba72011-06-20 19:16:29 -040010437 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010438}
Chris Mason39279cc2007-06-12 06:35:45 -040010439
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010440static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10441{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010442 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010443 struct btrfs_trans_handle *trans;
10444 struct btrfs_root *root = BTRFS_I(dir)->root;
10445 struct inode *inode = NULL;
10446 u64 objectid;
10447 u64 index;
10448 int ret = 0;
10449
10450 /*
10451 * 5 units required for adding orphan entry
10452 */
10453 trans = btrfs_start_transaction(root, 5);
10454 if (IS_ERR(trans))
10455 return PTR_ERR(trans);
10456
10457 ret = btrfs_find_free_ino(root, &objectid);
10458 if (ret)
10459 goto out;
10460
10461 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010462 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010463 if (IS_ERR(inode)) {
10464 ret = PTR_ERR(inode);
10465 inode = NULL;
10466 goto out;
10467 }
10468
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010469 inode->i_fop = &btrfs_file_operations;
10470 inode->i_op = &btrfs_file_inode_operations;
10471
10472 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010473 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10474
Chris Masonb0d5d102014-09-08 13:08:51 -070010475 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10476 if (ret)
10477 goto out_inode;
10478
10479 ret = btrfs_update_inode(trans, root, inode);
10480 if (ret)
10481 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010482 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010483 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010484 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010485
Filipe Manana5762b5c2014-08-01 00:10:32 +010010486 /*
10487 * We set number of links to 0 in btrfs_new_inode(), and here we set
10488 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10489 * through:
10490 *
10491 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10492 */
10493 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010494 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010495 d_tmpfile(dentry, inode);
10496 mark_inode_dirty(inode);
10497
10498out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010499 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010500 if (ret)
10501 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010502 btrfs_balance_delayed_items(fs_info);
10503 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010504 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010505
10506out_inode:
10507 unlock_new_inode(inode);
10508 goto out;
10509
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010510}
10511
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010512static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010513 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010514 .lookup = btrfs_lookup,
10515 .create = btrfs_create,
10516 .unlink = btrfs_unlink,
10517 .link = btrfs_link,
10518 .mkdir = btrfs_mkdir,
10519 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010520 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010521 .symlink = btrfs_symlink,
10522 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010523 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010524 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010525 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010526 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010527 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010528 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010529 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010530};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010531static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010532 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010533 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010534 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010535};
Yan, Zheng76dda932009-09-21 16:00:26 -040010536
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010537static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010538 .llseek = generic_file_llseek,
10539 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010540 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010541 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010542#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010543 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010544#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010545 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010546 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010547};
10548
David Sterba20e55062015-11-19 11:42:28 +010010549static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010550 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010551 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010552 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010553 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010554 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010555 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010556 .set_bit_hook = btrfs_set_bit_hook,
10557 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010558 .merge_extent_hook = btrfs_merge_extent_hook,
10559 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010560};
10561
Chris Mason35054392009-01-21 13:11:13 -050010562/*
10563 * btrfs doesn't support the bmap operation because swapfiles
10564 * use bmap to make a mapping of extents in the file. They assume
10565 * these extents won't change over the life of the file and they
10566 * use the bmap result to do IO directly to the drive.
10567 *
10568 * the btrfs bmap call would return logical addresses that aren't
10569 * suitable for IO and they also will change frequently as COW
10570 * operations happen. So, swapfile + btrfs == corruption.
10571 *
10572 * For now we're avoiding this by dropping bmap.
10573 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010574static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010575 .readpage = btrfs_readpage,
10576 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010577 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010578 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010579 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010580 .invalidatepage = btrfs_invalidatepage,
10581 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010582 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010583 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010584};
10585
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010586static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010587 .readpage = btrfs_readpage,
10588 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010589 .invalidatepage = btrfs_invalidatepage,
10590 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010591};
10592
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010593static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010594 .getattr = btrfs_getattr,
10595 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010596 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010597 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010598 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010599 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010600 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010601 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010602};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010603static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010604 .getattr = btrfs_getattr,
10605 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010606 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010607 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010608 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010609 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010610 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010611};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010612static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010613 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010614 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010615 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010616 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010617 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010618 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010619};
Yan, Zheng76dda932009-09-21 16:00:26 -040010620
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010621const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010622 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010623 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010624};