blob: c40060cc481f60440044d00ea4a76904cc4d9761 [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 Masonb888db22007-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 Masonb888db22007-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 Masonb888db22007-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 Masonb888db22007-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,
Robbie Ko91e1f562016-10-07 10:01:29 +08001326 num_bytes)) {
1327 if (!nolock)
1328 btrfs_end_write_no_snapshoting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001329 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001330 }
1331 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1332 if (!nolock)
1333 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001334 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001335 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 nocow = 1;
1337 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1338 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001339 btrfs_file_extent_inline_len(leaf,
1340 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001341 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001342 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 } else {
1344 BUG_ON(1);
1345 }
1346out_check:
1347 if (extent_end <= start) {
1348 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001349 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001350 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001351 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001352 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 goto next_slot;
1354 }
1355 if (!nocow) {
1356 if (cow_start == (u64)-1)
1357 cow_start = cur_offset;
1358 cur_offset = extent_end;
1359 if (cur_offset > end)
1360 break;
1361 path->slots[0]++;
1362 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001363 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001364
David Sterbab3b4aa72011-04-21 01:20:15 +02001365 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001367 ret = cow_file_range(inode, locked_page,
1368 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001369 end, page_started, nr_written, 1,
1370 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001371 if (ret) {
1372 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001373 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001374 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001375 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001376 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001377 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001378 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001380 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001381
Yan Zhengd899e052008-10-30 14:25:28 -04001382 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001383 u64 orig_start = found_key.offset - extent_offset;
1384
1385 em = create_io_em(inode, cur_offset, num_bytes,
1386 orig_start,
1387 disk_bytenr, /* block_start */
1388 num_bytes, /* block_len */
1389 disk_num_bytes, /* orig_block_len */
1390 ram_bytes, BTRFS_COMPRESS_NONE,
1391 BTRFS_ORDERED_PREALLOC);
1392 if (IS_ERR(em)) {
1393 if (!nolock && nocow)
1394 btrfs_end_write_no_snapshoting(root);
1395 if (nocow)
1396 btrfs_dec_nocow_writers(fs_info,
1397 disk_bytenr);
1398 ret = PTR_ERR(em);
1399 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001400 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001401 free_extent_map(em);
1402 }
1403
1404 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001405 type = BTRFS_ORDERED_PREALLOC;
1406 } else {
1407 type = BTRFS_ORDERED_NOCOW;
1408 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001409
1410 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001411 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001412 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001413 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001414 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001415
Yan, Zhengefa56462010-05-16 10:49:59 -04001416 if (root->root_key.objectid ==
1417 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1418 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1419 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001420 if (ret) {
1421 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001422 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001423 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001424 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001425 }
1426
Josef Bacikc2790a22013-07-29 11:20:47 -04001427 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001428 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001429 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001430 EXTENT_DELALLOC |
1431 EXTENT_CLEAR_DATA_RESV,
1432 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1433
Wang Shilonge9894fd2014-03-27 11:12:25 +08001434 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001435 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 cur_offset = extent_end;
1437 if (cur_offset > end)
1438 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001439 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001440 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001441
Josef Bacik17ca04a2012-05-31 15:58:55 -04001442 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001443 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001444 cur_offset = end;
1445 }
1446
Yan Zheng80ff3852008-10-30 14:20:02 -04001447 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001448 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1449 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001450 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001451 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001452 }
1453
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001454error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001455 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001456 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001457 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001458 EXTENT_DELALLOC | EXTENT_DEFRAG |
1459 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1460 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001461 PAGE_SET_WRITEBACK |
1462 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001463 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001464 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001465}
1466
Wang Shilong47059d92014-07-03 18:22:07 +08001467static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1468{
1469
1470 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1471 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1472 return 0;
1473
1474 /*
1475 * @defrag_bytes is a hint value, no spinlock held here,
1476 * if is not zero, it means the file is defragging.
1477 * Force cow if given extent needs to be defragged.
1478 */
1479 if (BTRFS_I(inode)->defrag_bytes &&
1480 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1481 EXTENT_DEFRAG, 0, NULL))
1482 return 1;
1483
1484 return 0;
1485}
1486
Chris Masond352ac62008-09-29 15:18:18 -04001487/*
1488 * extent_io.c call back to do delayed allocation processing
1489 */
Chris Masonc8b97812008-10-29 14:49:59 -04001490static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001491 u64 start, u64 end, int *page_started,
1492 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001493{
Chris Masonbe20aa92007-12-17 20:14:01 -05001494 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001495 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001496
Wang Shilong47059d92014-07-03 18:22:07 +08001497 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001498 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001499 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001500 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001501 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001502 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001503 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001504 ret = cow_file_range(inode, locked_page, start, end, end,
1505 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001506 } else {
1507 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1508 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001509 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001510 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001511 }
Chris Masonb888db22007-08-27 16:49:44 -04001512 return ret;
1513}
1514
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001515static void btrfs_split_extent_hook(struct inode *inode,
1516 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001517{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001518 u64 size;
1519
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001520 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001521 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001522 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001523
Josef Bacikdcab6a32015-02-11 15:08:59 -05001524 size = orig->end - orig->start + 1;
1525 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001526 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001527 u64 new_size;
1528
1529 /*
Josef Bacikba117212015-03-13 15:01:24 -04001530 * See the explanation in btrfs_merge_extent_hook, the same
1531 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001532 */
1533 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001534 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001535 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001536 num_extents += count_max_extents(new_size);
1537 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001538 return;
1539 }
1540
Josef Bacik9e0baf62011-07-15 15:16:44 +00001541 spin_lock(&BTRFS_I(inode)->lock);
1542 BTRFS_I(inode)->outstanding_extents++;
1543 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001544}
1545
1546/*
1547 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1548 * extents so we can keep track of new extents that are just merged onto old
1549 * extents, such as when we are doing sequential writes, so we can properly
1550 * account for the metadata space we'll need.
1551 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001552static void btrfs_merge_extent_hook(struct inode *inode,
1553 struct extent_state *new,
1554 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001555{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001556 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001557 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001558
Josef Bacik9ed74f22009-09-11 16:12:44 -04001559 /* not delalloc, ignore it */
1560 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001561 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001562
Josef Bacik8461a3d2015-03-13 15:12:08 -04001563 if (new->start > other->start)
1564 new_size = new->end - other->start + 1;
1565 else
1566 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001567
1568 /* we're not bigger than the max, unreserve the space and go */
1569 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1570 spin_lock(&BTRFS_I(inode)->lock);
1571 BTRFS_I(inode)->outstanding_extents--;
1572 spin_unlock(&BTRFS_I(inode)->lock);
1573 return;
1574 }
1575
1576 /*
Josef Bacikba117212015-03-13 15:01:24 -04001577 * We have to add up either side to figure out how many extents were
1578 * accounted for before we merged into one big extent. If the number of
1579 * extents we accounted for is <= the amount we need for the new range
1580 * then we can return, otherwise drop. Think of it like this
1581 *
1582 * [ 4k][MAX_SIZE]
1583 *
1584 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1585 * need 2 outstanding extents, on one side we have 1 and the other side
1586 * we have 1 so they are == and we can return. But in this case
1587 *
1588 * [MAX_SIZE+4k][MAX_SIZE+4k]
1589 *
1590 * Each range on their own accounts for 2 extents, but merged together
1591 * they are only 3 extents worth of accounting, so we need to drop in
1592 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001593 */
Josef Bacikba117212015-03-13 15:01:24 -04001594 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001595 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001596 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001597 num_extents += count_max_extents(old_size);
1598 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001599 return;
1600
Josef Bacik9e0baf62011-07-15 15:16:44 +00001601 spin_lock(&BTRFS_I(inode)->lock);
1602 BTRFS_I(inode)->outstanding_extents--;
1603 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001604}
1605
Miao Xieeb73c1b2013-05-15 07:48:22 +00001606static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1607 struct inode *inode)
1608{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001609 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1610
Miao Xieeb73c1b2013-05-15 07:48:22 +00001611 spin_lock(&root->delalloc_lock);
1612 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1613 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1614 &root->delalloc_inodes);
1615 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1616 &BTRFS_I(inode)->runtime_flags);
1617 root->nr_delalloc_inodes++;
1618 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001619 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001620 BUG_ON(!list_empty(&root->delalloc_root));
1621 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001622 &fs_info->delalloc_roots);
1623 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001624 }
1625 }
1626 spin_unlock(&root->delalloc_lock);
1627}
1628
1629static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001630 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001631{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001632 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001633
Miao Xieeb73c1b2013-05-15 07:48:22 +00001634 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001635 if (!list_empty(&inode->delalloc_inodes)) {
1636 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001637 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001638 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001639 root->nr_delalloc_inodes--;
1640 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001641 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001642 BUG_ON(list_empty(&root->delalloc_root));
1643 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001644 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001645 }
1646 }
1647 spin_unlock(&root->delalloc_lock);
1648}
1649
Chris Masond352ac62008-09-29 15:18:18 -04001650/*
1651 * extent_io.c set_bit_hook, used to track delayed allocation
1652 * bytes in this file, and to maintain the list of inodes that
1653 * have pending delalloc work to be done.
1654 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001655static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001656 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001657{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001658
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001659 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1660
Wang Shilong47059d92014-07-03 18:22:07 +08001661 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1662 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001663 /*
1664 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001665 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001666 * bit, which is only set or cleared with irqs on
1667 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001668 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001669 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001670 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001671 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001672
Josef Bacik9e0baf62011-07-15 15:16:44 +00001673 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001674 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001675 } else {
1676 spin_lock(&BTRFS_I(inode)->lock);
1677 BTRFS_I(inode)->outstanding_extents++;
1678 spin_unlock(&BTRFS_I(inode)->lock);
1679 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001680
Josef Bacik6a3891c2015-03-16 17:38:52 -04001681 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001682 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001683 return;
1684
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001685 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1686 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001687 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001688 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001689 if (*bits & EXTENT_DEFRAG)
1690 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001691 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001692 &BTRFS_I(inode)->runtime_flags))
1693 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001694 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001695 }
Chris Mason291d6732008-01-29 15:55:23 -05001696}
1697
Chris Masond352ac62008-09-29 15:18:18 -04001698/*
1699 * extent_io.c clear_bit_hook, see set_bit_hook for why
1700 */
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001701static void btrfs_clear_bit_hook(struct btrfs_inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001702 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001703 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001704{
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001705 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001706 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001707 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001708
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001709 spin_lock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001710 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001711 inode->defrag_bytes -= len;
1712 spin_unlock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001713
Chris Mason75eff682008-12-15 15:54:40 -05001714 /*
1715 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001716 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001717 * bit, which is only set or cleared with irqs on
1718 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001719 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001720 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001721 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001722
Josef Bacik9e0baf62011-07-15 15:16:44 +00001723 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001724 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001725 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001726 spin_lock(&inode->lock);
1727 inode->outstanding_extents -= num_extents;
1728 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001729 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001730
Josef Bacikb6d08f02013-09-27 14:57:43 -04001731 /*
1732 * We don't reserve metadata space for space cache inodes so we
1733 * don't need to call dellalloc_release_metadata if there is an
1734 * error.
1735 */
1736 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001738 btrfs_delalloc_release_metadata(inode, len);
1739
Josef Bacik6a3891c2015-03-16 17:38:52 -04001740 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001741 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001742 return;
1743
Josef Bacik0cb59c92010-07-02 12:14:14 -04001744 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001745 && do_list && !(state->state & EXTENT_NORESERVE)
1746 && (*bits & (EXTENT_DO_ACCOUNTING |
1747 EXTENT_CLEAR_DATA_RESV)))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001748 btrfs_free_reserved_data_space_noquota(
1749 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001750 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001751
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001752 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1753 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001754 spin_lock(&inode->lock);
1755 inode->delalloc_bytes -= len;
1756 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001757 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001758 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001759 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001760 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001761 }
Chris Mason291d6732008-01-29 15:55:23 -05001762}
1763
Chris Masond352ac62008-09-29 15:18:18 -04001764/*
1765 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1766 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001767 *
1768 * return 1 if page cannot be merged to bio
1769 * return 0 if page can be merged to bio
1770 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001771 */
Mike Christie81a75f672016-06-05 14:31:54 -05001772int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001773 size_t size, struct bio *bio,
1774 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001775{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001776 struct inode *inode = page->mapping->host;
1777 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001778 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001779 u64 length = 0;
1780 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001781 int ret;
1782
Chris Mason771ed682008-11-06 22:02:51 -05001783 if (bio_flags & EXTENT_BIO_COMPRESSED)
1784 return 0;
1785
Kent Overstreet4f024f32013-10-11 15:44:27 -07001786 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001787 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001788 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1789 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001790 if (ret < 0)
1791 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001792 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001793 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001794 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001795}
1796
Chris Masond352ac62008-09-29 15:18:18 -04001797/*
1798 * in order to insert checksums into the metadata in large chunks,
1799 * we wait until bio submission time. All the pages in the bio are
1800 * checksummed and sums are attached onto the ordered extent record.
1801 *
1802 * At IO completion time the cums attached on the ordered extent record
1803 * are inserted into the btree
1804 */
Mike Christie81a75f672016-06-05 14:31:54 -05001805static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1806 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001807 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001808{
Chris Mason065631f2008-02-20 12:07:25 -05001809 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001810
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001811 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001812 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001813 return 0;
1814}
Chris Masone0156402008-04-16 11:15:20 -04001815
Chris Mason4a69a412008-11-06 22:03:00 -05001816/*
1817 * in order to insert checksums into the metadata in large chunks,
1818 * we wait until bio submission time. All the pages in the bio are
1819 * checksummed and sums are attached onto the ordered extent record.
1820 *
1821 * At IO completion time the cums attached on the ordered extent record
1822 * are inserted into the btree
1823 */
Mike Christie81a75f672016-06-05 14:31:54 -05001824static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001825 int mirror_num, unsigned long bio_flags,
1826 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001827{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001828 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001829 int ret;
1830
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001831 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001832 if (ret) {
1833 bio->bi_error = ret;
1834 bio_endio(bio);
1835 }
Stefan Behrens61891922012-11-05 18:51:52 +01001836 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001837}
1838
Chris Masond352ac62008-09-29 15:18:18 -04001839/*
Chris Masoncad321a2008-12-17 14:51:42 -05001840 * extent_io.c submission hook. This does the right thing for csum calculation
1841 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001842 */
Mike Christie81a75f672016-06-05 14:31:54 -05001843static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001844 int mirror_num, unsigned long bio_flags,
1845 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001846{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001847 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001848 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301849 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001850 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001851 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001852 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001853
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001854 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001855
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001856 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301857 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001858
Mike Christie37226b22016-06-05 14:31:52 -05001859 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001860 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001861 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001862 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001863
Chris Masond20f7042008-12-08 16:58:54 -05001864 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001865 ret = btrfs_submit_compressed_read(inode, bio,
1866 mirror_num,
1867 bio_flags);
1868 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001869 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001870 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001871 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001872 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001873 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001874 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001875 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001876 /* csum items have already been cloned */
1877 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1878 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001879 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001880 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1881 bio_flags, bio_offset,
1882 __btrfs_submit_bio_start,
1883 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001884 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001885 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001886 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001887 if (ret)
1888 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001889 }
1890
Chris Mason0b86a832008-03-24 15:01:56 -04001891mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001892 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001893
1894out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001895 if (ret < 0) {
1896 bio->bi_error = ret;
1897 bio_endio(bio);
1898 }
Stefan Behrens61891922012-11-05 18:51:52 +01001899 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001900}
Chris Mason6885f302008-02-20 16:11:05 -05001901
Chris Masond352ac62008-09-29 15:18:18 -04001902/*
1903 * given a list of ordered sums record them in the inode. This happens
1904 * at IO completion time based on sums calculated at bio submission time.
1905 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001906static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01001907 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001908{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001909 struct btrfs_ordered_sum *sum;
1910
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001911 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001912 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001913 btrfs_csum_file_blocks(trans,
1914 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001915 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001916 }
1917 return 0;
1918}
1919
Josef Bacik2ac55d42010-02-03 19:33:23 +00001920int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001921 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04001922{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001923 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001924 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001925 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001926}
1927
Chris Masond352ac62008-09-29 15:18:18 -04001928/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001929struct btrfs_writepage_fixup {
1930 struct page *page;
1931 struct btrfs_work work;
1932};
1933
Christoph Hellwigb2950862008-12-02 09:54:17 -05001934static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001935{
1936 struct btrfs_writepage_fixup *fixup;
1937 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001938 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001939 struct page *page;
1940 struct inode *inode;
1941 u64 page_start;
1942 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001943 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001944
1945 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1946 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001947again:
Chris Mason247e7432008-07-17 12:53:51 -04001948 lock_page(page);
1949 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1950 ClearPageChecked(page);
1951 goto out_page;
1952 }
1953
1954 inode = page->mapping->host;
1955 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001956 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04001957
David Sterbaff13db42015-12-03 14:30:40 +01001958 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001959 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001960
1961 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001962 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001963 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001964
Nikolay Borisova776c6f2017-02-20 13:50:49 +02001965 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001966 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04001967 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001968 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1969 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001970 unlock_page(page);
1971 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001972 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001973 goto again;
1974 }
Chris Mason247e7432008-07-17 12:53:51 -04001975
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001976 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001977 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001978 if (ret) {
1979 mapping_set_error(page->mapping, ret);
1980 end_extent_writepage(page, ret, page_start, page_end);
1981 ClearPageChecked(page);
1982 goto out;
1983 }
1984
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001985 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
1986 0);
Chris Mason247e7432008-07-17 12:53:51 -04001987 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001988 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001989out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001990 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1991 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001992out_page:
1993 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001994 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001995 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001996}
1997
1998/*
1999 * There are a few paths in the higher layers of the kernel that directly
2000 * set the page dirty bit without asking the filesystem if it is a
2001 * good idea. This causes problems because we want to make sure COW
2002 * properly happens and the data=ordered rules are followed.
2003 *
Chris Masonc8b97812008-10-29 14:49:59 -04002004 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002005 * hasn't been properly setup for IO. We kick off an async process
2006 * to fix it up. The async helper will wait for ordered extents, set
2007 * the delalloc bit and make it safe to write the page.
2008 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002009static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002010{
2011 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002012 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002013 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002014
Chris Mason8b62b722009-09-02 16:53:46 -04002015 /* this page is properly in the ordered list */
2016 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002017 return 0;
2018
2019 if (PageChecked(page))
2020 return -EAGAIN;
2021
2022 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2023 if (!fixup)
2024 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002025
Chris Mason247e7432008-07-17 12:53:51 -04002026 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002027 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002028 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2029 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002030 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002031 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002032 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002033}
2034
Yan Zhengd899e052008-10-30 14:25:28 -04002035static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2036 struct inode *inode, u64 file_pos,
2037 u64 disk_bytenr, u64 disk_num_bytes,
2038 u64 num_bytes, u64 ram_bytes,
2039 u8 compression, u8 encryption,
2040 u16 other_encoding, int extent_type)
2041{
2042 struct btrfs_root *root = BTRFS_I(inode)->root;
2043 struct btrfs_file_extent_item *fi;
2044 struct btrfs_path *path;
2045 struct extent_buffer *leaf;
2046 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002047 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002048 int ret;
2049
2050 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002051 if (!path)
2052 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002053
Chris Masona1ed8352009-09-11 12:27:37 -04002054 /*
2055 * we may be replacing one extent in the tree with another.
2056 * The new extent is pinned in the extent map, and we don't want
2057 * to drop it from the cache until it is completely in the btree.
2058 *
2059 * So, tell btrfs_drop_extents to leave this extent in the cache.
2060 * the caller is expected to unpin it and allow it to be merged
2061 * with the others.
2062 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002063 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2064 file_pos + num_bytes, NULL, 0,
2065 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002066 if (ret)
2067 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002068
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002069 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002070 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002071 ins.offset = file_pos;
2072 ins.type = BTRFS_EXTENT_DATA_KEY;
2073
2074 path->leave_spinning = 1;
2075 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2076 sizeof(*fi));
2077 if (ret)
2078 goto out;
2079 }
Yan Zhengd899e052008-10-30 14:25:28 -04002080 leaf = path->nodes[0];
2081 fi = btrfs_item_ptr(leaf, path->slots[0],
2082 struct btrfs_file_extent_item);
2083 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2084 btrfs_set_file_extent_type(leaf, fi, extent_type);
2085 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2086 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2087 btrfs_set_file_extent_offset(leaf, fi, 0);
2088 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2089 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2090 btrfs_set_file_extent_compression(leaf, fi, compression);
2091 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2092 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002093
Yan Zhengd899e052008-10-30 14:25:28 -04002094 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002095 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002096
2097 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002098
2099 ins.objectid = disk_bytenr;
2100 ins.offset = disk_num_bytes;
2101 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002102 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002103 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002104 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002105 * Release the reserved range from inode dirty range map, as it is
2106 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002107 */
2108 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002109out:
Yan Zhengd899e052008-10-30 14:25:28 -04002110 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002111
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002112 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002113}
2114
Liu Bo38c227d2013-01-29 03:18:40 +00002115/* snapshot-aware defrag */
2116struct sa_defrag_extent_backref {
2117 struct rb_node node;
2118 struct old_sa_defrag_extent *old;
2119 u64 root_id;
2120 u64 inum;
2121 u64 file_pos;
2122 u64 extent_offset;
2123 u64 num_bytes;
2124 u64 generation;
2125};
2126
2127struct old_sa_defrag_extent {
2128 struct list_head list;
2129 struct new_sa_defrag_extent *new;
2130
2131 u64 extent_offset;
2132 u64 bytenr;
2133 u64 offset;
2134 u64 len;
2135 int count;
2136};
2137
2138struct new_sa_defrag_extent {
2139 struct rb_root root;
2140 struct list_head head;
2141 struct btrfs_path *path;
2142 struct inode *inode;
2143 u64 file_pos;
2144 u64 len;
2145 u64 bytenr;
2146 u64 disk_len;
2147 u8 compress_type;
2148};
2149
2150static int backref_comp(struct sa_defrag_extent_backref *b1,
2151 struct sa_defrag_extent_backref *b2)
2152{
2153 if (b1->root_id < b2->root_id)
2154 return -1;
2155 else if (b1->root_id > b2->root_id)
2156 return 1;
2157
2158 if (b1->inum < b2->inum)
2159 return -1;
2160 else if (b1->inum > b2->inum)
2161 return 1;
2162
2163 if (b1->file_pos < b2->file_pos)
2164 return -1;
2165 else if (b1->file_pos > b2->file_pos)
2166 return 1;
2167
2168 /*
2169 * [------------------------------] ===> (a range of space)
2170 * |<--->| |<---->| =============> (fs/file tree A)
2171 * |<---------------------------->| ===> (fs/file tree B)
2172 *
2173 * A range of space can refer to two file extents in one tree while
2174 * refer to only one file extent in another tree.
2175 *
2176 * So we may process a disk offset more than one time(two extents in A)
2177 * and locate at the same extent(one extent in B), then insert two same
2178 * backrefs(both refer to the extent in B).
2179 */
2180 return 0;
2181}
2182
2183static void backref_insert(struct rb_root *root,
2184 struct sa_defrag_extent_backref *backref)
2185{
2186 struct rb_node **p = &root->rb_node;
2187 struct rb_node *parent = NULL;
2188 struct sa_defrag_extent_backref *entry;
2189 int ret;
2190
2191 while (*p) {
2192 parent = *p;
2193 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2194
2195 ret = backref_comp(backref, entry);
2196 if (ret < 0)
2197 p = &(*p)->rb_left;
2198 else
2199 p = &(*p)->rb_right;
2200 }
2201
2202 rb_link_node(&backref->node, parent, p);
2203 rb_insert_color(&backref->node, root);
2204}
2205
2206/*
2207 * Note the backref might has changed, and in this case we just return 0.
2208 */
2209static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2210 void *ctx)
2211{
2212 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002213 struct old_sa_defrag_extent *old = ctx;
2214 struct new_sa_defrag_extent *new = old->new;
2215 struct btrfs_path *path = new->path;
2216 struct btrfs_key key;
2217 struct btrfs_root *root;
2218 struct sa_defrag_extent_backref *backref;
2219 struct extent_buffer *leaf;
2220 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002221 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002222 int slot;
2223 int ret;
2224 u64 extent_offset;
2225 u64 num_bytes;
2226
2227 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002228 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002229 return 0;
2230
2231 key.objectid = root_id;
2232 key.type = BTRFS_ROOT_ITEM_KEY;
2233 key.offset = (u64)-1;
2234
Liu Bo38c227d2013-01-29 03:18:40 +00002235 root = btrfs_read_fs_root_no_name(fs_info, &key);
2236 if (IS_ERR(root)) {
2237 if (PTR_ERR(root) == -ENOENT)
2238 return 0;
2239 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002240 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002241 inum, offset, root_id);
2242 return PTR_ERR(root);
2243 }
2244
2245 key.objectid = inum;
2246 key.type = BTRFS_EXTENT_DATA_KEY;
2247 if (offset > (u64)-1 << 32)
2248 key.offset = 0;
2249 else
2250 key.offset = offset;
2251
2252 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302253 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002254 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002255 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002256
2257 while (1) {
2258 cond_resched();
2259
2260 leaf = path->nodes[0];
2261 slot = path->slots[0];
2262
2263 if (slot >= btrfs_header_nritems(leaf)) {
2264 ret = btrfs_next_leaf(root, path);
2265 if (ret < 0) {
2266 goto out;
2267 } else if (ret > 0) {
2268 ret = 0;
2269 goto out;
2270 }
2271 continue;
2272 }
2273
2274 path->slots[0]++;
2275
2276 btrfs_item_key_to_cpu(leaf, &key, slot);
2277
2278 if (key.objectid > inum)
2279 goto out;
2280
2281 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2282 continue;
2283
2284 extent = btrfs_item_ptr(leaf, slot,
2285 struct btrfs_file_extent_item);
2286
2287 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2288 continue;
2289
Liu Boe68afa42013-07-01 22:13:26 +08002290 /*
2291 * 'offset' refers to the exact key.offset,
2292 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2293 * (key.offset - extent_offset).
2294 */
2295 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002296 continue;
2297
Liu Boe68afa42013-07-01 22:13:26 +08002298 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002299 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002300
Liu Bo38c227d2013-01-29 03:18:40 +00002301 if (extent_offset >= old->extent_offset + old->offset +
2302 old->len || extent_offset + num_bytes <=
2303 old->extent_offset + old->offset)
2304 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002305 break;
2306 }
2307
2308 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2309 if (!backref) {
2310 ret = -ENOENT;
2311 goto out;
2312 }
2313
2314 backref->root_id = root_id;
2315 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002316 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002317 backref->num_bytes = num_bytes;
2318 backref->extent_offset = extent_offset;
2319 backref->generation = btrfs_file_extent_generation(leaf, extent);
2320 backref->old = old;
2321 backref_insert(&new->root, backref);
2322 old->count++;
2323out:
2324 btrfs_release_path(path);
2325 WARN_ON(ret);
2326 return ret;
2327}
2328
2329static noinline bool record_extent_backrefs(struct btrfs_path *path,
2330 struct new_sa_defrag_extent *new)
2331{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002332 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002333 struct old_sa_defrag_extent *old, *tmp;
2334 int ret;
2335
2336 new->path = path;
2337
2338 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002339 ret = iterate_inodes_from_logical(old->bytenr +
2340 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002341 path, record_one_backref,
2342 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002343 if (ret < 0 && ret != -ENOENT)
2344 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002345
2346 /* no backref to be processed for this extent */
2347 if (!old->count) {
2348 list_del(&old->list);
2349 kfree(old);
2350 }
2351 }
2352
2353 if (list_empty(&new->head))
2354 return false;
2355
2356 return true;
2357}
2358
2359static int relink_is_mergable(struct extent_buffer *leaf,
2360 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002361 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002362{
Liu Bo116e0022013-08-02 16:30:40 +08002363 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002364 return 0;
2365
2366 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2367 return 0;
2368
Liu Bo116e0022013-08-02 16:30:40 +08002369 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2370 return 0;
2371
2372 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002373 btrfs_file_extent_other_encoding(leaf, fi))
2374 return 0;
2375
2376 return 1;
2377}
2378
2379/*
2380 * Note the backref might has changed, and in this case we just return 0.
2381 */
2382static noinline int relink_extent_backref(struct btrfs_path *path,
2383 struct sa_defrag_extent_backref *prev,
2384 struct sa_defrag_extent_backref *backref)
2385{
2386 struct btrfs_file_extent_item *extent;
2387 struct btrfs_file_extent_item *item;
2388 struct btrfs_ordered_extent *ordered;
2389 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002390 struct btrfs_root *root;
2391 struct btrfs_key key;
2392 struct extent_buffer *leaf;
2393 struct old_sa_defrag_extent *old = backref->old;
2394 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002395 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002396 struct inode *inode;
2397 struct extent_state *cached = NULL;
2398 int ret = 0;
2399 u64 start;
2400 u64 len;
2401 u64 lock_start;
2402 u64 lock_end;
2403 bool merge = false;
2404 int index;
2405
2406 if (prev && prev->root_id == backref->root_id &&
2407 prev->inum == backref->inum &&
2408 prev->file_pos + prev->num_bytes == backref->file_pos)
2409 merge = true;
2410
2411 /* step 1: get root */
2412 key.objectid = backref->root_id;
2413 key.type = BTRFS_ROOT_ITEM_KEY;
2414 key.offset = (u64)-1;
2415
Liu Bo38c227d2013-01-29 03:18:40 +00002416 index = srcu_read_lock(&fs_info->subvol_srcu);
2417
2418 root = btrfs_read_fs_root_no_name(fs_info, &key);
2419 if (IS_ERR(root)) {
2420 srcu_read_unlock(&fs_info->subvol_srcu, index);
2421 if (PTR_ERR(root) == -ENOENT)
2422 return 0;
2423 return PTR_ERR(root);
2424 }
Liu Bo38c227d2013-01-29 03:18:40 +00002425
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002426 if (btrfs_root_readonly(root)) {
2427 srcu_read_unlock(&fs_info->subvol_srcu, index);
2428 return 0;
2429 }
2430
Liu Bo38c227d2013-01-29 03:18:40 +00002431 /* step 2: get inode */
2432 key.objectid = backref->inum;
2433 key.type = BTRFS_INODE_ITEM_KEY;
2434 key.offset = 0;
2435
2436 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2437 if (IS_ERR(inode)) {
2438 srcu_read_unlock(&fs_info->subvol_srcu, index);
2439 return 0;
2440 }
2441
2442 srcu_read_unlock(&fs_info->subvol_srcu, index);
2443
2444 /* step 3: relink backref */
2445 lock_start = backref->file_pos;
2446 lock_end = backref->file_pos + backref->num_bytes - 1;
2447 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002448 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002449
2450 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2451 if (ordered) {
2452 btrfs_put_ordered_extent(ordered);
2453 goto out_unlock;
2454 }
2455
2456 trans = btrfs_join_transaction(root);
2457 if (IS_ERR(trans)) {
2458 ret = PTR_ERR(trans);
2459 goto out_unlock;
2460 }
2461
2462 key.objectid = backref->inum;
2463 key.type = BTRFS_EXTENT_DATA_KEY;
2464 key.offset = backref->file_pos;
2465
2466 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2467 if (ret < 0) {
2468 goto out_free_path;
2469 } else if (ret > 0) {
2470 ret = 0;
2471 goto out_free_path;
2472 }
2473
2474 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2475 struct btrfs_file_extent_item);
2476
2477 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2478 backref->generation)
2479 goto out_free_path;
2480
2481 btrfs_release_path(path);
2482
2483 start = backref->file_pos;
2484 if (backref->extent_offset < old->extent_offset + old->offset)
2485 start += old->extent_offset + old->offset -
2486 backref->extent_offset;
2487
2488 len = min(backref->extent_offset + backref->num_bytes,
2489 old->extent_offset + old->offset + old->len);
2490 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2491
2492 ret = btrfs_drop_extents(trans, root, inode, start,
2493 start + len, 1);
2494 if (ret)
2495 goto out_free_path;
2496again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002497 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002498 key.type = BTRFS_EXTENT_DATA_KEY;
2499 key.offset = start;
2500
Liu Boa09a0a72013-03-11 09:20:58 +00002501 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002502 if (merge) {
2503 struct btrfs_file_extent_item *fi;
2504 u64 extent_len;
2505 struct btrfs_key found_key;
2506
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002507 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002508 if (ret < 0)
2509 goto out_free_path;
2510
2511 path->slots[0]--;
2512 leaf = path->nodes[0];
2513 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2514
2515 fi = btrfs_item_ptr(leaf, path->slots[0],
2516 struct btrfs_file_extent_item);
2517 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2518
Liu Bo116e0022013-08-02 16:30:40 +08002519 if (extent_len + found_key.offset == start &&
2520 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002521 btrfs_set_file_extent_num_bytes(leaf, fi,
2522 extent_len + len);
2523 btrfs_mark_buffer_dirty(leaf);
2524 inode_add_bytes(inode, len);
2525
2526 ret = 1;
2527 goto out_free_path;
2528 } else {
2529 merge = false;
2530 btrfs_release_path(path);
2531 goto again;
2532 }
2533 }
2534
2535 ret = btrfs_insert_empty_item(trans, root, path, &key,
2536 sizeof(*extent));
2537 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002538 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002539 goto out_free_path;
2540 }
2541
2542 leaf = path->nodes[0];
2543 item = btrfs_item_ptr(leaf, path->slots[0],
2544 struct btrfs_file_extent_item);
2545 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2546 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2547 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2548 btrfs_set_file_extent_num_bytes(leaf, item, len);
2549 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2550 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2551 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2552 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2553 btrfs_set_file_extent_encryption(leaf, item, 0);
2554 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2555
2556 btrfs_mark_buffer_dirty(leaf);
2557 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002558 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002559
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002560 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002561 new->disk_len, 0,
2562 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002563 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002564 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002565 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002566 goto out_free_path;
2567 }
2568
2569 ret = 1;
2570out_free_path:
2571 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002572 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002573 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002574out_unlock:
2575 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2576 &cached, GFP_NOFS);
2577 iput(inode);
2578 return ret;
2579}
2580
Liu Bo6f519562013-10-29 10:45:05 +08002581static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2582{
2583 struct old_sa_defrag_extent *old, *tmp;
2584
2585 if (!new)
2586 return;
2587
2588 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002589 kfree(old);
2590 }
2591 kfree(new);
2592}
2593
Liu Bo38c227d2013-01-29 03:18:40 +00002594static void relink_file_extents(struct new_sa_defrag_extent *new)
2595{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002596 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002597 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002598 struct sa_defrag_extent_backref *backref;
2599 struct sa_defrag_extent_backref *prev = NULL;
2600 struct inode *inode;
2601 struct btrfs_root *root;
2602 struct rb_node *node;
2603 int ret;
2604
2605 inode = new->inode;
2606 root = BTRFS_I(inode)->root;
2607
2608 path = btrfs_alloc_path();
2609 if (!path)
2610 return;
2611
2612 if (!record_extent_backrefs(path, new)) {
2613 btrfs_free_path(path);
2614 goto out;
2615 }
2616 btrfs_release_path(path);
2617
2618 while (1) {
2619 node = rb_first(&new->root);
2620 if (!node)
2621 break;
2622 rb_erase(node, &new->root);
2623
2624 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2625
2626 ret = relink_extent_backref(path, prev, backref);
2627 WARN_ON(ret < 0);
2628
2629 kfree(prev);
2630
2631 if (ret == 1)
2632 prev = backref;
2633 else
2634 prev = NULL;
2635 cond_resched();
2636 }
2637 kfree(prev);
2638
2639 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002640out:
Liu Bo6f519562013-10-29 10:45:05 +08002641 free_sa_defrag_extent(new);
2642
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002643 atomic_dec(&fs_info->defrag_running);
2644 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002645}
2646
2647static struct new_sa_defrag_extent *
2648record_old_file_extents(struct inode *inode,
2649 struct btrfs_ordered_extent *ordered)
2650{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002651 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002652 struct btrfs_root *root = BTRFS_I(inode)->root;
2653 struct btrfs_path *path;
2654 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002655 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002656 struct new_sa_defrag_extent *new;
2657 int ret;
2658
2659 new = kmalloc(sizeof(*new), GFP_NOFS);
2660 if (!new)
2661 return NULL;
2662
2663 new->inode = inode;
2664 new->file_pos = ordered->file_offset;
2665 new->len = ordered->len;
2666 new->bytenr = ordered->start;
2667 new->disk_len = ordered->disk_len;
2668 new->compress_type = ordered->compress_type;
2669 new->root = RB_ROOT;
2670 INIT_LIST_HEAD(&new->head);
2671
2672 path = btrfs_alloc_path();
2673 if (!path)
2674 goto out_kfree;
2675
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002676 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002677 key.type = BTRFS_EXTENT_DATA_KEY;
2678 key.offset = new->file_pos;
2679
2680 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2681 if (ret < 0)
2682 goto out_free_path;
2683 if (ret > 0 && path->slots[0] > 0)
2684 path->slots[0]--;
2685
2686 /* find out all the old extents for the file range */
2687 while (1) {
2688 struct btrfs_file_extent_item *extent;
2689 struct extent_buffer *l;
2690 int slot;
2691 u64 num_bytes;
2692 u64 offset;
2693 u64 end;
2694 u64 disk_bytenr;
2695 u64 extent_offset;
2696
2697 l = path->nodes[0];
2698 slot = path->slots[0];
2699
2700 if (slot >= btrfs_header_nritems(l)) {
2701 ret = btrfs_next_leaf(root, path);
2702 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002703 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002704 else if (ret > 0)
2705 break;
2706 continue;
2707 }
2708
2709 btrfs_item_key_to_cpu(l, &key, slot);
2710
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002711 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002712 break;
2713 if (key.type != BTRFS_EXTENT_DATA_KEY)
2714 break;
2715 if (key.offset >= new->file_pos + new->len)
2716 break;
2717
2718 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2719
2720 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2721 if (key.offset + num_bytes < new->file_pos)
2722 goto next;
2723
2724 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2725 if (!disk_bytenr)
2726 goto next;
2727
2728 extent_offset = btrfs_file_extent_offset(l, extent);
2729
2730 old = kmalloc(sizeof(*old), GFP_NOFS);
2731 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002732 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002733
2734 offset = max(new->file_pos, key.offset);
2735 end = min(new->file_pos + new->len, key.offset + num_bytes);
2736
2737 old->bytenr = disk_bytenr;
2738 old->extent_offset = extent_offset;
2739 old->offset = offset - key.offset;
2740 old->len = end - offset;
2741 old->new = new;
2742 old->count = 0;
2743 list_add_tail(&old->list, &new->head);
2744next:
2745 path->slots[0]++;
2746 cond_resched();
2747 }
2748
2749 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002750 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002751
2752 return new;
2753
Liu Bo38c227d2013-01-29 03:18:40 +00002754out_free_path:
2755 btrfs_free_path(path);
2756out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002757 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002758 return NULL;
2759}
2760
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002761static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002762 u64 start, u64 len)
2763{
2764 struct btrfs_block_group_cache *cache;
2765
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002766 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002767 ASSERT(cache);
2768
2769 spin_lock(&cache->lock);
2770 cache->delalloc_bytes -= len;
2771 spin_unlock(&cache->lock);
2772
2773 btrfs_put_block_group(cache);
2774}
2775
Chris Masond352ac62008-09-29 15:18:18 -04002776/* as ordered data IO finishes, this gets called so we can finish
2777 * an ordered extent if the range of bytes in the file it covers are
2778 * fully written.
2779 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002780static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002781{
Josef Bacik5fd02042012-05-02 14:00:54 -04002782 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002783 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002784 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002785 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002786 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002787 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002788 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002789 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002790 int ret = 0;
2791 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002792 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002793 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002794
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002795 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002796
Josef Bacik5fd02042012-05-02 14:00:54 -04002797 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2798 ret = -EIO;
2799 goto out;
2800 }
2801
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002802 btrfs_free_io_failure_record(BTRFS_I(inode),
2803 ordered_extent->file_offset,
2804 ordered_extent->file_offset +
2805 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002806
Josef Bacik77cef2e2013-08-29 13:57:21 -04002807 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2808 truncated = true;
2809 logical_len = ordered_extent->truncated_len;
2810 /* Truncated the entire extent, don't bother adding */
2811 if (!logical_len)
2812 goto out;
2813 }
2814
Yan, Zhengc2167752009-11-12 09:34:21 +00002815 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002816 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002817
2818 /*
2819 * For mwrite(mmap + memset to write) case, we still reserve
2820 * space for NOCOW range.
2821 * As NOCOW won't cause a new delayed ref, just free the space
2822 */
2823 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2824 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002825 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2826 if (nolock)
2827 trans = btrfs_join_transaction_nolock(root);
2828 else
2829 trans = btrfs_join_transaction(root);
2830 if (IS_ERR(trans)) {
2831 ret = PTR_ERR(trans);
2832 trans = NULL;
2833 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002834 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002835 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002836 ret = btrfs_update_inode_fallback(trans, root, inode);
2837 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002838 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002839 goto out;
2840 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002841
Josef Bacik2ac55d42010-02-03 19:33:23 +00002842 lock_extent_bits(io_tree, ordered_extent->file_offset,
2843 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002844 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002845
Liu Bo38c227d2013-01-29 03:18:40 +00002846 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2847 ordered_extent->file_offset + ordered_extent->len - 1,
2848 EXTENT_DEFRAG, 1, cached_state);
2849 if (ret) {
2850 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002851 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002852 /* the inode is shared */
2853 new = record_old_file_extents(inode, ordered_extent);
2854
2855 clear_extent_bit(io_tree, ordered_extent->file_offset,
2856 ordered_extent->file_offset + ordered_extent->len - 1,
2857 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2858 }
2859
Josef Bacik0cb59c92010-07-02 12:14:14 -04002860 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002861 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002862 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002863 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002864 if (IS_ERR(trans)) {
2865 ret = PTR_ERR(trans);
2866 trans = NULL;
2867 goto out_unlock;
2868 }
Chris Masona79b7d42014-05-22 16:18:52 -07002869
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002870 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002871
Chris Masonc8b97812008-10-29 14:49:59 -04002872 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002873 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002874 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002875 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002876 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002877 ordered_extent->file_offset,
2878 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002879 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002880 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002881 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002882 ret = insert_reserved_file_extent(trans, inode,
2883 ordered_extent->file_offset,
2884 ordered_extent->start,
2885 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002886 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002887 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002888 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002889 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002890 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002891 ordered_extent->start,
2892 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002893 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002894 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2895 ordered_extent->file_offset, ordered_extent->len,
2896 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002897 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002898 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002899 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002900 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002901
David Sterbadf9f6282017-02-10 19:35:37 +01002902 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002903
Josef Bacik6c760c02012-11-09 10:53:21 -05002904 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2905 ret = btrfs_update_inode_fallback(trans, root, inode);
2906 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002907 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002908 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002909 }
2910 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002911out_unlock:
2912 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2913 ordered_extent->file_offset +
2914 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002915out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002916 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02002917 btrfs_delalloc_release_metadata(BTRFS_I(inode),
2918 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002919 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002920 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002921
Josef Bacik77cef2e2013-08-29 13:57:21 -04002922 if (ret || truncated) {
2923 u64 start, end;
2924
2925 if (truncated)
2926 start = ordered_extent->file_offset + logical_len;
2927 else
2928 start = ordered_extent->file_offset;
2929 end = ordered_extent->file_offset + ordered_extent->len - 1;
2930 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2931
2932 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02002933 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002934
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002935 /*
2936 * If the ordered extent had an IOERR or something else went
2937 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002938 * back to the allocator. We only free the extent in the
2939 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002940 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002941 if ((ret || !logical_len) &&
2942 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002943 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002944 btrfs_free_reserved_extent(fs_info,
2945 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002946 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002947 }
2948
2949
Josef Bacik5fd02042012-05-02 14:00:54 -04002950 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002951 * This needs to be done to make sure anybody waiting knows we are done
2952 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002953 */
2954 btrfs_remove_ordered_extent(inode, ordered_extent);
2955
Liu Bo38c227d2013-01-29 03:18:40 +00002956 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002957 if (new) {
2958 if (ret) {
2959 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002960 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08002961 } else {
2962 relink_file_extents(new);
2963 }
2964 }
Liu Bo38c227d2013-01-29 03:18:40 +00002965
Chris Masone6dcd2d2008-07-17 12:53:50 -04002966 /* once for us */
2967 btrfs_put_ordered_extent(ordered_extent);
2968 /* once for the tree */
2969 btrfs_put_ordered_extent(ordered_extent);
2970
Josef Bacik5fd02042012-05-02 14:00:54 -04002971 return ret;
2972}
2973
2974static void finish_ordered_fn(struct btrfs_work *work)
2975{
2976 struct btrfs_ordered_extent *ordered_extent;
2977 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2978 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002979}
2980
David Sterbac3988d62017-02-17 15:18:32 +01002981static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002982 struct extent_state *state, int uptodate)
2983{
Josef Bacik5fd02042012-05-02 14:00:54 -04002984 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002985 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002986 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002987 struct btrfs_workqueue *wq;
2988 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002989
liubo1abe9b82011-03-24 11:18:59 +00002990 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2991
Chris Mason8b62b722009-09-02 16:53:46 -04002992 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002993 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2994 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002995 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002996
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002997 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002998 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08002999 func = btrfs_freespace_write_helper;
3000 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003001 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003002 func = btrfs_endio_write_helper;
3003 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003004
Liu Bo9e0af232014-08-15 23:36:53 +08003005 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3006 NULL);
3007 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003008}
3009
Miao Xiedc380ae2014-09-12 18:43:55 +08003010static int __readpage_endio_check(struct inode *inode,
3011 struct btrfs_io_bio *io_bio,
3012 int icsum, struct page *page,
3013 int pgoff, u64 start, size_t len)
3014{
3015 char *kaddr;
3016 u32 csum_expected;
3017 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003018
3019 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3020
3021 kaddr = kmap_atomic(page);
3022 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003023 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003024 if (csum != csum_expected)
3025 goto zeroit;
3026
3027 kunmap_atomic(kaddr);
3028 return 0;
3029zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003030 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003031 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003032 memset(kaddr + pgoff, 1, len);
3033 flush_dcache_page(page);
3034 kunmap_atomic(kaddr);
3035 if (csum_expected == 0)
3036 return 0;
3037 return -EIO;
3038}
3039
Chris Masond352ac62008-09-29 15:18:18 -04003040/*
Chris Masond352ac62008-09-29 15:18:18 -04003041 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003042 * if there's a match, we allow the bio to finish. If not, the code in
3043 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003044 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003045static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3046 u64 phy_offset, struct page *page,
3047 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003048{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003049 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003050 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003051 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003052 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003053
Chris Masond20f7042008-12-08 16:58:54 -05003054 if (PageChecked(page)) {
3055 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003056 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003057 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003058
3059 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003060 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003061
Yan Zheng17d217f2008-12-12 10:03:38 -05003062 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003063 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003064 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003065 return 0;
3066 }
3067
Miao Xiefacc8a222013-07-25 19:22:34 +08003068 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003069 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3070 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003071}
Chris Masonb888db22007-08-27 16:49:44 -04003072
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003073void btrfs_add_delayed_iput(struct inode *inode)
3074{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003075 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003076 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003077
3078 if (atomic_add_unless(&inode->i_count, -1, 1))
3079 return;
3080
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003081 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003082 if (binode->delayed_iput_count == 0) {
3083 ASSERT(list_empty(&binode->delayed_iput));
3084 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3085 } else {
3086 binode->delayed_iput_count++;
3087 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003088 spin_unlock(&fs_info->delayed_iput_lock);
3089}
3090
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003091void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003092{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003093
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003094 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003095 while (!list_empty(&fs_info->delayed_iputs)) {
3096 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003097
David Sterba8089fe62015-11-19 14:15:51 +01003098 inode = list_first_entry(&fs_info->delayed_iputs,
3099 struct btrfs_inode, delayed_iput);
3100 if (inode->delayed_iput_count) {
3101 inode->delayed_iput_count--;
3102 list_move_tail(&inode->delayed_iput,
3103 &fs_info->delayed_iputs);
3104 } else {
3105 list_del_init(&inode->delayed_iput);
3106 }
3107 spin_unlock(&fs_info->delayed_iput_lock);
3108 iput(&inode->vfs_inode);
3109 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003110 }
David Sterba8089fe62015-11-19 14:15:51 +01003111 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003112}
3113
Yan, Zhengd68fc572010-05-16 10:49:58 -04003114/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003115 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003116 * files in the subvolume, it removes orphan item and frees block_rsv
3117 * structure.
3118 */
3119void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3120 struct btrfs_root *root)
3121{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003122 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003123 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003124 int ret;
3125
Josef Bacik8a35d952012-05-23 14:26:42 -04003126 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003127 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3128 return;
3129
Josef Bacik90290e12011-12-02 15:44:12 -05003130 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003131 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003132 spin_unlock(&root->orphan_lock);
3133 return;
3134 }
3135
3136 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3137 spin_unlock(&root->orphan_lock);
3138 return;
3139 }
3140
3141 block_rsv = root->orphan_block_rsv;
3142 root->orphan_block_rsv = NULL;
3143 spin_unlock(&root->orphan_lock);
3144
Miao Xie27cdeb72014-04-02 19:51:05 +08003145 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003146 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003147 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003148 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003149 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003150 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003151 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003152 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3153 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003154 }
3155
Josef Bacik90290e12011-12-02 15:44:12 -05003156 if (block_rsv) {
3157 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003158 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003159 }
3160}
3161
3162/*
Josef Bacik7b128762008-07-24 12:17:14 -04003163 * This creates an orphan entry for the given inode in case something goes
3164 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003165 *
3166 * NOTE: caller of this function should reserve 5 units of metadata for
3167 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003168 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003169int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3170 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003171{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003172 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3173 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003174 struct btrfs_block_rsv *block_rsv = NULL;
3175 int reserve = 0;
3176 int insert = 0;
3177 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003178
Yan, Zhengd68fc572010-05-16 10:49:58 -04003179 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003180 block_rsv = btrfs_alloc_block_rsv(fs_info,
3181 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003182 if (!block_rsv)
3183 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003184 }
3185
Yan, Zhengd68fc572010-05-16 10:49:58 -04003186 spin_lock(&root->orphan_lock);
3187 if (!root->orphan_block_rsv) {
3188 root->orphan_block_rsv = block_rsv;
3189 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003190 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003191 block_rsv = NULL;
3192 }
Josef Bacik7b128762008-07-24 12:17:14 -04003193
Josef Bacik8a35d952012-05-23 14:26:42 -04003194 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003195 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003196#if 0
3197 /*
3198 * For proper ENOSPC handling, we should do orphan
3199 * cleanup when mounting. But this introduces backward
3200 * compatibility issue.
3201 */
3202 if (!xchg(&root->orphan_item_inserted, 1))
3203 insert = 2;
3204 else
3205 insert = 1;
3206#endif
3207 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003208 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003209 }
Josef Bacik7b128762008-07-24 12:17:14 -04003210
Josef Bacik72ac3c02012-05-23 14:13:11 -04003211 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003212 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003213 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003214 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003215
Yan, Zhengd68fc572010-05-16 10:49:58 -04003216 /* grab metadata reservation from transaction handle */
3217 if (reserve) {
3218 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003219 ASSERT(!ret);
3220 if (ret) {
3221 atomic_dec(&root->orphan_inodes);
3222 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003223 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003224 if (insert)
3225 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003226 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003227 return ret;
3228 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003229 }
3230
3231 /* insert an orphan item to track this unlinked/truncated file */
3232 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003233 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003234 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003235 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003236 if (reserve) {
3237 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003238 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003239 btrfs_orphan_release_metadata(inode);
3240 }
3241 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003242 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003243 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003244 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003245 return ret;
3246 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003247 }
3248 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003249 }
3250
3251 /* insert an orphan item to track subvolume contains orphan files */
3252 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003253 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003254 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003255 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003256 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003257 return ret;
3258 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003259 }
3260 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003261}
3262
3263/*
3264 * We have done the truncate/delete so we can go ahead and remove the orphan
3265 * item for this particular inode.
3266 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003267static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003268 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003269{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003270 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003271 int delete_item = 0;
3272 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003273 int ret = 0;
3274
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003276 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003277 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003279
Josef Bacik72ac3c02012-05-23 14:13:11 -04003280 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003281 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003283 spin_unlock(&root->orphan_lock);
3284
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003285 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003286 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003287 if (trans)
3288 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003289 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003290 }
Josef Bacik7b128762008-07-24 12:17:14 -04003291
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003292 if (release_rsv)
3293 btrfs_orphan_release_metadata(inode);
3294
Josef Bacik4ef31a42013-08-13 14:10:08 -04003295 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003296}
3297
3298/*
3299 * this cleans up any orphans that may be left on the list from the last use
3300 * of this root.
3301 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003302int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003303{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003304 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003305 struct btrfs_path *path;
3306 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003307 struct btrfs_key key, found_key;
3308 struct btrfs_trans_handle *trans;
3309 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003310 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003311 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3312
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003314 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003315
3316 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003317 if (!path) {
3318 ret = -ENOMEM;
3319 goto out;
3320 }
David Sterbae4058b52015-11-27 16:31:35 +01003321 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003322
3323 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003324 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003325 key.offset = (u64)-1;
3326
Josef Bacik7b128762008-07-24 12:17:14 -04003327 while (1) {
3328 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003329 if (ret < 0)
3330 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003331
3332 /*
3333 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003334 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003335 * find the key and see if we have stuff that matches
3336 */
3337 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003338 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003339 if (path->slots[0] == 0)
3340 break;
3341 path->slots[0]--;
3342 }
3343
3344 /* pull out the item */
3345 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003346 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3347
3348 /* make sure the item matches what we want */
3349 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3350 break;
David Sterba962a2982014-06-04 18:41:45 +02003351 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003352 break;
3353
3354 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003355 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003356
3357 /*
3358 * this is where we are basically btrfs_lookup, without the
3359 * crossing root thing. we store the inode number in the
3360 * offset of the orphan item.
3361 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003362
3363 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003364 btrfs_err(fs_info,
3365 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003366 ret = -EINVAL;
3367 goto out;
3368 }
3369
3370 last_objectid = found_key.offset;
3371
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003372 found_key.objectid = found_key.offset;
3373 found_key.type = BTRFS_INODE_ITEM_KEY;
3374 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003375 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303376 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003377 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003378 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003379
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003380 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003381 struct btrfs_root *dead_root;
3382 struct btrfs_fs_info *fs_info = root->fs_info;
3383 int is_dead_root = 0;
3384
3385 /*
3386 * this is an orphan in the tree root. Currently these
3387 * could come from 2 sources:
3388 * a) a snapshot deletion in progress
3389 * b) a free space cache inode
3390 * We need to distinguish those two, as the snapshot
3391 * orphan must not get deleted.
3392 * find_dead_roots already ran before us, so if this
3393 * is a snapshot deletion, we should find the root
3394 * in the dead_roots list
3395 */
3396 spin_lock(&fs_info->trans_lock);
3397 list_for_each_entry(dead_root, &fs_info->dead_roots,
3398 root_list) {
3399 if (dead_root->root_key.objectid ==
3400 found_key.objectid) {
3401 is_dead_root = 1;
3402 break;
3403 }
3404 }
3405 spin_unlock(&fs_info->trans_lock);
3406 if (is_dead_root) {
3407 /* prevent this orphan from being found again */
3408 key.offset = found_key.objectid - 1;
3409 continue;
3410 }
3411 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003412 /*
3413 * Inode is already gone but the orphan item is still there,
3414 * kill the orphan item.
3415 */
Filipe Manana67710892016-06-06 11:51:25 +01003416 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003417 trans = btrfs_start_transaction(root, 1);
3418 if (IS_ERR(trans)) {
3419 ret = PTR_ERR(trans);
3420 goto out;
3421 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003422 btrfs_debug(fs_info, "auto deleting %Lu",
3423 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003424 ret = btrfs_del_orphan_item(trans, root,
3425 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003426 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003427 if (ret)
3428 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003429 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003430 }
Josef Bacik7b128762008-07-24 12:17:14 -04003431
Josef Bacik7b128762008-07-24 12:17:14 -04003432 /*
3433 * add this inode to the orphan list so btrfs_orphan_del does
3434 * the proper thing when we hit it
3435 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003436 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3437 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003438 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003439
Josef Bacik7b128762008-07-24 12:17:14 -04003440 /* if we have links, this was a truncate, lets do that */
3441 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303442 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003443 iput(inode);
3444 continue;
3445 }
Josef Bacik7b128762008-07-24 12:17:14 -04003446 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003447
3448 /* 1 for the orphan item deletion. */
3449 trans = btrfs_start_transaction(root, 1);
3450 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003451 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003452 ret = PTR_ERR(trans);
3453 goto out;
3454 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003455 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003456 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003457 if (ret) {
3458 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003459 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003460 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003461
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003462 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003463 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003464 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003465 } else {
3466 nr_unlink++;
3467 }
3468
3469 /* this will do delete_inode and everything for us */
3470 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003471 if (ret)
3472 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003473 }
Miao Xie3254c872011-11-10 20:45:05 -05003474 /* release the path since we're done with it */
3475 btrfs_release_path(path);
3476
Yan, Zhengd68fc572010-05-16 10:49:58 -04003477 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3478
3479 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003480 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003481 (u64)-1);
3482
Miao Xie27cdeb72014-04-02 19:51:05 +08003483 if (root->orphan_block_rsv ||
3484 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003485 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003486 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003487 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003488 }
Josef Bacik7b128762008-07-24 12:17:14 -04003489
3490 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003491 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003492 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003493 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003494
3495out:
3496 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003497 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003498 btrfs_free_path(path);
3499 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003500}
3501
Chris Masond352ac62008-09-29 15:18:18 -04003502/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003503 * very simple check to peek ahead in the leaf looking for xattrs. If we
3504 * don't find any xattrs, we know there can't be any acls.
3505 *
3506 * slot is the slot the inode is in, objectid is the objectid of the inode
3507 */
3508static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003509 int slot, u64 objectid,
3510 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003511{
3512 u32 nritems = btrfs_header_nritems(leaf);
3513 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003514 static u64 xattr_access = 0;
3515 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003516 int scanned = 0;
3517
Josef Bacikf23b5a52013-06-19 10:16:26 -04003518 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003519 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3520 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3521 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3522 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003523 }
3524
Chris Mason46a53cc2009-04-27 11:47:50 -04003525 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003526 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003527 while (slot < nritems) {
3528 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3529
3530 /* we found a different objectid, there must not be acls */
3531 if (found_key.objectid != objectid)
3532 return 0;
3533
3534 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003535 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003536 if (*first_xattr_slot == -1)
3537 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003538 if (found_key.offset == xattr_access ||
3539 found_key.offset == xattr_default)
3540 return 1;
3541 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003542
3543 /*
3544 * we found a key greater than an xattr key, there can't
3545 * be any acls later on
3546 */
3547 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3548 return 0;
3549
3550 slot++;
3551 scanned++;
3552
3553 /*
3554 * it goes inode, inode backrefs, xattrs, extents,
3555 * so if there are a ton of hard links to an inode there can
3556 * be a lot of backrefs. Don't waste time searching too hard,
3557 * this is just an optimization
3558 */
3559 if (scanned >= 8)
3560 break;
3561 }
3562 /* we hit the end of the leaf before we found an xattr or
3563 * something larger than an xattr. We have to assume the inode
3564 * has acls
3565 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003566 if (*first_xattr_slot == -1)
3567 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003568 return 1;
3569}
3570
3571/*
Chris Masond352ac62008-09-29 15:18:18 -04003572 * read an inode from the btree into the in-memory inode
3573 */
Filipe Manana67710892016-06-06 11:51:25 +01003574static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003575{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003576 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003577 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003578 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003579 struct btrfs_inode_item *inode_item;
3580 struct btrfs_root *root = BTRFS_I(inode)->root;
3581 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003582 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003583 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003584 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003585 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003586 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003587 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003588
3589 ret = btrfs_fill_inode(inode, &rdev);
3590 if (!ret)
3591 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003592
3593 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003594 if (!path) {
3595 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003596 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003597 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003598
Chris Mason39279cc2007-06-12 06:35:45 -04003599 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003600
Chris Mason39279cc2007-06-12 06:35:45 -04003601 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003602 if (ret) {
3603 if (ret > 0)
3604 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003605 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003606 }
Chris Mason39279cc2007-06-12 06:35:45 -04003607
Chris Mason5f39d392007-10-15 16:14:19 -04003608 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003609
3610 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003611 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003612
Chris Mason5f39d392007-10-15 16:14:19 -04003613 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3614 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003615 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003616 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003617 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3618 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003619 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003620
David Sterbaa937b972014-12-12 17:39:12 +01003621 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3622 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003623
David Sterbaa937b972014-12-12 17:39:12 +01003624 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3625 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003626
David Sterbaa937b972014-12-12 17:39:12 +01003627 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3628 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003629
chandan r9cc97d62012-07-04 12:48:07 +05303630 BTRFS_I(inode)->i_otime.tv_sec =
3631 btrfs_timespec_sec(leaf, &inode_item->otime);
3632 BTRFS_I(inode)->i_otime.tv_nsec =
3633 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003634
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003635 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003636 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003637 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3638
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003639 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003640 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003641 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003642 rdev = btrfs_inode_rdev(leaf, inode_item);
3643
Josef Bacikaec74772008-07-24 12:12:38 -04003644 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003645 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003646
3647cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003648 /*
3649 * If we were modified in the current generation and evicted from memory
3650 * and then re-read we need to do a full sync since we don't have any
3651 * idea about which extents were modified before we were evicted from
3652 * cache.
3653 *
3654 * This is required for both inode re-read from disk and delayed inode
3655 * in delayed_nodes_tree.
3656 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003657 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003658 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3659 &BTRFS_I(inode)->runtime_flags);
3660
Filipe Mananabde6c242015-07-24 00:00:19 +01003661 /*
3662 * We don't persist the id of the transaction where an unlink operation
3663 * against the inode was last made. So here we assume the inode might
3664 * have been evicted, and therefore the exact value of last_unlink_trans
3665 * lost, and set it to last_trans to avoid metadata inconsistencies
3666 * between the inode and its parent if the inode is fsync'ed and the log
3667 * replayed. For example, in the scenario:
3668 *
3669 * touch mydir/foo
3670 * ln mydir/foo mydir/bar
3671 * sync
3672 * unlink mydir/bar
3673 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3674 * xfs_io -c fsync mydir/foo
3675 * <power failure>
3676 * mount fs, triggers fsync log replay
3677 *
3678 * We must make sure that when we fsync our inode foo we also log its
3679 * parent inode, otherwise after log replay the parent still has the
3680 * dentry with the "bar" name but our inode foo has a link count of 1
3681 * and doesn't have an inode ref with the name "bar" anymore.
3682 *
3683 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003684 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003685 * transaction commits on fsync if our inode is a directory, or if our
3686 * inode is not a directory, logging its parent unnecessarily.
3687 */
3688 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3689
Miao Xie67de1172013-12-26 13:07:06 +08003690 path->slots[0]++;
3691 if (inode->i_nlink != 1 ||
3692 path->slots[0] >= btrfs_header_nritems(leaf))
3693 goto cache_acl;
3694
3695 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003696 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003697 goto cache_acl;
3698
3699 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3700 if (location.type == BTRFS_INODE_REF_KEY) {
3701 struct btrfs_inode_ref *ref;
3702
3703 ref = (struct btrfs_inode_ref *)ptr;
3704 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3705 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3706 struct btrfs_inode_extref *extref;
3707
3708 extref = (struct btrfs_inode_extref *)ptr;
3709 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3710 extref);
3711 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003712cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003713 /*
3714 * try to precache a NULL acl entry for files that don't have
3715 * any xattrs or acls
3716 */
Li Zefan33345d012011-04-20 10:31:50 +08003717 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003718 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003719 if (first_xattr_slot != -1) {
3720 path->slots[0] = first_xattr_slot;
3721 ret = btrfs_load_inode_props(inode, path);
3722 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003723 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003724 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003725 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003726 root->root_key.objectid, ret);
3727 }
3728 btrfs_free_path(path);
3729
Al Viro72c04902009-06-24 16:58:48 -04003730 if (!maybe_acls)
3731 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003732
Chris Mason39279cc2007-06-12 06:35:45 -04003733 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003734 case S_IFREG:
3735 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003736 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003737 inode->i_fop = &btrfs_file_operations;
3738 inode->i_op = &btrfs_file_inode_operations;
3739 break;
3740 case S_IFDIR:
3741 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003742 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003743 break;
3744 case S_IFLNK:
3745 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003746 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003747 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3748 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003749 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003750 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003751 init_special_inode(inode, inode->i_mode, rdev);
3752 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003753 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003754
3755 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003756 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003757
3758make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003759 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003760 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003761 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003762}
3763
Chris Masond352ac62008-09-29 15:18:18 -04003764/*
3765 * given a leaf and an inode, copy the inode fields into the leaf
3766 */
Chris Masone02119d2008-09-05 16:13:11 -04003767static void fill_inode_item(struct btrfs_trans_handle *trans,
3768 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003769 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003770 struct inode *inode)
3771{
Liu Bo51fab692012-12-27 09:01:21 +00003772 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003773
Liu Bo51fab692012-12-27 09:01:21 +00003774 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003775
Liu Bo51fab692012-12-27 09:01:21 +00003776 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3777 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3778 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3779 &token);
3780 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3781 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003782
David Sterbaa937b972014-12-12 17:39:12 +01003783 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003784 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003785 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003786 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003787
David Sterbaa937b972014-12-12 17:39:12 +01003788 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003789 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003790 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003791 inode->i_mtime.tv_nsec, &token);
3792
David Sterbaa937b972014-12-12 17:39:12 +01003793 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003794 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003795 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003796 inode->i_ctime.tv_nsec, &token);
3797
chandan r9cc97d62012-07-04 12:48:07 +05303798 btrfs_set_token_timespec_sec(leaf, &item->otime,
3799 BTRFS_I(inode)->i_otime.tv_sec, &token);
3800 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3801 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3802
Liu Bo51fab692012-12-27 09:01:21 +00003803 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3804 &token);
3805 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3806 &token);
3807 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3808 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3809 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3810 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3811 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003812}
3813
Chris Masond352ac62008-09-29 15:18:18 -04003814/*
3815 * copy everything in the in-memory inode into the btree.
3816 */
Chris Mason21151332011-11-10 20:39:08 -05003817static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003818 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003819{
3820 struct btrfs_inode_item *inode_item;
3821 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003822 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003823 int ret;
3824
3825 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003826 if (!path)
3827 return -ENOMEM;
3828
Chris Masonb9473432009-03-13 11:00:37 -04003829 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003830 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3831 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003832 if (ret) {
3833 if (ret > 0)
3834 ret = -ENOENT;
3835 goto failed;
3836 }
3837
Chris Mason5f39d392007-10-15 16:14:19 -04003838 leaf = path->nodes[0];
3839 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003840 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003841
Chris Masone02119d2008-09-05 16:13:11 -04003842 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003843 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003844 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003845 ret = 0;
3846failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003847 btrfs_free_path(path);
3848 return ret;
3849}
3850
Chris Masond352ac62008-09-29 15:18:18 -04003851/*
Chris Mason21151332011-11-10 20:39:08 -05003852 * copy everything in the in-memory inode into the btree.
3853 */
3854noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3855 struct btrfs_root *root, struct inode *inode)
3856{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003857 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003858 int ret;
3859
3860 /*
3861 * If the inode is a free space inode, we can deadlock during commit
3862 * if we put it into the delayed code.
3863 *
3864 * The data relocation inode should also be directly updated
3865 * without delay
3866 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003867 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003868 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003869 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003870 btrfs_update_root_times(trans, root);
3871
Chris Mason21151332011-11-10 20:39:08 -05003872 ret = btrfs_delayed_update_inode(trans, root, inode);
3873 if (!ret)
3874 btrfs_set_inode_last_trans(trans, inode);
3875 return ret;
3876 }
3877
3878 return btrfs_update_inode_item(trans, root, inode);
3879}
3880
Josef Bacikbe6aef62012-10-22 15:43:12 -04003881noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3882 struct btrfs_root *root,
3883 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003884{
3885 int ret;
3886
3887 ret = btrfs_update_inode(trans, root, inode);
3888 if (ret == -ENOSPC)
3889 return btrfs_update_inode_item(trans, root, inode);
3890 return ret;
3891}
3892
3893/*
Chris Masond352ac62008-09-29 15:18:18 -04003894 * unlink helper that gets used here in inode.c and in the tree logging
3895 * recovery code. It remove a link in a directory with a given name, and
3896 * also drops the back refs in the inode to the directory
3897 */
Al Viro92986792011-03-04 17:14:37 +00003898static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3899 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003900 struct btrfs_inode *dir,
3901 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003902 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003903{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003904 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003905 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003906 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003907 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003909 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003910 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003911 u64 ino = btrfs_ino(inode);
3912 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003913
3914 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003915 if (!path) {
3916 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003917 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003918 }
3919
Chris Masonb9473432009-03-13 11:00:37 -04003920 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003921 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003922 name, name_len, -1);
3923 if (IS_ERR(di)) {
3924 ret = PTR_ERR(di);
3925 goto err;
3926 }
3927 if (!di) {
3928 ret = -ENOENT;
3929 goto err;
3930 }
Chris Mason5f39d392007-10-15 16:14:19 -04003931 leaf = path->nodes[0];
3932 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003933 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003934 if (ret)
3935 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003936 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003937
Miao Xie67de1172013-12-26 13:07:06 +08003938 /*
3939 * If we don't have dir index, we have to get it by looking up
3940 * the inode ref, since we get the inode ref, remove it directly,
3941 * it is unnecessary to do delayed deletion.
3942 *
3943 * But if we have dir index, needn't search inode ref to get it.
3944 * Since the inode ref is close to the inode item, it is better
3945 * that we delay to delete it, and just do this deletion when
3946 * we update the inode item.
3947 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003948 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003949 ret = btrfs_delayed_delete_inode_ref(inode);
3950 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003951 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003952 goto skip_backref;
3953 }
3954 }
3955
Li Zefan33345d012011-04-20 10:31:50 +08003956 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3957 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003958 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003959 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003960 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003961 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003962 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003963 goto err;
3964 }
Miao Xie67de1172013-12-26 13:07:06 +08003965skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003966 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003967 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003968 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003969 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003970 }
Chris Mason39279cc2007-06-12 06:35:45 -04003971
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003972 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3973 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003974 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003975 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003976 goto err;
3977 }
Chris Masone02119d2008-09-05 16:13:11 -04003978
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003979 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3980 index);
Chris Mason6418c962010-10-30 07:34:24 -04003981 if (ret == -ENOENT)
3982 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003983 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003984 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003985err:
3986 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003987 if (ret)
3988 goto out;
3989
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003990 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003991 inode_inc_iversion(&inode->vfs_inode);
3992 inode_inc_iversion(&dir->vfs_inode);
3993 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3994 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3995 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003996out:
Chris Mason39279cc2007-06-12 06:35:45 -04003997 return ret;
3998}
3999
Al Viro92986792011-03-04 17:14:37 +00004000int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4001 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004002 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004003 const char *name, int name_len)
4004{
4005 int ret;
4006 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4007 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004008 drop_nlink(&inode->vfs_inode);
4009 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004010 }
4011 return ret;
4012}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004013
4014/*
4015 * helper to start transaction for unlink and rmdir.
4016 *
Josef Bacikd52be812013-05-29 14:54:47 -04004017 * unlink and rmdir are special in btrfs, they do not always free space, so
4018 * if we cannot make our reservations the normal way try and see if there is
4019 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4020 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004021 */
Josef Bacikd52be812013-05-29 14:54:47 -04004022static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004023{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004025
Josef Bacike70bea52011-10-11 14:18:24 -04004026 /*
4027 * 1 for the possible orphan item
4028 * 1 for the dir item
4029 * 1 for the dir index
4030 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004031 * 1 for the inode
4032 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004033 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004034}
4035
Chris Mason39279cc2007-06-12 06:35:45 -04004036static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4037{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004038 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004039 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004040 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004041 int ret;
4042
Josef Bacikd52be812013-05-29 14:54:47 -04004043 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004044 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004045 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004046
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004047 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4048 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004049
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004050 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4051 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4052 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004053 if (ret)
4054 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004055
Yan, Zhenga22285a2010-05-16 10:48:46 -04004056 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004057 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004058 if (ret)
4059 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004060 }
Josef Bacik7b128762008-07-24 12:17:14 -04004061
Tsutomu Itohb5324022011-07-19 07:27:20 +00004062out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004063 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004064 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004065 return ret;
4066}
4067
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004068int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4069 struct btrfs_root *root,
4070 struct inode *dir, u64 objectid,
4071 const char *name, int name_len)
4072{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004073 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004074 struct btrfs_path *path;
4075 struct extent_buffer *leaf;
4076 struct btrfs_dir_item *di;
4077 struct btrfs_key key;
4078 u64 index;
4079 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004080 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004081
4082 path = btrfs_alloc_path();
4083 if (!path)
4084 return -ENOMEM;
4085
Li Zefan33345d012011-04-20 10:31:50 +08004086 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004087 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004088 if (IS_ERR_OR_NULL(di)) {
4089 if (!di)
4090 ret = -ENOENT;
4091 else
4092 ret = PTR_ERR(di);
4093 goto out;
4094 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004095
4096 leaf = path->nodes[0];
4097 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4098 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4099 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004100 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004101 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004102 goto out;
4103 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004104 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004105
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004106 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4107 root->root_key.objectid, dir_ino,
4108 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004109 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004110 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004111 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004112 goto out;
4113 }
Li Zefan33345d012011-04-20 10:31:50 +08004114 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116 if (IS_ERR_OR_NULL(di)) {
4117 if (!di)
4118 ret = -ENOENT;
4119 else
4120 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004121 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004122 goto out;
4123 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004124
4125 leaf = path->nodes[0];
4126 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004127 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128 index = key.offset;
4129 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004130 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004131
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004132 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004133 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004134 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 goto out;
4136 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004137
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004138 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004139 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004140 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004141 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004142 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004143 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004144out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004145 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004146 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004147}
4148
Chris Mason39279cc2007-06-12 06:35:45 -04004149static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4150{
David Howells2b0143b2015-03-17 22:25:59 +00004151 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004152 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004153 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004154 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004155 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004156
David Sterbab3ae2442012-09-13 16:04:34 -06004157 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004158 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004159 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004160 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004161
Josef Bacikd52be812013-05-29 14:54:47 -04004162 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004163 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004164 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004165
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004166 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004167 err = btrfs_unlink_subvol(trans, root, dir,
4168 BTRFS_I(inode)->location.objectid,
4169 dentry->d_name.name,
4170 dentry->d_name.len);
4171 goto out;
4172 }
4173
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004174 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004175 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004176 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004177
Filipe Manana44f714d2016-06-06 16:11:13 +01004178 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4179
Chris Mason39279cc2007-06-12 06:35:45 -04004180 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004181 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4182 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4183 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004184 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004185 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004186 /*
4187 * Propagate the last_unlink_trans value of the deleted dir to
4188 * its parent directory. This is to prevent an unrecoverable
4189 * log tree in the case we do something like this:
4190 * 1) create dir foo
4191 * 2) create snapshot under dir foo
4192 * 3) delete the snapshot
4193 * 4) rmdir foo
4194 * 5) mkdir foo
4195 * 6) fsync foo or some file inside foo
4196 */
4197 if (last_unlink_trans >= trans->transid)
4198 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4199 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004200out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004201 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004202 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004203
Chris Mason39279cc2007-06-12 06:35:45 -04004204 return err;
4205}
4206
Chris Mason28f75a02015-02-04 06:59:29 -08004207static int truncate_space_check(struct btrfs_trans_handle *trans,
4208 struct btrfs_root *root,
4209 u64 bytes_deleted)
4210{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004211 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004212 int ret;
4213
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004214 /*
4215 * This is only used to apply pressure to the enospc system, we don't
4216 * intend to use this reservation at all.
4217 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004218 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004219 bytes_deleted *= fs_info->nodesize;
4220 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004221 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004222 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004223 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004224 trans->transid,
4225 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004226 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004227 }
Chris Mason28f75a02015-02-04 06:59:29 -08004228 return ret;
4229
4230}
4231
Filipe Manana0305cd52015-10-16 12:34:25 +01004232static int truncate_inline_extent(struct inode *inode,
4233 struct btrfs_path *path,
4234 struct btrfs_key *found_key,
4235 const u64 item_end,
4236 const u64 new_size)
4237{
4238 struct extent_buffer *leaf = path->nodes[0];
4239 int slot = path->slots[0];
4240 struct btrfs_file_extent_item *fi;
4241 u32 size = (u32)(new_size - found_key->offset);
4242 struct btrfs_root *root = BTRFS_I(inode)->root;
4243
4244 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4245
4246 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4247 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004248 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004249
4250 /*
4251 * Zero out the remaining of the last page of our inline extent,
4252 * instead of directly truncating our inline extent here - that
4253 * would be much more complex (decompressing all the data, then
4254 * compressing the truncated data, which might be bigger than
4255 * the size of the inline extent, resize the extent, etc).
4256 * We release the path because to get the page we might need to
4257 * read the extent item from disk (data not in the page cache).
4258 */
4259 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304260 return btrfs_truncate_block(inode, offset, page_end - offset,
4261 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004262 }
4263
4264 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4265 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004266 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004267
4268 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4269 inode_sub_bytes(inode, item_end + 1 - new_size);
4270
4271 return 0;
4272}
4273
Chris Mason323ac952008-10-01 19:05:46 -04004274/*
Chris Mason39279cc2007-06-12 06:35:45 -04004275 * this can truncate away extent items, csum items and directory items.
4276 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004277 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004278 *
4279 * csum items that cross the new i_size are truncated to the new size
4280 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004281 *
4282 * min_type is the minimum key type to truncate down to. If set to 0, this
4283 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004284 */
Yan, Zheng80825102009-11-12 09:35:36 +00004285int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4286 struct btrfs_root *root,
4287 struct inode *inode,
4288 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004289{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004290 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004291 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004292 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004293 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004294 struct btrfs_key key;
4295 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004296 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004297 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004298 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004299 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004300 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004301 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004302 int found_extent;
4303 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004304 int pending_del_nr = 0;
4305 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004306 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004307 int ret;
4308 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004309 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004310 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004311 bool be_nice = 0;
4312 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004313 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004314
4315 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004316
Chris Mason28ed1342014-12-17 09:41:04 -08004317 /*
4318 * for non-free space inodes and ref cows, we want to back off from
4319 * time to time
4320 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004321 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004322 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4323 be_nice = 1;
4324
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004325 path = btrfs_alloc_path();
4326 if (!path)
4327 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004328 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004329
Josef Bacik5dc562c2012-08-17 13:14:17 -04004330 /*
4331 * We want to drop from the next block forward in case this new size is
4332 * not block aligned since we will be keeping the last block of the
4333 * extent just the way it is.
4334 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004335 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004336 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004337 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004338 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004339 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004340
Miao Xie16cdcec2011-04-22 18:12:22 +08004341 /*
4342 * This function is also used to drop the items in the log tree before
4343 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4344 * it is used to drop the loged items. So we shouldn't kill the delayed
4345 * items.
4346 */
4347 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004348 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004349
Li Zefan33345d012011-04-20 10:31:50 +08004350 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004351 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004352 key.type = (u8)-1;
4353
Chris Mason85e21ba2008-01-29 15:11:36 -05004354search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004355 /*
4356 * with a 16K leaf size and 128MB extents, you can actually queue
4357 * up a huge file in a single leaf. Most of the time that
4358 * bytes_deleted is > 0, it will be huge by the time we get here
4359 */
Byongho Leeee221842015-12-15 01:42:10 +09004360 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004361 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004362 err = -EAGAIN;
4363 goto error;
4364 }
4365 }
4366
4367
Chris Masonb9473432009-03-13 11:00:37 -04004368 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004369 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004370 if (ret < 0) {
4371 err = ret;
4372 goto out;
4373 }
Chris Masond3977122009-01-05 21:25:51 -05004374
Chris Mason85e21ba2008-01-29 15:11:36 -05004375 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004376 /* there are no items in the tree for us to truncate, we're
4377 * done
4378 */
Yan, Zheng80825102009-11-12 09:35:36 +00004379 if (path->slots[0] == 0)
4380 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004381 path->slots[0]--;
4382 }
4383
Chris Masond3977122009-01-05 21:25:51 -05004384 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004385 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004386 leaf = path->nodes[0];
4387 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004388 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004389
Li Zefan33345d012011-04-20 10:31:50 +08004390 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004391 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004392
Chris Mason85e21ba2008-01-29 15:11:36 -05004393 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004394 break;
4395
Chris Mason5f39d392007-10-15 16:14:19 -04004396 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004397 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004398 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004399 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004400 extent_type = btrfs_file_extent_type(leaf, fi);
4401 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004402 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004403 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004404 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004405 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004406 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004407 }
Yan008630c2007-11-07 13:31:09 -05004408 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004409 }
Yan, Zheng80825102009-11-12 09:35:36 +00004410 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004411 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004412 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004413 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004414 break;
4415 if (found_key.offset >= new_size)
4416 del_item = 1;
4417 else
4418 del_item = 0;
4419 }
Chris Mason39279cc2007-06-12 06:35:45 -04004420 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004421 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004422 if (found_type != BTRFS_EXTENT_DATA_KEY)
4423 goto delete;
4424
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004425 if (del_item)
4426 last_size = found_key.offset;
4427 else
4428 last_size = new_size;
4429
Chris Mason179e29e2007-11-01 11:28:41 -04004430 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004431 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004432 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004433 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004434 u64 orig_num_bytes =
4435 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004436 extent_num_bytes = ALIGN(new_size -
4437 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004438 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004439 btrfs_set_file_extent_num_bytes(leaf, fi,
4440 extent_num_bytes);
4441 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004442 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004443 if (test_bit(BTRFS_ROOT_REF_COWS,
4444 &root->state) &&
4445 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004446 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004447 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004448 } else {
Chris Masondb945352007-10-15 16:15:53 -04004449 extent_num_bytes =
4450 btrfs_file_extent_disk_num_bytes(leaf,
4451 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004452 extent_offset = found_key.offset -
4453 btrfs_file_extent_offset(leaf, fi);
4454
Chris Mason39279cc2007-06-12 06:35:45 -04004455 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004456 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004457 if (extent_start != 0) {
4458 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004459 if (test_bit(BTRFS_ROOT_REF_COWS,
4460 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004461 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004462 }
4463 }
Chris Mason90692182008-02-08 13:49:28 -05004464 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004465 /*
4466 * we can't truncate inline items that have had
4467 * special encodings
4468 */
4469 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004470 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4471 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004472
4473 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004474 * Need to release path in order to truncate a
4475 * compressed extent. So delete any accumulated
4476 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004477 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004478 if (btrfs_file_extent_compression(leaf, fi) !=
4479 BTRFS_COMPRESS_NONE && pending_del_nr) {
4480 err = btrfs_del_items(trans, root, path,
4481 pending_del_slot,
4482 pending_del_nr);
4483 if (err) {
4484 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004485 err);
4486 goto error;
4487 }
4488 pending_del_nr = 0;
4489 }
4490
4491 err = truncate_inline_extent(inode, path,
4492 &found_key,
4493 item_end,
4494 new_size);
4495 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004496 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004497 goto error;
4498 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004499 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4500 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004501 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004502 }
Chris Mason39279cc2007-06-12 06:35:45 -04004503 }
Chris Mason179e29e2007-11-01 11:28:41 -04004504delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004505 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004506 if (!pending_del_nr) {
4507 /* no pending yet, add ourselves */
4508 pending_del_slot = path->slots[0];
4509 pending_del_nr = 1;
4510 } else if (pending_del_nr &&
4511 path->slots[0] + 1 == pending_del_slot) {
4512 /* hop on the pending chunk */
4513 pending_del_nr++;
4514 pending_del_slot = path->slots[0];
4515 } else {
Chris Masond3977122009-01-05 21:25:51 -05004516 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004517 }
Chris Mason39279cc2007-06-12 06:35:45 -04004518 } else {
4519 break;
4520 }
Chris Mason28f75a02015-02-04 06:59:29 -08004521 should_throttle = 0;
4522
Miao Xie27cdeb72014-04-02 19:51:05 +08004523 if (found_extent &&
4524 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004525 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004526 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004527 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004528 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004529 extent_num_bytes, 0,
4530 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004531 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004532 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004533 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4534 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004535 trans->delayed_ref_updates * 2,
4536 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004537 if (be_nice) {
4538 if (truncate_space_check(trans, root,
4539 extent_num_bytes)) {
4540 should_end = 1;
4541 }
4542 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004543 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004544 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004545 }
Chris Mason39279cc2007-06-12 06:35:45 -04004546 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004547
Yan, Zheng80825102009-11-12 09:35:36 +00004548 if (found_type == BTRFS_INODE_ITEM_KEY)
4549 break;
4550
4551 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004552 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004553 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004554 if (pending_del_nr) {
4555 ret = btrfs_del_items(trans, root, path,
4556 pending_del_slot,
4557 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004558 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004559 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004560 goto error;
4561 }
Yan, Zheng80825102009-11-12 09:35:36 +00004562 pending_del_nr = 0;
4563 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004564 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004565 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004566 unsigned long updates = trans->delayed_ref_updates;
4567 if (updates) {
4568 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004569 ret = btrfs_run_delayed_refs(trans,
4570 fs_info,
4571 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004572 if (ret && !err)
4573 err = ret;
4574 }
4575 }
Chris Mason28f75a02015-02-04 06:59:29 -08004576 /*
4577 * if we failed to refill our space rsv, bail out
4578 * and let the transaction restart
4579 */
4580 if (should_end) {
4581 err = -EAGAIN;
4582 goto error;
4583 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004584 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004585 } else {
4586 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004587 }
Chris Mason39279cc2007-06-12 06:35:45 -04004588 }
Yan, Zheng80825102009-11-12 09:35:36 +00004589out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004590 if (pending_del_nr) {
4591 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4592 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004593 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004594 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004595 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004596error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004597 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4598 ASSERT(last_size >= new_size);
4599 if (!err && last_size > new_size)
4600 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004601 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004602 }
Chris Mason28ed1342014-12-17 09:41:04 -08004603
Chris Mason39279cc2007-06-12 06:35:45 -04004604 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004605
Liu Bo19fd2df2016-12-01 13:01:02 -08004606 if (err == 0) {
4607 /* only inline file may have last_size != new_size */
4608 if (new_size >= fs_info->sectorsize ||
4609 new_size > fs_info->max_inline)
4610 ASSERT(last_size == new_size);
4611 }
4612
Byongho Leeee221842015-12-15 01:42:10 +09004613 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004614 unsigned long updates = trans->delayed_ref_updates;
4615 if (updates) {
4616 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004617 ret = btrfs_run_delayed_refs(trans, fs_info,
4618 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004619 if (ret && !err)
4620 err = ret;
4621 }
4622 }
Yan, Zheng80825102009-11-12 09:35:36 +00004623 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004624}
4625
Chris Masona52d9a82007-08-27 16:49:44 -04004626/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304627 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004628 * @inode - inode that we're zeroing
4629 * @from - the offset to start zeroing
4630 * @len - the length to zero, 0 to zero the entire range respective to the
4631 * offset
4632 * @front - zero up to the offset instead of from the offset on
4633 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304634 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004635 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004636 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304637int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004638 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004639{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004640 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004641 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004642 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4643 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004644 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004645 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004646 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004647 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304648 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004649 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004650 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004651 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304652 u64 block_start;
4653 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004654
Josef Bacik2aaa6652012-08-29 14:27:18 -04004655 if ((offset & (blocksize - 1)) == 0 &&
4656 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004657 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304658
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004659 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304660 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004661 if (ret)
4662 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004663
Chris Mason211c17f2008-05-15 09:13:45 -04004664again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004665 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004666 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004667 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304668 round_down(from, blocksize),
4669 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004670 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004671 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004672 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004673
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304674 block_start = round_down(from, blocksize);
4675 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004676
Chris Masona52d9a82007-08-27 16:49:44 -04004677 if (!PageUptodate(page)) {
4678 ret = btrfs_readpage(NULL, page);
4679 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004680 if (page->mapping != mapping) {
4681 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004682 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004683 goto again;
4684 }
Chris Masona52d9a82007-08-27 16:49:44 -04004685 if (!PageUptodate(page)) {
4686 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004687 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004688 }
4689 }
Chris Mason211c17f2008-05-15 09:13:45 -04004690 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004691
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304692 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004693 set_page_extent_mapped(page);
4694
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304695 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004696 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304697 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004698 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004699 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004700 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004701 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004702 btrfs_put_ordered_extent(ordered);
4703 goto again;
4704 }
4705
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304706 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004707 EXTENT_DIRTY | EXTENT_DELALLOC |
4708 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004709 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004710
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304711 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004712 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004713 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304714 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004715 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004716 goto out_unlock;
4717 }
4718
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304719 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004720 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304721 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004722 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004723 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304724 memset(kaddr + (block_start - page_offset(page)),
4725 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004726 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304727 memset(kaddr + (block_start - page_offset(page)) + offset,
4728 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004729 flush_dcache_page(page);
4730 kunmap(page);
4731 }
Chris Mason247e7432008-07-17 12:53:51 -04004732 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004733 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304734 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004735 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004736
Chris Mason89642222008-07-24 09:41:53 -04004737out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004738 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304739 btrfs_delalloc_release_space(inode, block_start,
4740 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004741 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004742 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004743out:
4744 return ret;
4745}
4746
Josef Bacik16e75492013-10-22 12:18:51 -04004747static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4748 u64 offset, u64 len)
4749{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004750 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004751 struct btrfs_trans_handle *trans;
4752 int ret;
4753
4754 /*
4755 * Still need to make sure the inode looks like it's been updated so
4756 * that any holes get logged if we fsync.
4757 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004758 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4759 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004760 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4761 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4762 return 0;
4763 }
4764
4765 /*
4766 * 1 - for the one we're dropping
4767 * 1 - for the one we're adding
4768 * 1 - for updating the inode.
4769 */
4770 trans = btrfs_start_transaction(root, 3);
4771 if (IS_ERR(trans))
4772 return PTR_ERR(trans);
4773
4774 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4775 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004776 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004777 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004778 return ret;
4779 }
4780
David Sterbaf85b7372017-01-20 14:54:07 +01004781 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4782 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004783 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004784 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004785 else
4786 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004787 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004788 return ret;
4789}
4790
Josef Bacik695a0d02011-03-04 15:46:53 -05004791/*
4792 * This function puts in dummy file extents for the area we're creating a hole
4793 * for. So if we are truncating this file to a larger size we need to insert
4794 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4795 * the range between oldsize and size
4796 */
Josef Bacika41ad392011-01-31 15:30:16 -05004797int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004798{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004799 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004800 struct btrfs_root *root = BTRFS_I(inode)->root;
4801 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004802 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004803 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004804 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004805 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4806 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004807 u64 last_byte;
4808 u64 cur_offset;
4809 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004810 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004811
Josef Bacika71754f2013-06-17 17:14:39 -04004812 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304813 * If our size started in the middle of a block we need to zero out the
4814 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004815 * expose stale data.
4816 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304817 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004818 if (err)
4819 return err;
4820
Yan Zheng9036c102008-10-30 14:19:41 -04004821 if (size <= hole_start)
4822 return 0;
4823
Yan Zheng9036c102008-10-30 14:19:41 -04004824 while (1) {
4825 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004826
David Sterbaff13db42015-12-03 14:30:40 +01004827 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004828 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004829 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004830 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004831 if (!ordered)
4832 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004833 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4834 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004835 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004836 btrfs_put_ordered_extent(ordered);
4837 }
4838
Yan Zheng9036c102008-10-30 14:19:41 -04004839 cur_offset = hole_start;
4840 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004841 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004842 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004843 if (IS_ERR(em)) {
4844 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004845 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004846 break;
4847 }
Yan Zheng9036c102008-10-30 14:19:41 -04004848 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004849 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004850 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004851 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004852 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004853
Josef Bacik16e75492013-10-22 12:18:51 -04004854 err = maybe_insert_hole(root, inode, cur_offset,
4855 hole_size);
4856 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004857 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004858 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004859 cur_offset + hole_size - 1, 0);
4860 hole_em = alloc_extent_map();
4861 if (!hole_em) {
4862 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4863 &BTRFS_I(inode)->runtime_flags);
4864 goto next;
4865 }
4866 hole_em->start = cur_offset;
4867 hole_em->len = hole_size;
4868 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004869
Josef Bacik5dc562c2012-08-17 13:14:17 -04004870 hole_em->block_start = EXTENT_MAP_HOLE;
4871 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004872 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004873 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004874 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004875 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004876 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004877
4878 while (1) {
4879 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004880 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004881 write_unlock(&em_tree->lock);
4882 if (err != -EEXIST)
4883 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004884 btrfs_drop_extent_cache(BTRFS_I(inode),
4885 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004886 cur_offset +
4887 hole_size - 1, 0);
4888 }
4889 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004890 }
Josef Bacik16e75492013-10-22 12:18:51 -04004891next:
Yan Zheng9036c102008-10-30 14:19:41 -04004892 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004893 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004894 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004895 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004896 break;
4897 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004898 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004899 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4900 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004901 return err;
4902}
4903
Eric Sandeen3972f262013-01-12 02:57:22 +00004904static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004905{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004906 struct btrfs_root *root = BTRFS_I(inode)->root;
4907 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004908 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004909 loff_t newsize = attr->ia_size;
4910 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004911 int ret;
4912
Eric Sandeen3972f262013-01-12 02:57:22 +00004913 /*
4914 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4915 * special case where we need to update the times despite not having
4916 * these flags set. For all other operations the VFS set these flags
4917 * explicitly if it wants a timestamp update.
4918 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004919 if (newsize != oldsize) {
4920 inode_inc_iversion(inode);
4921 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4922 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004923 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004924 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004925
Josef Bacika41ad392011-01-31 15:30:16 -05004926 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004927 /*
4928 * Don't do an expanding truncate while snapshoting is ongoing.
4929 * This is to ensure the snapshot captures a fully consistent
4930 * state of this file - if the snapshot captures this expanding
4931 * truncation, it must capture all writes that happened before
4932 * this truncation.
4933 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004934 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004935 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004936 if (ret) {
4937 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004938 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004939 }
Yan, Zheng80825102009-11-12 09:35:36 +00004940
Miao Xief4a2f4c2011-12-14 20:12:01 -05004941 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004942 if (IS_ERR(trans)) {
4943 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004944 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004945 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004946
4947 i_size_write(inode, newsize);
4948 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304949 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004950 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004951 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004952 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004953 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004954
Josef Bacika41ad392011-01-31 15:30:16 -05004955 /*
4956 * We're truncating a file that used to have good data down to
4957 * zero. Make sure it gets into the ordered flush list so that
4958 * any new writes get down to disk quickly.
4959 */
4960 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004961 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4962 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004963
Josef Bacikf3fe8202013-01-07 17:03:21 -05004964 /*
4965 * 1 for the orphan item we're going to add
4966 * 1 for the orphan item deletion.
4967 */
4968 trans = btrfs_start_transaction(root, 2);
4969 if (IS_ERR(trans))
4970 return PTR_ERR(trans);
4971
4972 /*
4973 * We need to do this in case we fail at _any_ point during the
4974 * actual truncate. Once we do the truncate_setsize we could
4975 * invalidate pages which forces any outstanding ordered io to
4976 * be instantly completed which will give us extents that need
4977 * to be truncated. If we fail to get an orphan inode down we
4978 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004979 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05004980 * will be consistent.
4981 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004982 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004983 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004984 if (ret)
4985 return ret;
4986
Josef Bacika41ad392011-01-31 15:30:16 -05004987 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4988 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004989
4990 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02004991 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004992 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02004993 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004994
Josef Bacika41ad392011-01-31 15:30:16 -05004995 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004996 if (ret && inode->i_nlink) {
4997 int err;
4998
Liu Bo19fd2df2016-12-01 13:01:02 -08004999 /* To get a stable disk_i_size */
5000 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5001 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005002 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005003 return err;
5004 }
5005
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005006 /*
5007 * failed to truncate, disk_i_size is only adjusted down
5008 * as we remove extents, so it should represent the true
5009 * size of the inode, so reset the in memory size and
5010 * delete our orphan entry.
5011 */
5012 trans = btrfs_join_transaction(root);
5013 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005014 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005015 return ret;
5016 }
5017 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005018 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005019 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005020 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005021 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005022 }
Yan, Zheng80825102009-11-12 09:35:36 +00005023 }
5024
Josef Bacika41ad392011-01-31 15:30:16 -05005025 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005026}
5027
Chris Mason39279cc2007-06-12 06:35:45 -04005028static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5029{
David Howells2b0143b2015-03-17 22:25:59 +00005030 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005031 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005032 int err;
5033
Li Zefanb83cc962010-12-20 16:04:08 +08005034 if (btrfs_root_readonly(root))
5035 return -EROFS;
5036
Jan Kara31051c82016-05-26 16:55:18 +02005037 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005038 if (err)
5039 return err;
5040
Chris Mason5a3f23d2009-03-31 13:27:11 -04005041 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005042 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005043 if (err)
5044 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005045 }
Yan Zheng9036c102008-10-30 14:19:41 -04005046
Christoph Hellwig10257742010-06-04 11:30:02 +02005047 if (attr->ia_valid) {
5048 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005049 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005050 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005051
Josef Bacik22c44fe2011-11-30 10:45:38 -05005052 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005053 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005054 }
5055
Chris Mason39279cc2007-06-12 06:35:45 -04005056 return err;
5057}
Chris Mason61295eb2008-01-14 16:24:38 -05005058
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005059/*
5060 * While truncating the inode pages during eviction, we get the VFS calling
5061 * btrfs_invalidatepage() against each page of the inode. This is slow because
5062 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5063 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5064 * extent_state structures over and over, wasting lots of time.
5065 *
5066 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5067 * those expensive operations on a per page basis and do only the ordered io
5068 * finishing, while we release here the extent_map and extent_state structures,
5069 * without the excessive merging and splitting.
5070 */
5071static void evict_inode_truncate_pages(struct inode *inode)
5072{
5073 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5074 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5075 struct rb_node *node;
5076
5077 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005078 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005079
5080 write_lock(&map_tree->lock);
5081 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5082 struct extent_map *em;
5083
5084 node = rb_first(&map_tree->map);
5085 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005086 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5087 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005088 remove_extent_mapping(map_tree, em);
5089 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005090 if (need_resched()) {
5091 write_unlock(&map_tree->lock);
5092 cond_resched();
5093 write_lock(&map_tree->lock);
5094 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005095 }
5096 write_unlock(&map_tree->lock);
5097
Filipe Manana6ca07092015-05-26 00:55:42 +01005098 /*
5099 * Keep looping until we have no more ranges in the io tree.
5100 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005101 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5102 * still in progress (unlocked the pages in the bio but did not yet
5103 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005104 * ranges can still be locked and eviction started because before
5105 * submitting those bios, which are executed by a separate task (work
5106 * queue kthread), inode references (inode->i_count) were not taken
5107 * (which would be dropped in the end io callback of each bio).
5108 * Therefore here we effectively end up waiting for those bios and
5109 * anyone else holding locked ranges without having bumped the inode's
5110 * reference count - if we don't do it, when they access the inode's
5111 * io_tree to unlock a range it may be too late, leading to an
5112 * use-after-free issue.
5113 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005114 spin_lock(&io_tree->lock);
5115 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5116 struct extent_state *state;
5117 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005118 u64 start;
5119 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005120
5121 node = rb_first(&io_tree->state);
5122 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005123 start = state->start;
5124 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005125 spin_unlock(&io_tree->lock);
5126
David Sterbaff13db42015-12-03 14:30:40 +01005127 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005128
5129 /*
5130 * If still has DELALLOC flag, the extent didn't reach disk,
5131 * and its reserved space won't be freed by delayed_ref.
5132 * So we need to free its reserved space here.
5133 * (Refer to comment in btrfs_invalidatepage, case 2)
5134 *
5135 * Note, end is the bytenr of last byte, so we need + 1 here.
5136 */
5137 if (state->state & EXTENT_DELALLOC)
5138 btrfs_qgroup_free_data(inode, start, end - start + 1);
5139
Filipe Manana6ca07092015-05-26 00:55:42 +01005140 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005141 EXTENT_LOCKED | EXTENT_DIRTY |
5142 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5143 EXTENT_DEFRAG, 1, 1,
5144 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005145
Filipe Manana7064dd52014-08-08 02:47:05 +01005146 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005147 spin_lock(&io_tree->lock);
5148 }
5149 spin_unlock(&io_tree->lock);
5150}
5151
Al Virobd555972010-06-07 11:35:40 -04005152void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005153{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005154 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005155 struct btrfs_trans_handle *trans;
5156 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005157 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005158 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005159 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005160 int ret;
5161
liubo1abe9b82011-03-24 11:18:59 +00005162 trace_btrfs_inode_evict(inode);
5163
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005164 if (!root) {
5165 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5166 return;
5167 }
5168
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005169 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005170
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005171 evict_inode_truncate_pages(inode);
5172
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005173 if (inode->i_nlink &&
5174 ((btrfs_root_refs(&root->root_item) != 0 &&
5175 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005176 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005177 goto no_delete;
5178
Chris Mason39279cc2007-06-12 06:35:45 -04005179 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005180 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005181 goto no_delete;
5182 }
Al Virobd555972010-06-07 11:35:40 -04005183 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005184 if (!special_file(inode->i_mode))
5185 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005186
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005187 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005188
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005189 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005190 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005191 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005192 goto no_delete;
5193 }
5194
Yan, Zheng76dda932009-09-21 16:00:26 -04005195 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005196 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5197 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005198 goto no_delete;
5199 }
5200
Nikolay Borisovaa790212017-01-10 20:35:40 +02005201 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005202 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005203 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005204 goto no_delete;
5205 }
5206
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005207 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005208 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005209 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005210 goto no_delete;
5211 }
Josef Bacik4a338542011-08-29 11:01:31 -04005212 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005213 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005214 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005215
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005216 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005217
Josef Bacik4289a662011-08-05 13:22:24 -04005218 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005219 * This is a bit simpler than btrfs_truncate since we've already
5220 * reserved our space for our orphan item in the unlink, so we just
5221 * need to reserve some slack space in case we add bytes and update
5222 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005223 */
Yan, Zheng80825102009-11-12 09:35:36 +00005224 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005225 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5226 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005227
Josef Bacik726c35f2011-09-26 15:46:06 -04005228 /*
5229 * Try and steal from the global reserve since we will
5230 * likely not use this space anyway, we want to try as
5231 * hard as possible to get this to work.
5232 */
5233 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005234 steal_from_global++;
5235 else
5236 steal_from_global = 0;
5237 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005238
Josef Bacik3bce8762015-02-24 12:35:51 -08005239 /*
5240 * steal_from_global == 0: we reserved stuff, hooray!
5241 * steal_from_global == 1: we didn't reserve stuff, boo!
5242 * steal_from_global == 2: we've committed, still not a lot of
5243 * room but maybe we'll have room in the global reserve this
5244 * time.
5245 * steal_from_global == 3: abandon all hope!
5246 */
5247 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005248 btrfs_warn(fs_info,
5249 "Could not get space for a delete, will truncate on mount %d",
5250 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005251 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005252 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005253 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005254 }
5255
Miao Xie0e8c36a2012-12-19 06:59:51 +00005256 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005257 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005258 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005259 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005260 goto no_delete;
5261 }
5262
Josef Bacik3bce8762015-02-24 12:35:51 -08005263 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005264 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005265 * sure there is room to do it, if not we need to commit and try
5266 * again.
5267 */
5268 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005269 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005270 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005271 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005272 else
5273 ret = -ENOSPC;
5274 }
5275
5276 /*
5277 * Couldn't steal from the global reserve, we have too much
5278 * pending stuff built up, commit the transaction and try it
5279 * again.
5280 */
5281 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005282 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005283 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005284 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005285 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005286 goto no_delete;
5287 }
5288 continue;
5289 } else {
5290 steal_from_global = 0;
5291 }
5292
Josef Bacik4289a662011-08-05 13:22:24 -04005293 trans->block_rsv = rsv;
5294
Yan, Zhengd68fc572010-05-16 10:49:58 -04005295 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005296 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005297 break;
5298
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005299 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005300 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005301 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005302 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005303 }
5304
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005305 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005306
Josef Bacik4ef31a42013-08-13 14:10:08 -04005307 /*
5308 * Errors here aren't a big deal, it just means we leave orphan items
5309 * in the tree. They will be cleaned up on the next mount.
5310 */
Yan, Zheng80825102009-11-12 09:35:36 +00005311 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005312 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005313 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005314 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005315 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005316 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005317
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005318 trans->block_rsv = &fs_info->trans_block_rsv;
5319 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005320 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005321 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005322
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005323 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005324 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005325no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005326 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005327 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005328}
5329
5330/*
5331 * this returns the key found in the dir entry in the location pointer.
5332 * If no dir entries were found, location->objectid is 0.
5333 */
5334static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5335 struct btrfs_key *location)
5336{
5337 const char *name = dentry->d_name.name;
5338 int namelen = dentry->d_name.len;
5339 struct btrfs_dir_item *di;
5340 struct btrfs_path *path;
5341 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005342 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005343
5344 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005345 if (!path)
5346 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005347
David Sterbaf85b7372017-01-20 14:54:07 +01005348 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5349 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005350 if (IS_ERR(di))
5351 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005352
David Sterbac7040052011-04-19 18:00:01 +02005353 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005354 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005355
Chris Mason5f39d392007-10-15 16:14:19 -04005356 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005357out:
Chris Mason39279cc2007-06-12 06:35:45 -04005358 btrfs_free_path(path);
5359 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005360out_err:
5361 location->objectid = 0;
5362 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005363}
5364
5365/*
5366 * when we hit a tree root in a directory, the btrfs part of the inode
5367 * needs to be changed to reflect the root directory of the tree root. This
5368 * is kind of like crossing a mount point.
5369 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005370static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005371 struct inode *dir,
5372 struct dentry *dentry,
5373 struct btrfs_key *location,
5374 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005375{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005376 struct btrfs_path *path;
5377 struct btrfs_root *new_root;
5378 struct btrfs_root_ref *ref;
5379 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005380 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005381 int ret;
5382 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005383
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005384 path = btrfs_alloc_path();
5385 if (!path) {
5386 err = -ENOMEM;
5387 goto out;
5388 }
Chris Mason39279cc2007-06-12 06:35:45 -04005389
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005390 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005391 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5392 key.type = BTRFS_ROOT_REF_KEY;
5393 key.offset = location->objectid;
5394
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005395 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005396 if (ret) {
5397 if (ret < 0)
5398 err = ret;
5399 goto out;
5400 }
Chris Mason39279cc2007-06-12 06:35:45 -04005401
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005402 leaf = path->nodes[0];
5403 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005404 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005405 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5406 goto out;
5407
5408 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5409 (unsigned long)(ref + 1),
5410 dentry->d_name.len);
5411 if (ret)
5412 goto out;
5413
David Sterbab3b4aa72011-04-21 01:20:15 +02005414 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005415
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005416 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005417 if (IS_ERR(new_root)) {
5418 err = PTR_ERR(new_root);
5419 goto out;
5420 }
5421
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005422 *sub_root = new_root;
5423 location->objectid = btrfs_root_dirid(&new_root->root_item);
5424 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005425 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005426 err = 0;
5427out:
5428 btrfs_free_path(path);
5429 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005430}
5431
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005432static void inode_tree_add(struct inode *inode)
5433{
5434 struct btrfs_root *root = BTRFS_I(inode)->root;
5435 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005436 struct rb_node **p;
5437 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005438 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005439 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005440
Al Viro1d3382cb2010-10-23 15:19:20 -04005441 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005442 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005443 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005444 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005445 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005446 while (*p) {
5447 parent = *p;
5448 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5449
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005450 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005451 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005452 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005453 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005454 else {
5455 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005456 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005457 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005458 RB_CLEAR_NODE(parent);
5459 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005460 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005461 }
5462 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005463 rb_link_node(new, parent, p);
5464 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005465 spin_unlock(&root->inode_lock);
5466}
5467
5468static void inode_tree_del(struct inode *inode)
5469{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005470 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005471 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005472 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005473
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005474 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005475 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005476 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005477 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005478 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005479 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005480 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005481
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005482 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005483 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005484 spin_lock(&root->inode_lock);
5485 empty = RB_EMPTY_ROOT(&root->inode_tree);
5486 spin_unlock(&root->inode_lock);
5487 if (empty)
5488 btrfs_add_dead_root(root);
5489 }
5490}
5491
Jeff Mahoney143bede2012-03-01 14:56:26 +01005492void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005493{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005494 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005495 struct rb_node *node;
5496 struct rb_node *prev;
5497 struct btrfs_inode *entry;
5498 struct inode *inode;
5499 u64 objectid = 0;
5500
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005501 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005502 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005503
5504 spin_lock(&root->inode_lock);
5505again:
5506 node = root->inode_tree.rb_node;
5507 prev = NULL;
5508 while (node) {
5509 prev = node;
5510 entry = rb_entry(node, struct btrfs_inode, rb_node);
5511
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005512 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005513 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005514 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005515 node = node->rb_right;
5516 else
5517 break;
5518 }
5519 if (!node) {
5520 while (prev) {
5521 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005522 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005523 node = prev;
5524 break;
5525 }
5526 prev = rb_next(prev);
5527 }
5528 }
5529 while (node) {
5530 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005531 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005532 inode = igrab(&entry->vfs_inode);
5533 if (inode) {
5534 spin_unlock(&root->inode_lock);
5535 if (atomic_read(&inode->i_count) > 1)
5536 d_prune_aliases(inode);
5537 /*
Al Viro45321ac2010-06-07 13:43:19 -04005538 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005539 * the inode cache when its usage count
5540 * hits zero.
5541 */
5542 iput(inode);
5543 cond_resched();
5544 spin_lock(&root->inode_lock);
5545 goto again;
5546 }
5547
5548 if (cond_resched_lock(&root->inode_lock))
5549 goto again;
5550
5551 node = rb_next(node);
5552 }
5553 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005554}
5555
Chris Masone02119d2008-09-05 16:13:11 -04005556static int btrfs_init_locked_inode(struct inode *inode, void *p)
5557{
5558 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005559 inode->i_ino = args->location->objectid;
5560 memcpy(&BTRFS_I(inode)->location, args->location,
5561 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005562 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005563 return 0;
5564}
5565
5566static int btrfs_find_actor(struct inode *inode, void *opaque)
5567{
5568 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005569 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005570 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005571}
5572
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005573static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005574 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005575 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005576{
5577 struct inode *inode;
5578 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005579 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005580
Chris Mason90d3e592014-01-09 17:28:00 -08005581 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005582 args.root = root;
5583
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005584 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005585 btrfs_init_locked_inode,
5586 (void *)&args);
5587 return inode;
5588}
5589
Balaji Rao1a54ef82008-07-21 02:01:04 +05305590/* Get an inode object given its location and corresponding root.
5591 * Returns in *is_new if the inode was read from disk
5592 */
5593struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005594 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305595{
5596 struct inode *inode;
5597
Chris Mason90d3e592014-01-09 17:28:00 -08005598 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305599 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305601
5602 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005603 int ret;
5604
5605 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005606 if (!is_bad_inode(inode)) {
5607 inode_tree_add(inode);
5608 unlock_new_inode(inode);
5609 if (new)
5610 *new = 1;
5611 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005612 unlock_new_inode(inode);
5613 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005614 ASSERT(ret < 0);
5615 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005616 }
5617 }
5618
Balaji Rao1a54ef82008-07-21 02:01:04 +05305619 return inode;
5620}
5621
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005622static struct inode *new_simple_dir(struct super_block *s,
5623 struct btrfs_key *key,
5624 struct btrfs_root *root)
5625{
5626 struct inode *inode = new_inode(s);
5627
5628 if (!inode)
5629 return ERR_PTR(-ENOMEM);
5630
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005631 BTRFS_I(inode)->root = root;
5632 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005633 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005634
5635 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005636 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005637 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638 inode->i_fop = &simple_dir_operations;
5639 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005640 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305641 inode->i_atime = inode->i_mtime;
5642 inode->i_ctime = inode->i_mtime;
5643 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005644
5645 return inode;
5646}
5647
Chris Mason3de45862008-11-17 21:02:50 -05005648struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005649{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005650 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005651 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005653 struct btrfs_root *sub_root = root;
5654 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005655 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005656 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005657
5658 if (dentry->d_name.len > BTRFS_NAME_LEN)
5659 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005660
Jeff Layton39e3c952012-11-28 11:30:53 -05005661 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005662 if (ret < 0)
5663 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005664
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005665 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005666 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005667
5668 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005669 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005670 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005671 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005672
5673 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5674
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005675 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005676 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005677 &location, &sub_root);
5678 if (ret < 0) {
5679 if (ret != -ENOENT)
5680 inode = ERR_PTR(ret);
5681 else
5682 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5683 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005684 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005685 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005686 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005687
Julia Lawall34d19ba2011-01-24 19:55:19 +00005688 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005689 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005690 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005691 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005692 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005693 if (ret) {
5694 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005695 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005696 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005697 }
5698
Chris Mason3de45862008-11-17 21:02:50 -05005699 return inode;
5700}
5701
Nick Pigginfe15ce42011-01-07 17:49:23 +11005702static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005703{
5704 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005705 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005706
Li Zefan848cce02012-02-21 17:04:28 +08005707 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005708 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005709
Li Zefan848cce02012-02-21 17:04:28 +08005710 if (inode) {
5711 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005712 if (btrfs_root_refs(&root->root_item) == 0)
5713 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005714
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005715 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005716 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005717 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005718 return 0;
5719}
5720
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005721static void btrfs_dentry_release(struct dentry *dentry)
5722{
Daeseok Youn944a4512014-04-14 15:37:02 +09005723 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005724}
5725
Chris Mason3de45862008-11-17 21:02:50 -05005726static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005727 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005728{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005729 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005730
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005731 inode = btrfs_lookup_dentry(dir, dentry);
5732 if (IS_ERR(inode)) {
5733 if (PTR_ERR(inode) == -ENOENT)
5734 inode = NULL;
5735 else
5736 return ERR_CAST(inode);
5737 }
5738
Al Viro41d28bc2014-10-12 22:24:21 -04005739 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005740}
5741
Miao Xie16cdcec2011-04-22 18:12:22 +08005742unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005743 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5744};
5745
Al Viro9cdda8d2013-05-22 16:48:09 -04005746static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005747{
Al Viro9cdda8d2013-05-22 16:48:09 -04005748 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005749 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005750 struct btrfs_root *root = BTRFS_I(inode)->root;
5751 struct btrfs_item *item;
5752 struct btrfs_dir_item *di;
5753 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005754 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005755 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005756 struct list_head ins_list;
5757 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005758 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005759 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005760 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005761 unsigned char d_type;
5762 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005763 char tmp_name[32];
5764 char *name_ptr;
5765 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005766 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005767 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005768
Al Viro9cdda8d2013-05-22 16:48:09 -04005769 if (!dir_emit_dots(file, ctx))
5770 return 0;
5771
David Woodhouse49593bf2008-08-17 17:08:36 +01005772 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005773 if (!path)
5774 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005775
David Sterbae4058b52015-11-27 16:31:35 +01005776 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005777
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005778 INIT_LIST_HEAD(&ins_list);
5779 INIT_LIST_HEAD(&del_list);
5780 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005781
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005782 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005783 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005784 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005785
Chris Mason39279cc2007-06-12 06:35:45 -04005786 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5787 if (ret < 0)
5788 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005789
5790 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005791 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005792 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005793 if (slot >= btrfs_header_nritems(leaf)) {
5794 ret = btrfs_next_leaf(root, path);
5795 if (ret < 0)
5796 goto err;
5797 else if (ret > 0)
5798 break;
5799 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005800 }
Chris Mason3de45862008-11-17 21:02:50 -05005801
Ross Kirkdd3cc162013-09-16 15:58:09 +01005802 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005803 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5804
5805 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005806 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005807 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005808 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005809 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005810 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005811 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005812 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005813
Al Viro9cdda8d2013-05-22 16:48:09 -04005814 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005815
Chris Mason39279cc2007-06-12 06:35:45 -04005816 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005817 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005818 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005819
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005820 name_len = btrfs_dir_name_len(leaf, di);
5821 if (name_len <= sizeof(tmp_name)) {
5822 name_ptr = tmp_name;
5823 } else {
5824 name_ptr = kmalloc(name_len, GFP_KERNEL);
5825 if (!name_ptr) {
5826 ret = -ENOMEM;
5827 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005828 }
Chris Mason39279cc2007-06-12 06:35:45 -04005829 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005830 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5831 name_len);
5832
5833 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5834 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5835
5836 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5837 d_type);
5838
5839 if (name_ptr != tmp_name)
5840 kfree(name_ptr);
5841
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005842 if (over)
5843 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005844 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005845next:
5846 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005847 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005848
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005849 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005850 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005851 goto nopos;
5852
Zach Browndb62efb2013-07-11 16:19:42 -07005853 /*
5854 * Stop new entries from being returned after we return the last
5855 * entry.
5856 *
5857 * New directory entries are assigned a strictly increasing
5858 * offset. This means that new entries created during readdir
5859 * are *guaranteed* to be seen in the future by that readdir.
5860 * This has broken buggy programs which operate on names as
5861 * they're returned by readdir. Until we re-use freed offsets
5862 * we have this hack to stop new entries from being returned
5863 * under the assumption that they'll never reach this huge
5864 * offset.
5865 *
5866 * This is being careful not to overflow 32bit loff_t unless the
5867 * last entry requires it because doing so has broken 32bit apps
5868 * in the past.
5869 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005870 if (ctx->pos >= INT_MAX)
5871 ctx->pos = LLONG_MAX;
5872 else
5873 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005874nopos:
5875 ret = 0;
5876err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005877 if (put)
5878 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005879 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005880 return ret;
5881}
5882
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005883int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005884{
5885 struct btrfs_root *root = BTRFS_I(inode)->root;
5886 struct btrfs_trans_handle *trans;
5887 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005888 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005889
Josef Bacik72ac3c02012-05-23 14:13:11 -04005890 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005891 return 0;
5892
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005893 if (btrfs_fs_closing(root->fs_info) &&
5894 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08005895 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005896
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005897 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005898 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005899 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005900 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005901 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005902 if (IS_ERR(trans))
5903 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005904 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005905 }
5906 return ret;
5907}
5908
5909/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005910 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005911 * inode changes. But, it is most likely to find the inode in cache.
5912 * FIXME, needs more benchmarking...there are no reasons other than performance
5913 * to keep or drop this code.
5914 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005915static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005916{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005917 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005918 struct btrfs_root *root = BTRFS_I(inode)->root;
5919 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005920 int ret;
5921
Josef Bacik72ac3c02012-05-23 14:13:11 -04005922 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005923 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005924
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005925 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005926 if (IS_ERR(trans))
5927 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005928
5929 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005930 if (ret && ret == -ENOSPC) {
5931 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005932 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005933 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005934 if (IS_ERR(trans))
5935 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005936
Chris Mason94b60442010-05-26 11:02:00 -04005937 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005938 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005939 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005940 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005941 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005942
5943 return ret;
5944}
5945
5946/*
5947 * This is a copy of file_update_time. We need this so we can return error on
5948 * ENOSPC for updating the inode in the case of file write and mmap writes.
5949 */
Josef Bacike41f9412012-03-26 09:46:47 -04005950static int btrfs_update_time(struct inode *inode, struct timespec *now,
5951 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005952{
Alexander Block2bc5565282012-06-15 09:49:33 +02005953 struct btrfs_root *root = BTRFS_I(inode)->root;
5954
5955 if (btrfs_root_readonly(root))
5956 return -EROFS;
5957
Josef Bacike41f9412012-03-26 09:46:47 -04005958 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005959 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005960 if (flags & S_CTIME)
5961 inode->i_ctime = *now;
5962 if (flags & S_MTIME)
5963 inode->i_mtime = *now;
5964 if (flags & S_ATIME)
5965 inode->i_atime = *now;
5966 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005967}
5968
Chris Masond352ac62008-09-29 15:18:18 -04005969/*
5970 * find the highest existing sequence number in a directory
5971 * and then set the in-memory index_cnt variable to reflect
5972 * free sequence numbers
5973 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005974static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005975{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005976 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005977 struct btrfs_key key, found_key;
5978 struct btrfs_path *path;
5979 struct extent_buffer *leaf;
5980 int ret;
5981
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005982 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005983 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005984 key.offset = (u64)-1;
5985
5986 path = btrfs_alloc_path();
5987 if (!path)
5988 return -ENOMEM;
5989
5990 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5991 if (ret < 0)
5992 goto out;
5993 /* FIXME: we should be able to handle this */
5994 if (ret == 0)
5995 goto out;
5996 ret = 0;
5997
5998 /*
5999 * MAGIC NUMBER EXPLANATION:
6000 * since we search a directory based on f_pos we have to start at 2
6001 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6002 * else has to start at 2
6003 */
6004 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006005 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006006 goto out;
6007 }
6008
6009 path->slots[0]--;
6010
6011 leaf = path->nodes[0];
6012 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6013
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006014 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006015 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006016 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006017 goto out;
6018 }
6019
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006020 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006021out:
6022 btrfs_free_path(path);
6023 return ret;
6024}
6025
Chris Masond352ac62008-09-29 15:18:18 -04006026/*
6027 * helper to find a free sequence number in a given directory. This current
6028 * code is very simple, later versions will do smarter things in the btree
6029 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006030int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006031{
6032 int ret = 0;
6033
Nikolay Borisov877574e2017-02-20 13:50:33 +02006034 if (dir->index_cnt == (u64)-1) {
6035 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006036 if (ret) {
6037 ret = btrfs_set_inode_index_count(dir);
6038 if (ret)
6039 return ret;
6040 }
Josef Bacikaec74772008-07-24 12:12:38 -04006041 }
6042
Nikolay Borisov877574e2017-02-20 13:50:33 +02006043 *index = dir->index_cnt;
6044 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006045
6046 return ret;
6047}
6048
Chris Masonb0d5d102014-09-08 13:08:51 -07006049static int btrfs_insert_inode_locked(struct inode *inode)
6050{
6051 struct btrfs_iget_args args;
6052 args.location = &BTRFS_I(inode)->location;
6053 args.root = BTRFS_I(inode)->root;
6054
6055 return insert_inode_locked4(inode,
6056 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6057 btrfs_find_actor, &args);
6058}
6059
Chris Mason39279cc2007-06-12 06:35:45 -04006060static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6061 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006062 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006063 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006064 u64 ref_objectid, u64 objectid,
6065 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006066{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006067 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006068 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006069 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006070 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006071 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006072 struct btrfs_inode_ref *ref;
6073 struct btrfs_key key[2];
6074 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006075 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006076 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006077 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006078
Chris Mason5f39d392007-10-15 16:14:19 -04006079 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006080 if (!path)
6081 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006082
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006083 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006084 if (!inode) {
6085 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006086 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006087 }
Chris Mason39279cc2007-06-12 06:35:45 -04006088
Li Zefan581bb052011-04-20 10:06:11 +08006089 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006090 * O_TMPFILE, set link count to 0, so that after this point,
6091 * we fill in an inode item with the correct link count.
6092 */
6093 if (!name)
6094 set_nlink(inode, 0);
6095
6096 /*
Li Zefan581bb052011-04-20 10:06:11 +08006097 * we have to initialize this early, so we can reclaim the inode
6098 * number if we fail afterwards in this function.
6099 */
6100 inode->i_ino = objectid;
6101
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006102 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006103 trace_btrfs_inode_request(dir);
6104
Nikolay Borisov877574e2017-02-20 13:50:33 +02006105 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006106 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006107 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006108 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006109 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006110 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006111 } else if (dir) {
6112 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006113 }
6114 /*
6115 * index_cnt is ignored for everything but a dir,
6116 * btrfs_get_inode_index_count has an explanation for the magic
6117 * number
6118 */
6119 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006120 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006121 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006122 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006123 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006124
Josef Bacik5dc562c2012-08-17 13:14:17 -04006125 /*
6126 * We could have gotten an inode number from somebody who was fsynced
6127 * and then removed in this same transaction, so let's just set full
6128 * sync since it will be a full sync anyway and this will blow away the
6129 * old info in the log.
6130 */
6131 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6132
Chris Mason9c583092008-01-29 15:15:18 -05006133 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006134 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006135 key[0].offset = 0;
6136
Chris Mason9c583092008-01-29 15:15:18 -05006137 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006138
6139 if (name) {
6140 /*
6141 * Start new inodes with an inode_ref. This is slightly more
6142 * efficient for small numbers of hard links since they will
6143 * be packed into one item. Extended refs will kick in if we
6144 * add more hard links than can fit in the ref item.
6145 */
6146 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006147 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006148 key[1].offset = ref_objectid;
6149
6150 sizes[1] = name_len + sizeof(*ref);
6151 }
Chris Mason9c583092008-01-29 15:15:18 -05006152
Chris Masonb0d5d102014-09-08 13:08:51 -07006153 location = &BTRFS_I(inode)->location;
6154 location->objectid = objectid;
6155 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006156 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006157
6158 ret = btrfs_insert_inode_locked(inode);
6159 if (ret < 0)
6160 goto fail;
6161
Chris Masonb9473432009-03-13 11:00:37 -04006162 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006163 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006164 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006165 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006166
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006167 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006168 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306169
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006170 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306171 inode->i_atime = inode->i_mtime;
6172 inode->i_ctime = inode->i_mtime;
6173 BTRFS_I(inode)->i_otime = inode->i_mtime;
6174
Chris Mason5f39d392007-10-15 16:14:19 -04006175 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6176 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006177 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006178 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006179 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006180
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006181 if (name) {
6182 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6183 struct btrfs_inode_ref);
6184 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6185 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6186 ptr = (unsigned long)(ref + 1);
6187 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6188 }
Chris Mason9c583092008-01-29 15:15:18 -05006189
Chris Mason5f39d392007-10-15 16:14:19 -04006190 btrfs_mark_buffer_dirty(path->nodes[0]);
6191 btrfs_free_path(path);
6192
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006193 btrfs_inherit_iflags(inode, dir);
6194
Al Viro569254b2011-07-24 17:08:40 -04006195 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006196 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006197 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006198 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006199 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6200 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006201 }
6202
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006203 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006204
6205 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006206 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006207
Alexander Block8ea05e32012-07-25 17:35:53 +02006208 btrfs_update_root_times(trans, root);
6209
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006210 ret = btrfs_inode_inherit_props(trans, inode, dir);
6211 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006212 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006213 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006214 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006215
Chris Mason39279cc2007-06-12 06:35:45 -04006216 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006217
6218fail_unlock:
6219 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006220fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006221 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006222 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006223 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006224 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006225 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006226}
6227
6228static inline u8 btrfs_inode_type(struct inode *inode)
6229{
6230 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6231}
6232
Chris Masond352ac62008-09-29 15:18:18 -04006233/*
6234 * utility function to add 'inode' into 'parent_inode' with
6235 * a give name and a given sequence number.
6236 * if 'add_backref' is true, also insert a backref from the
6237 * inode to the parent directory.
6238 */
Chris Masone02119d2008-09-05 16:13:11 -04006239int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006240 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006241 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006242{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006243 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006244 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006245 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006246 struct btrfs_root *root = parent_inode->root;
6247 u64 ino = btrfs_ino(inode);
6248 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006249
Li Zefan33345d012011-04-20 10:31:50 +08006250 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006251 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006252 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006253 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006254 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006255 key.offset = 0;
6256 }
Chris Mason39279cc2007-06-12 06:35:45 -04006257
Li Zefan33345d012011-04-20 10:31:50 +08006258 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006259 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6260 root->root_key.objectid, parent_ino,
6261 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006262 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006263 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6264 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006265 }
6266
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006267 /* Nothing to clean up yet */
6268 if (ret)
6269 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006270
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006271 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6272 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006273 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006274 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006275 goto fail_dir_item;
6276 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006277 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006278 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006279 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006280
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006281 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006282 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006283 inode_inc_iversion(&parent_inode->vfs_inode);
6284 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6285 current_time(&parent_inode->vfs_inode);
6286 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006287 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006288 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006289 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006290
6291fail_dir_item:
6292 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6293 u64 local_index;
6294 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006295 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6296 root->root_key.objectid, parent_ino,
6297 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006298
6299 } else if (add_backref) {
6300 u64 local_index;
6301 int err;
6302
6303 err = btrfs_del_inode_ref(trans, root, name, name_len,
6304 ino, parent_ino, &local_index);
6305 }
6306 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006307}
6308
6309static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006310 struct btrfs_inode *dir, struct dentry *dentry,
6311 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006312{
Josef Bacika1b075d2010-11-19 20:36:11 +00006313 int err = btrfs_add_link(trans, dir, inode,
6314 dentry->d_name.name, dentry->d_name.len,
6315 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006316 if (err > 0)
6317 err = -EEXIST;
6318 return err;
6319}
6320
Josef Bacik618e21d2007-07-11 10:18:17 -04006321static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006322 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006323{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006324 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006325 struct btrfs_trans_handle *trans;
6326 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006327 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006328 int err;
6329 int drop_inode = 0;
6330 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006331 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006332
Josef Bacik9ed74f22009-09-11 16:12:44 -04006333 /*
6334 * 2 for inode item and ref
6335 * 2 for dir items
6336 * 1 for xattr if selinux is on
6337 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006338 trans = btrfs_start_transaction(root, 5);
6339 if (IS_ERR(trans))
6340 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006341
Li Zefan581bb052011-04-20 10:06:11 +08006342 err = btrfs_find_free_ino(root, &objectid);
6343 if (err)
6344 goto out_unlock;
6345
Josef Bacikaec74772008-07-24 12:12:38 -04006346 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006347 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6348 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006349 if (IS_ERR(inode)) {
6350 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006351 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006352 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006353
Casey Schauflerad19db72011-12-15 10:09:07 -05006354 /*
6355 * If the active LSM wants to access the inode during
6356 * d_instantiate it needs these. Smack checks to see
6357 * if the filesystem supports xattrs by looking at the
6358 * ops vector.
6359 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006360 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006361 init_special_inode(inode, inode->i_mode, rdev);
6362
6363 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006364 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006365 goto out_unlock_inode;
6366
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006367 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6368 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006369 if (err) {
6370 goto out_unlock_inode;
6371 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006372 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006373 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006374 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006375 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006376
Josef Bacik618e21d2007-07-11 10:18:17 -04006377out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006378 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006379 btrfs_balance_delayed_items(fs_info);
6380 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006381 if (drop_inode) {
6382 inode_dec_link_count(inode);
6383 iput(inode);
6384 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006385 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006386
6387out_unlock_inode:
6388 drop_inode = 1;
6389 unlock_new_inode(inode);
6390 goto out_unlock;
6391
Josef Bacik618e21d2007-07-11 10:18:17 -04006392}
6393
Chris Mason39279cc2007-06-12 06:35:45 -04006394static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006395 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006396{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006397 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006398 struct btrfs_trans_handle *trans;
6399 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006400 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006401 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006402 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006403 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006404 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006405
Josef Bacik9ed74f22009-09-11 16:12:44 -04006406 /*
6407 * 2 for inode item and ref
6408 * 2 for dir items
6409 * 1 for xattr if selinux is on
6410 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006411 trans = btrfs_start_transaction(root, 5);
6412 if (IS_ERR(trans))
6413 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006414
Li Zefan581bb052011-04-20 10:06:11 +08006415 err = btrfs_find_free_ino(root, &objectid);
6416 if (err)
6417 goto out_unlock;
6418
Josef Bacikaec74772008-07-24 12:12:38 -04006419 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006420 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6421 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006422 if (IS_ERR(inode)) {
6423 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006424 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006425 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006426 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006427 /*
6428 * If the active LSM wants to access the inode during
6429 * d_instantiate it needs these. Smack checks to see
6430 * if the filesystem supports xattrs by looking at the
6431 * ops vector.
6432 */
6433 inode->i_fop = &btrfs_file_operations;
6434 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006435 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006436
6437 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6438 if (err)
6439 goto out_unlock_inode;
6440
6441 err = btrfs_update_inode(trans, root, inode);
6442 if (err)
6443 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006444
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006445 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6446 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006447 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006448 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006449
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006450 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006451 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006452 d_instantiate(dentry, inode);
6453
Chris Mason39279cc2007-06-12 06:35:45 -04006454out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006455 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006456 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006457 inode_dec_link_count(inode);
6458 iput(inode);
6459 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006460 btrfs_balance_delayed_items(fs_info);
6461 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006462 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006463
6464out_unlock_inode:
6465 unlock_new_inode(inode);
6466 goto out_unlock;
6467
Chris Mason39279cc2007-06-12 06:35:45 -04006468}
6469
6470static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6471 struct dentry *dentry)
6472{
Filipe Manana271dba452016-01-05 16:24:05 +00006473 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006474 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006475 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006476 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006477 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006478 int err;
6479 int drop_inode = 0;
6480
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006481 /* do not allow sys_link's with other subvols of the same device */
6482 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006483 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006484
Mark Fashehf1863732012-08-08 11:32:27 -07006485 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006486 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006487
Nikolay Borisov877574e2017-02-20 13:50:33 +02006488 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006489 if (err)
6490 goto fail;
6491
Yan, Zhenga22285a2010-05-16 10:48:46 -04006492 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006493 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006494 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006495 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006496 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006497 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006498 if (IS_ERR(trans)) {
6499 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006500 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006501 goto fail;
6502 }
Chris Mason5f39d392007-10-15 16:14:19 -04006503
Miao Xie67de1172013-12-26 13:07:06 +08006504 /* There are several dir indexes for this inode, clear the cache. */
6505 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006506 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006507 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006508 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006509 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006510 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006511
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006512 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6513 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006514
Yan, Zhenga5719522009-09-24 09:17:31 -04006515 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006516 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006517 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006518 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006519 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006520 if (err)
6521 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006522 if (inode->i_nlink == 1) {
6523 /*
6524 * If new hard link count is 1, it's a file created
6525 * with open(2) O_TMPFILE flag.
6526 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006527 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006528 if (err)
6529 goto fail;
6530 }
Al Viro08c422c2011-12-23 07:58:13 -05006531 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006532 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006533 }
Chris Mason39279cc2007-06-12 06:35:45 -04006534
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006535 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006536fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006537 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006538 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006539 if (drop_inode) {
6540 inode_dec_link_count(inode);
6541 iput(inode);
6542 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006543 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006544 return err;
6545}
6546
Al Viro18bb1db2011-07-26 01:41:39 -04006547static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006548{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006549 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006550 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006551 struct btrfs_trans_handle *trans;
6552 struct btrfs_root *root = BTRFS_I(dir)->root;
6553 int err = 0;
6554 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006555 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006556 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006557
Josef Bacik9ed74f22009-09-11 16:12:44 -04006558 /*
6559 * 2 items for inode and ref
6560 * 2 items for dir items
6561 * 1 for xattr if selinux is on
6562 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006563 trans = btrfs_start_transaction(root, 5);
6564 if (IS_ERR(trans))
6565 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006566
Li Zefan581bb052011-04-20 10:06:11 +08006567 err = btrfs_find_free_ino(root, &objectid);
6568 if (err)
6569 goto out_fail;
6570
Josef Bacikaec74772008-07-24 12:12:38 -04006571 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006572 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6573 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006574 if (IS_ERR(inode)) {
6575 err = PTR_ERR(inode);
6576 goto out_fail;
6577 }
Chris Mason5f39d392007-10-15 16:14:19 -04006578
Chris Mason39279cc2007-06-12 06:35:45 -04006579 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006580 /* these must be set before we unlock the inode */
6581 inode->i_op = &btrfs_dir_inode_operations;
6582 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006583
Eric Paris2a7dba32011-02-01 11:05:39 -05006584 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006585 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006586 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006587
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006588 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006589 err = btrfs_update_inode(trans, root, inode);
6590 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006591 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006592
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006593 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6594 dentry->d_name.name,
6595 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006596 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006597 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006598
Chris Mason39279cc2007-06-12 06:35:45 -04006599 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006600 /*
6601 * mkdir is special. We're unlocking after we call d_instantiate
6602 * to avoid a race with nfsd calling d_instantiate.
6603 */
6604 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006605 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006606
6607out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006608 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006609 if (drop_on_err) {
6610 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006611 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006612 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006613 btrfs_balance_delayed_items(fs_info);
6614 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006615 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006616
6617out_fail_inode:
6618 unlock_new_inode(inode);
6619 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006620}
6621
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006622/* Find next extent map of a given extent map, caller needs to ensure locks */
6623static struct extent_map *next_extent_map(struct extent_map *em)
6624{
6625 struct rb_node *next;
6626
6627 next = rb_next(&em->rb_node);
6628 if (!next)
6629 return NULL;
6630 return container_of(next, struct extent_map, rb_node);
6631}
6632
6633static struct extent_map *prev_extent_map(struct extent_map *em)
6634{
6635 struct rb_node *prev;
6636
6637 prev = rb_prev(&em->rb_node);
6638 if (!prev)
6639 return NULL;
6640 return container_of(prev, struct extent_map, rb_node);
6641}
6642
Chris Masond352ac62008-09-29 15:18:18 -04006643/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006644 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006645 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006646 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006647 */
Chris Mason3b951512008-04-17 11:29:12 -04006648static int merge_extent_mapping(struct extent_map_tree *em_tree,
6649 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006650 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006651 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006652{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006653 struct extent_map *prev;
6654 struct extent_map *next;
6655 u64 start;
6656 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006657 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006658
Chris Masone6dcd2d2008-07-17 12:53:50 -04006659 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006660
6661 if (existing->start > map_start) {
6662 next = existing;
6663 prev = prev_extent_map(next);
6664 } else {
6665 prev = existing;
6666 next = next_extent_map(prev);
6667 }
6668
6669 start = prev ? extent_map_end(prev) : em->start;
6670 start = max_t(u64, start, em->start);
6671 end = next ? next->start : extent_map_end(em);
6672 end = min_t(u64, end, extent_map_end(em));
6673 start_diff = start - em->start;
6674 em->start = start;
6675 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006676 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6677 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006678 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006679 em->block_len -= start_diff;
6680 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006681 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006682}
6683
Chris Masonc8b97812008-10-29 14:49:59 -04006684static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006685 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006686 size_t pg_offset, u64 extent_offset,
6687 struct btrfs_file_extent_item *item)
6688{
6689 int ret;
6690 struct extent_buffer *leaf = path->nodes[0];
6691 char *tmp;
6692 size_t max_size;
6693 unsigned long inline_size;
6694 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006695 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006696
6697 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006698 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006699 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6700 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006701 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006702 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006703 if (!tmp)
6704 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006705 ptr = btrfs_file_extent_inline_start(item);
6706
6707 read_extent_buffer(leaf, tmp, ptr, inline_size);
6708
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006709 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006710 ret = btrfs_decompress(compress_type, tmp, page,
6711 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006712 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006713 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006714}
6715
Chris Masond352ac62008-09-29 15:18:18 -04006716/*
6717 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006718 * the ugly parts come from merging extents from the disk with the in-ram
6719 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006720 * where the in-ram extents might be locked pending data=ordered completion.
6721 *
6722 * This also copies inline extents directly into the page.
6723 */
Chris Masond3977122009-01-05 21:25:51 -05006724
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006725struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6726 struct page *page,
6727 size_t pg_offset, u64 start, u64 len,
6728 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006729{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006730 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006731 int ret;
6732 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006733 u64 extent_start = 0;
6734 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006735 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006736 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006737 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006738 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006739 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006740 struct extent_buffer *leaf;
6741 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006742 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006743 struct extent_map_tree *em_tree = &inode->extent_tree;
6744 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006745 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006746 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006747
Chris Masona52d9a82007-08-27 16:49:44 -04006748again:
Chris Mason890871b2009-09-02 16:24:52 -04006749 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006750 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006751 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006752 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006753 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006754
Chris Masona52d9a82007-08-27 16:49:44 -04006755 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006756 if (em->start > start || em->start + em->len <= start)
6757 free_extent_map(em);
6758 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006759 free_extent_map(em);
6760 else
6761 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006762 }
David Sterba172ddd62011-04-21 00:48:27 +02006763 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006764 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006765 err = -ENOMEM;
6766 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006767 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006768 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006769 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006770 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006771 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006772 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006773
6774 if (!path) {
6775 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006776 if (!path) {
6777 err = -ENOMEM;
6778 goto out;
6779 }
6780 /*
6781 * Chances are we'll be called again, so go ahead and do
6782 * readahead
6783 */
David Sterbae4058b52015-11-27 16:31:35 +01006784 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006785 }
6786
Chris Mason179e29e2007-11-01 11:28:41 -04006787 ret = btrfs_lookup_file_extent(trans, root, path,
6788 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006789 if (ret < 0) {
6790 err = ret;
6791 goto out;
6792 }
6793
6794 if (ret != 0) {
6795 if (path->slots[0] == 0)
6796 goto not_found;
6797 path->slots[0]--;
6798 }
6799
Chris Mason5f39d392007-10-15 16:14:19 -04006800 leaf = path->nodes[0];
6801 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006802 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006803 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006804 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006805 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006806 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006807 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006808 /*
6809 * If we backup past the first extent we want to move forward
6810 * and see if there is an extent in front of us, otherwise we'll
6811 * say there is a hole for our whole search range which can
6812 * cause problems.
6813 */
6814 extent_end = start;
6815 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006816 }
6817
Chris Mason5f39d392007-10-15 16:14:19 -04006818 found_type = btrfs_file_extent_type(leaf, item);
6819 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006820 if (found_type == BTRFS_FILE_EXTENT_REG ||
6821 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006822 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006823 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006824 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6825 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006826 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006827 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006828 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006829 }
Josef Bacik25a50342013-10-14 12:08:38 -04006830next:
Yan Zheng9036c102008-10-30 14:19:41 -04006831 if (start >= extent_end) {
6832 path->slots[0]++;
6833 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6834 ret = btrfs_next_leaf(root, path);
6835 if (ret < 0) {
6836 err = ret;
6837 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006838 }
Yan Zheng9036c102008-10-30 14:19:41 -04006839 if (ret > 0)
6840 goto not_found;
6841 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006842 }
Yan Zheng9036c102008-10-30 14:19:41 -04006843 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6844 if (found_key.objectid != objectid ||
6845 found_key.type != BTRFS_EXTENT_DATA_KEY)
6846 goto not_found;
6847 if (start + len <= found_key.offset)
6848 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006849 if (start > found_key.offset)
6850 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006851 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006852 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006853 em->len = found_key.offset - start;
6854 goto not_found_em;
6855 }
6856
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006857 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006858 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006859
Yan Zhengd899e052008-10-30 14:25:28 -04006860 if (found_type == BTRFS_FILE_EXTENT_REG ||
6861 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006862 goto insert;
6863 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006864 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006865 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006866 size_t size;
6867 size_t extent_offset;
6868 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006869
Filipe Manana7ffbb592014-06-09 03:48:05 +01006870 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006871 goto out;
Yan689f9342007-10-29 11:41:07 -04006872
Chris Mason514ac8a2014-01-03 21:07:00 -08006873 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006874 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006875 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6876 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006877 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006878 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006879 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006880 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006881 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006882 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006883 if (btrfs_file_extent_compression(leaf, item) !=
6884 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006885 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006886 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006887 if (ret) {
6888 err = ret;
6889 goto out;
6890 }
Chris Masonc8b97812008-10-29 14:49:59 -04006891 } else {
6892 map = kmap(page);
6893 read_extent_buffer(leaf, map + pg_offset, ptr,
6894 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006895 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006896 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006897 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006898 copy_size);
6899 }
Chris Masonc8b97812008-10-29 14:49:59 -04006900 kunmap(page);
6901 }
Chris Mason179e29e2007-11-01 11:28:41 -04006902 flush_dcache_page(page);
6903 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006904 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006905 if (!trans) {
6906 kunmap(page);
6907 free_extent_map(em);
6908 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006909
David Sterbab3b4aa72011-04-21 01:20:15 +02006910 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006911 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006912
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006913 if (IS_ERR(trans))
6914 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006915 goto again;
6916 }
Chris Masonc8b97812008-10-29 14:49:59 -04006917 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006918 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006919 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006920 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006921 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006922 }
Chris Masond1310b22008-01-24 16:13:08 -05006923 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006924 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006925 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006926 }
6927not_found:
6928 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006929 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006930 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006931not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006932 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006933 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006934insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006935 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006936 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006937 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006938 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6939 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006940 err = -EIO;
6941 goto out;
6942 }
Chris Masond1310b22008-01-24 16:13:08 -05006943
6944 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006945 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006946 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006947 /* it is possible that someone inserted the extent into the tree
6948 * while we had the lock dropped. It is also possible that
6949 * an overlapping map exists in the tree
6950 */
Chris Masona52d9a82007-08-27 16:49:44 -04006951 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006952 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006953
6954 ret = 0;
6955
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006956 existing = search_extent_mapping(em_tree, start, len);
6957 /*
6958 * existing will always be non-NULL, since there must be
6959 * extent causing the -EEXIST.
6960 */
Chris Mason8dff9c82015-09-19 11:28:25 -07006961 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006962 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07006963 em->block_start == existing->block_start) {
6964 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006965 * The existing extent map already encompasses the
6966 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07006967 */
6968 free_extent_map(em);
6969 em = existing;
6970 err = 0;
6971
6972 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006973 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006974 /*
6975 * The existing extent map is the one nearest to
6976 * the [start, start + len) range which overlaps
6977 */
6978 err = merge_extent_mapping(em_tree, existing,
6979 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006980 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006981 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006982 free_extent_map(em);
6983 em = NULL;
6984 }
6985 } else {
6986 free_extent_map(em);
6987 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006988 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006989 }
Chris Masona52d9a82007-08-27 16:49:44 -04006990 }
Chris Mason890871b2009-09-02 16:24:52 -04006991 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006992out:
liubo1abe9b82011-03-24 11:18:59 +00006993
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006994 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006995
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006996 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006997 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006998 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05006999 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007000 err = ret;
7001 }
Chris Masona52d9a82007-08-27 16:49:44 -04007002 if (err) {
7003 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007004 return ERR_PTR(err);
7005 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007006 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007007 return em;
7008}
7009
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007010struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7011 struct page *page,
7012 size_t pg_offset, u64 start, u64 len,
7013 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007014{
7015 struct extent_map *em;
7016 struct extent_map *hole_em = NULL;
7017 u64 range_start = start;
7018 u64 end;
7019 u64 found;
7020 u64 found_end;
7021 int err = 0;
7022
7023 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7024 if (IS_ERR(em))
7025 return em;
7026 if (em) {
7027 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007028 * if our em maps to
7029 * - a hole or
7030 * - a pre-alloc extent,
7031 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007032 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007033 if (em->block_start != EXTENT_MAP_HOLE &&
7034 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007035 return em;
7036 else
7037 hole_em = em;
7038 }
7039
7040 /* check to see if we've wrapped (len == -1 or similar) */
7041 end = start + len;
7042 if (end < start)
7043 end = (u64)-1;
7044 else
7045 end -= 1;
7046
7047 em = NULL;
7048
7049 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007050 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007051 end, len, EXTENT_DELALLOC, 1);
7052 found_end = range_start + found;
7053 if (found_end < range_start)
7054 found_end = (u64)-1;
7055
7056 /*
7057 * we didn't find anything useful, return
7058 * the original results from get_extent()
7059 */
7060 if (range_start > end || found_end <= start) {
7061 em = hole_em;
7062 hole_em = NULL;
7063 goto out;
7064 }
7065
7066 /* adjust the range_start to make sure it doesn't
7067 * go backwards from the start they passed in
7068 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307069 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007070 found = found_end - range_start;
7071
7072 if (found > 0) {
7073 u64 hole_start = start;
7074 u64 hole_len = len;
7075
David Sterba172ddd62011-04-21 00:48:27 +02007076 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007077 if (!em) {
7078 err = -ENOMEM;
7079 goto out;
7080 }
7081 /*
7082 * when btrfs_get_extent can't find anything it
7083 * returns one huge hole
7084 *
7085 * make sure what it found really fits our range, and
7086 * adjust to make sure it is based on the start from
7087 * the caller
7088 */
7089 if (hole_em) {
7090 u64 calc_end = extent_map_end(hole_em);
7091
7092 if (calc_end <= start || (hole_em->start > end)) {
7093 free_extent_map(hole_em);
7094 hole_em = NULL;
7095 } else {
7096 hole_start = max(hole_em->start, start);
7097 hole_len = calc_end - hole_start;
7098 }
7099 }
7100 em->bdev = NULL;
7101 if (hole_em && range_start > hole_start) {
7102 /* our hole starts before our delalloc, so we
7103 * have to return just the parts of the hole
7104 * that go until the delalloc starts
7105 */
7106 em->len = min(hole_len,
7107 range_start - hole_start);
7108 em->start = hole_start;
7109 em->orig_start = hole_start;
7110 /*
7111 * don't adjust block start at all,
7112 * it is fixed at EXTENT_MAP_HOLE
7113 */
7114 em->block_start = hole_em->block_start;
7115 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007116 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7117 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007118 } else {
7119 em->start = range_start;
7120 em->len = found;
7121 em->orig_start = range_start;
7122 em->block_start = EXTENT_MAP_DELALLOC;
7123 em->block_len = found;
7124 }
7125 } else if (hole_em) {
7126 return hole_em;
7127 }
7128out:
7129
7130 free_extent_map(hole_em);
7131 if (err) {
7132 free_extent_map(em);
7133 return ERR_PTR(err);
7134 }
7135 return em;
7136}
7137
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007138static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7139 const u64 start,
7140 const u64 len,
7141 const u64 orig_start,
7142 const u64 block_start,
7143 const u64 block_len,
7144 const u64 orig_block_len,
7145 const u64 ram_bytes,
7146 const int type)
7147{
7148 struct extent_map *em = NULL;
7149 int ret;
7150
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007151 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007152 em = create_io_em(inode, start, len, orig_start,
7153 block_start, block_len, orig_block_len,
7154 ram_bytes,
7155 BTRFS_COMPRESS_NONE, /* compress_type */
7156 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007157 if (IS_ERR(em))
7158 goto out;
7159 }
7160 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7161 len, block_len, type);
7162 if (ret) {
7163 if (em) {
7164 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007165 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007166 start + len - 1, 0);
7167 }
7168 em = ERR_PTR(ret);
7169 }
7170 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007171
7172 return em;
7173}
7174
Josef Bacik4b46fce2010-05-23 11:00:55 -04007175static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7176 u64 start, u64 len)
7177{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007178 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007179 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007180 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007181 struct btrfs_key ins;
7182 u64 alloc_hint;
7183 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007184
Josef Bacik4b46fce2010-05-23 11:00:55 -04007185 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007186 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007187 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007188 if (ret)
7189 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007190
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007191 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7192 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007193 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007194 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007195 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007196 btrfs_free_reserved_extent(fs_info, ins.objectid,
7197 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007198
Josef Bacik4b46fce2010-05-23 11:00:55 -04007199 return em;
7200}
7201
Chris Mason46bfbb52010-05-26 11:04:10 -04007202/*
7203 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7204 * block must be cow'd
7205 */
Josef Bacik00361582013-08-14 14:02:47 -04007206noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007207 u64 *orig_start, u64 *orig_block_len,
7208 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007209{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007210 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007211 struct btrfs_path *path;
7212 int ret;
7213 struct extent_buffer *leaf;
7214 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007215 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007216 struct btrfs_file_extent_item *fi;
7217 struct btrfs_key key;
7218 u64 disk_bytenr;
7219 u64 backref_offset;
7220 u64 extent_end;
7221 u64 num_bytes;
7222 int slot;
7223 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007224 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007225
Chris Mason46bfbb52010-05-26 11:04:10 -04007226 path = btrfs_alloc_path();
7227 if (!path)
7228 return -ENOMEM;
7229
David Sterbaf85b7372017-01-20 14:54:07 +01007230 ret = btrfs_lookup_file_extent(NULL, root, path,
7231 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007232 if (ret < 0)
7233 goto out;
7234
7235 slot = path->slots[0];
7236 if (ret == 1) {
7237 if (slot == 0) {
7238 /* can't find the item, must cow */
7239 ret = 0;
7240 goto out;
7241 }
7242 slot--;
7243 }
7244 ret = 0;
7245 leaf = path->nodes[0];
7246 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007247 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007248 key.type != BTRFS_EXTENT_DATA_KEY) {
7249 /* not our file or wrong item type, must cow */
7250 goto out;
7251 }
7252
7253 if (key.offset > offset) {
7254 /* Wrong offset, must cow */
7255 goto out;
7256 }
7257
7258 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7259 found_type = btrfs_file_extent_type(leaf, fi);
7260 if (found_type != BTRFS_FILE_EXTENT_REG &&
7261 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7262 /* not a regular extent, must cow */
7263 goto out;
7264 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007265
7266 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7267 goto out;
7268
Miao Xiee77751a2013-12-27 21:11:50 +08007269 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7270 if (extent_end <= offset)
7271 goto out;
7272
Chris Mason46bfbb52010-05-26 11:04:10 -04007273 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007274 if (disk_bytenr == 0)
7275 goto out;
7276
7277 if (btrfs_file_extent_compression(leaf, fi) ||
7278 btrfs_file_extent_encryption(leaf, fi) ||
7279 btrfs_file_extent_other_encoding(leaf, fi))
7280 goto out;
7281
Chris Mason46bfbb52010-05-26 11:04:10 -04007282 backref_offset = btrfs_file_extent_offset(leaf, fi);
7283
Josef Bacik7ee9e442013-06-21 16:37:03 -04007284 if (orig_start) {
7285 *orig_start = key.offset - backref_offset;
7286 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7287 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7288 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007289
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007290 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007291 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007292
7293 num_bytes = min(offset + *len, extent_end) - offset;
7294 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7295 u64 range_end;
7296
Jeff Mahoneyda170662016-06-15 09:22:56 -04007297 range_end = round_up(offset + num_bytes,
7298 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007299 ret = test_range_bit(io_tree, offset, range_end,
7300 EXTENT_DELALLOC, 0, NULL);
7301 if (ret) {
7302 ret = -EAGAIN;
7303 goto out;
7304 }
7305 }
7306
Josef Bacik1bda19e2013-10-18 12:10:36 -04007307 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007308
7309 /*
7310 * look for other files referencing this extent, if we
7311 * find any we must cow
7312 */
Josef Bacik00361582013-08-14 14:02:47 -04007313
Liu Boe4c3b2d2017-01-30 12:25:28 -08007314 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007315 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007316 if (ret) {
7317 ret = 0;
7318 goto out;
7319 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007320
7321 /*
7322 * adjust disk_bytenr and num_bytes to cover just the bytes
7323 * in this extent we are about to write. If there
7324 * are any csums in that range we have to cow in order
7325 * to keep the csums correct
7326 */
7327 disk_bytenr += backref_offset;
7328 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007329 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7330 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007331 /*
7332 * all of the above have passed, it is safe to overwrite this extent
7333 * without cow
7334 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007335 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007336 ret = 1;
7337out:
7338 btrfs_free_path(path);
7339 return ret;
7340}
7341
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007342bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7343{
7344 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7345 int found = false;
7346 void **pagep = NULL;
7347 struct page *page = NULL;
7348 int start_idx;
7349 int end_idx;
7350
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007351 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007352
7353 /*
7354 * end is the last byte in the last page. end == start is legal
7355 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007356 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007357
7358 rcu_read_lock();
7359
7360 /* Most of the code in this while loop is lifted from
7361 * find_get_page. It's been modified to begin searching from a
7362 * page and return just the first page found in that range. If the
7363 * found idx is less than or equal to the end idx then we know that
7364 * a page exists. If no pages are found or if those pages are
7365 * outside of the range then we're fine (yay!) */
7366 while (page == NULL &&
7367 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7368 page = radix_tree_deref_slot(pagep);
7369 if (unlikely(!page))
7370 break;
7371
7372 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007373 if (radix_tree_deref_retry(page)) {
7374 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007375 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007376 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007377 /*
7378 * Otherwise, shmem/tmpfs must be storing a swap entry
7379 * here as an exceptional entry: so return it without
7380 * attempting to raise page count.
7381 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007382 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007383 break; /* TODO: Is this relevant for this use case? */
7384 }
7385
Filipe Manana91405152014-06-05 13:22:24 +01007386 if (!page_cache_get_speculative(page)) {
7387 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007388 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007389 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007390
7391 /*
7392 * Has the page moved?
7393 * This is part of the lockless pagecache protocol. See
7394 * include/linux/pagemap.h for details.
7395 */
7396 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007397 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007398 page = NULL;
7399 }
7400 }
7401
7402 if (page) {
7403 if (page->index <= end_idx)
7404 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007405 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007406 }
7407
7408 rcu_read_unlock();
7409 return found;
7410}
7411
Josef Bacikeb838e72012-07-31 16:28:48 -04007412static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7413 struct extent_state **cached_state, int writing)
7414{
7415 struct btrfs_ordered_extent *ordered;
7416 int ret = 0;
7417
7418 while (1) {
7419 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007420 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007421 /*
7422 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007423 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007424 * extents in this range.
7425 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007426 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007427 lockend - lockstart + 1);
7428
7429 /*
7430 * We need to make sure there are no buffered pages in this
7431 * range either, we could have raced between the invalidate in
7432 * generic_file_direct_write and locking the extent. The
7433 * invalidate needs to happen so that reads after a write do not
7434 * get stale data.
7435 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007436 if (!ordered &&
7437 (!writing ||
7438 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007439 break;
7440
7441 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7442 cached_state, GFP_NOFS);
7443
7444 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007445 /*
7446 * If we are doing a DIO read and the ordered extent we
7447 * found is for a buffered write, we can not wait for it
7448 * to complete and retry, because if we do so we can
7449 * deadlock with concurrent buffered writes on page
7450 * locks. This happens only if our DIO read covers more
7451 * than one extent map, if at this point has already
7452 * created an ordered extent for a previous extent map
7453 * and locked its range in the inode's io tree, and a
7454 * concurrent write against that previous extent map's
7455 * range and this range started (we unlock the ranges
7456 * in the io tree only when the bios complete and
7457 * buffered writes always lock pages before attempting
7458 * to lock range in the io tree).
7459 */
7460 if (writing ||
7461 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7462 btrfs_start_ordered_extent(inode, ordered, 1);
7463 else
7464 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007465 btrfs_put_ordered_extent(ordered);
7466 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007467 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007468 * We could trigger writeback for this range (and wait
7469 * for it to complete) and then invalidate the pages for
7470 * this range (through invalidate_inode_pages2_range()),
7471 * but that can lead us to a deadlock with a concurrent
7472 * call to readpages() (a buffered read or a defrag call
7473 * triggered a readahead) on a page lock due to an
7474 * ordered dio extent we created before but did not have
7475 * yet a corresponding bio submitted (whence it can not
7476 * complete), which makes readpages() wait for that
7477 * ordered extent to complete while holding a lock on
7478 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007479 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007480 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007481 }
7482
Filipe Mananaade77022016-02-18 14:28:55 +00007483 if (ret)
7484 break;
7485
Josef Bacikeb838e72012-07-31 16:28:48 -04007486 cond_resched();
7487 }
7488
7489 return ret;
7490}
7491
Liu Bo6f9994d2017-01-31 07:50:22 -08007492/* The callers of this must take lock_extent() */
7493static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7494 u64 orig_start, u64 block_start,
7495 u64 block_len, u64 orig_block_len,
7496 u64 ram_bytes, int compress_type,
7497 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007498{
7499 struct extent_map_tree *em_tree;
7500 struct extent_map *em;
7501 struct btrfs_root *root = BTRFS_I(inode)->root;
7502 int ret;
7503
Liu Bo6f9994d2017-01-31 07:50:22 -08007504 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7505 type == BTRFS_ORDERED_COMPRESSED ||
7506 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007507 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007508
Josef Bacik69ffb542012-09-11 15:40:07 -04007509 em_tree = &BTRFS_I(inode)->extent_tree;
7510 em = alloc_extent_map();
7511 if (!em)
7512 return ERR_PTR(-ENOMEM);
7513
7514 em->start = start;
7515 em->orig_start = orig_start;
7516 em->len = len;
7517 em->block_len = block_len;
7518 em->block_start = block_start;
7519 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007520 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007521 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007522 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007523 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007524 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007525 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007526 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007527 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7528 em->compress_type = compress_type;
7529 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007530
7531 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007532 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007533 em->start + em->len - 1, 0);
7534 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007535 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007536 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007537 /*
7538 * The caller has taken lock_extent(), who could race with us
7539 * to add em?
7540 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007541 } while (ret == -EEXIST);
7542
7543 if (ret) {
7544 free_extent_map(em);
7545 return ERR_PTR(ret);
7546 }
7547
Liu Bo6f9994d2017-01-31 07:50:22 -08007548 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007549 return em;
7550}
7551
Filipe Manana9c9464c2015-11-04 09:52:04 +00007552static void adjust_dio_outstanding_extents(struct inode *inode,
7553 struct btrfs_dio_data *dio_data,
7554 const u64 len)
7555{
David Sterba823bb202017-01-04 11:09:51 +01007556 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007557
Filipe Manana9c9464c2015-11-04 09:52:04 +00007558 /*
7559 * If we have an outstanding_extents count still set then we're
7560 * within our reservation, otherwise we need to adjust our inode
7561 * counter appropriately.
7562 */
Liu Boc2931662016-12-22 17:13:54 -08007563 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007564 dio_data->outstanding_extents -= num_extents;
7565 } else {
Liu Boc2931662016-12-22 17:13:54 -08007566 /*
7567 * If dio write length has been split due to no large enough
7568 * contiguous space, we need to compensate our inode counter
7569 * appropriately.
7570 */
7571 u64 num_needed = num_extents - dio_data->outstanding_extents;
7572
Filipe Manana9c9464c2015-11-04 09:52:04 +00007573 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007574 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007575 spin_unlock(&BTRFS_I(inode)->lock);
7576 }
7577}
7578
Josef Bacik4b46fce2010-05-23 11:00:55 -04007579static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7580 struct buffer_head *bh_result, int create)
7581{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007582 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007583 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007584 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307585 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007586 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007587 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007588 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007589 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007590 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007591
Miao Xie172a5042013-02-21 02:48:22 -07007592 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007593 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007594 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007595 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007596
Josef Bacikc3298612012-08-03 16:49:19 -04007597 lockstart = start;
7598 lockend = start + len - 1;
7599
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007600 if (current->journal_info) {
7601 /*
7602 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007603 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007604 * confused.
7605 */
chandan50745b02015-08-28 21:10:13 +05307606 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007607 current->journal_info = NULL;
7608 }
7609
Josef Bacikeb838e72012-07-31 16:28:48 -04007610 /*
7611 * If this errors out it's because we couldn't invalidate pagecache for
7612 * this range and we need to fallback to buffered.
7613 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007614 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7615 create)) {
7616 ret = -ENOTBLK;
7617 goto err;
7618 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007619
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007620 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007621 if (IS_ERR(em)) {
7622 ret = PTR_ERR(em);
7623 goto unlock_err;
7624 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007625
7626 /*
7627 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7628 * io. INLINE is special, and we could probably kludge it in here, but
7629 * it's still buffered so for safety lets just fall back to the generic
7630 * buffered path.
7631 *
7632 * For COMPRESSED we _have_ to read the entire extent in so we can
7633 * decompress it, so there will be buffering required no matter what we
7634 * do, so go ahead and fallback to buffered.
7635 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007636 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007637 * to buffered IO. Don't blame me, this is the price we pay for using
7638 * the generic code.
7639 */
7640 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7641 em->block_start == EXTENT_MAP_INLINE) {
7642 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007643 ret = -ENOTBLK;
7644 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007645 }
7646
7647 /* Just a good old fashioned hole, return */
7648 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7649 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7650 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007651 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007652 }
7653
7654 /*
7655 * We don't allocate a new extent in the following cases
7656 *
7657 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7658 * existing extent.
7659 * 2) The extent is marked as PREALLOC. We're good to go here and can
7660 * just use the extent.
7661 *
7662 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007663 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007664 len = min(len, em->len - (start - em->start));
7665 lockstart = start + len;
7666 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007667 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007668
7669 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7670 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7671 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007672 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007673 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007674
7675 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7676 type = BTRFS_ORDERED_PREALLOC;
7677 else
7678 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007679 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007680 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007681
Josef Bacik00361582013-08-14 14:02:47 -04007682 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007683 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007684 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007685 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007686
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007687 em2 = btrfs_create_dio_extent(inode, start, len,
7688 orig_start, block_start,
7689 len, orig_block_len,
7690 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007691 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007692 if (type == BTRFS_ORDERED_PREALLOC) {
7693 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007694 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007695 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007696 if (em2 && IS_ERR(em2)) {
7697 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007698 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007699 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007700 /*
7701 * For inode marked NODATACOW or extent marked PREALLOC,
7702 * use the existing or preallocated extent, so does not
7703 * need to adjust btrfs_space_info's bytes_may_use.
7704 */
7705 btrfs_free_reserved_data_space_noquota(inode,
7706 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007707 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007708 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007709 }
Josef Bacik00361582013-08-14 14:02:47 -04007710
Chris Mason46bfbb52010-05-26 11:04:10 -04007711 /*
7712 * this will cow the extent, reset the len in case we changed
7713 * it above
7714 */
7715 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007716 free_extent_map(em);
7717 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007718 if (IS_ERR(em)) {
7719 ret = PTR_ERR(em);
7720 goto unlock_err;
7721 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007722 len = min(len, em->len - (start - em->start));
7723unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007724 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7725 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007726 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007727 bh_result->b_bdev = em->bdev;
7728 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007729 if (create) {
7730 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7731 set_buffer_new(bh_result);
7732
7733 /*
7734 * Need to update the i_size under the extent lock so buffered
7735 * readers will get the updated i_size when we unlock.
7736 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007737 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007738 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007739
Filipe Manana9c9464c2015-11-04 09:52:04 +00007740 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307741 WARN_ON(dio_data->reserve < len);
7742 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007743 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307744 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007745 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007746
Josef Bacikeb838e72012-07-31 16:28:48 -04007747 /*
7748 * In the case of write we need to clear and unlock the entire range,
7749 * in the case of read we need to unlock only the end area that we
7750 * aren't using if there is any left over space.
7751 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007752 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007753 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7754 lockend, unlock_bits, 1, 0,
7755 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007756 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007757 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007758 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007759
Josef Bacik4b46fce2010-05-23 11:00:55 -04007760 free_extent_map(em);
7761
7762 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007763
7764unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007765 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7766 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007767err:
chandan50745b02015-08-28 21:10:13 +05307768 if (dio_data)
7769 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007770 /*
7771 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7772 * write less data then expected, so that we don't underflow our inode's
7773 * outstanding extents counter.
7774 */
7775 if (create && dio_data)
7776 adjust_dio_outstanding_extents(inode, dio_data, len);
7777
Josef Bacikeb838e72012-07-31 16:28:48 -04007778 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007779}
7780
Miao Xie8b110e32014-09-12 18:44:03 +08007781static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007782 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007783{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007784 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007785 int ret;
7786
Mike Christie37226b22016-06-05 14:31:52 -05007787 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007788
7789 bio_get(bio);
7790
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007791 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007792 if (ret)
7793 goto err;
7794
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007795 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007796err:
7797 bio_put(bio);
7798 return ret;
7799}
7800
7801static int btrfs_check_dio_repairable(struct inode *inode,
7802 struct bio *failed_bio,
7803 struct io_failure_record *failrec,
7804 int failed_mirror)
7805{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007806 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007807 int num_copies;
7808
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007809 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007810 if (num_copies == 1) {
7811 /*
7812 * we only have a single copy of the data, so don't bother with
7813 * all the retry and error correction code that follows. no
7814 * matter what the error is, it is very likely to persist.
7815 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007816 btrfs_debug(fs_info,
7817 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7818 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007819 return 0;
7820 }
7821
7822 failrec->failed_mirror = failed_mirror;
7823 failrec->this_mirror++;
7824 if (failrec->this_mirror == failed_mirror)
7825 failrec->this_mirror++;
7826
7827 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007828 btrfs_debug(fs_info,
7829 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7830 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007831 return 0;
7832 }
7833
7834 return 1;
7835}
7836
7837static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307838 struct page *page, unsigned int pgoff,
7839 u64 start, u64 end, int failed_mirror,
7840 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007841{
7842 struct io_failure_record *failrec;
7843 struct bio *bio;
7844 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007845 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007846 int ret;
7847
Mike Christie37226b22016-06-05 14:31:52 -05007848 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007849
7850 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7851 if (ret)
7852 return ret;
7853
7854 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7855 failed_mirror);
7856 if (!ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007857 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007858 return -EIO;
7859 }
7860
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307861 if ((failed_bio->bi_vcnt > 1)
7862 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007863 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007864 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007865
7866 isector = start - btrfs_io_bio(failed_bio)->logical;
7867 isector >>= inode->i_sb->s_blocksize_bits;
7868 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307869 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007870 if (!bio) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007871 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007872 return -EIO;
7873 }
Mike Christie37226b22016-06-05 14:31:52 -05007874 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007875
7876 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7877 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7878 read_mode, failrec->this_mirror, failrec->in_validation);
7879
Mike Christie81a75f672016-06-05 14:31:54 -05007880 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007881 if (ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007882 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007883 bio_put(bio);
7884 }
7885
7886 return ret;
7887}
7888
7889struct btrfs_retry_complete {
7890 struct completion done;
7891 struct inode *inode;
7892 u64 start;
7893 int uptodate;
7894};
7895
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007896static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007897{
7898 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307899 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007900 struct bio_vec *bvec;
7901 int i;
7902
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007903 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007904 goto end;
7905
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307906 ASSERT(bio->bi_vcnt == 1);
7907 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007908 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307909
Miao Xie8b110e32014-09-12 18:44:03 +08007910 done->uptodate = 1;
7911 bio_for_each_segment_all(bvec, bio, i)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02007912 clean_io_failure(BTRFS_I(done->inode), done->start, bvec->bv_page, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007913end:
7914 complete(&done->done);
7915 bio_put(bio);
7916}
7917
7918static int __btrfs_correct_data_nocsum(struct inode *inode,
7919 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007920{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307921 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007922 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007923 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007924 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307925 unsigned int pgoff;
7926 u32 sectorsize;
7927 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007928 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007929 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007930
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307931 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007932 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307933
Miao Xiec1dc0892014-09-12 18:43:56 +08007934 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007935 done.inode = inode;
7936
7937 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307938 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7939 pgoff = bvec->bv_offset;
7940
7941next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007942 done.uptodate = 0;
7943 done.start = start;
7944 init_completion(&done.done);
7945
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307946 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7947 pgoff, start, start + sectorsize - 1,
7948 io_bio->mirror_num,
7949 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007950 if (ret)
7951 return ret;
7952
7953 wait_for_completion(&done.done);
7954
7955 if (!done.uptodate) {
7956 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307957 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007958 }
7959
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307960 start += sectorsize;
7961
7962 if (nr_sectors--) {
7963 pgoff += sectorsize;
7964 goto next_block_or_try_again;
7965 }
Miao Xie8b110e32014-09-12 18:44:03 +08007966 }
7967
7968 return 0;
7969}
7970
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007971static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007972{
7973 struct btrfs_retry_complete *done = bio->bi_private;
7974 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307975 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007976 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307977 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08007978 int uptodate;
7979 int ret;
7980 int i;
7981
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007982 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007983 goto end;
7984
7985 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307986
7987 start = done->start;
7988
7989 ASSERT(bio->bi_vcnt == 1);
7990 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007991 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307992
Miao Xie8b110e32014-09-12 18:44:03 +08007993 bio_for_each_segment_all(bvec, bio, i) {
7994 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307995 bvec->bv_page, bvec->bv_offset,
7996 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007997 if (!ret)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02007998 clean_io_failure(BTRFS_I(done->inode), done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307999 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008000 else
8001 uptodate = 0;
8002 }
8003
8004 done->uptodate = uptodate;
8005end:
8006 complete(&done->done);
8007 bio_put(bio);
8008}
8009
8010static int __btrfs_subio_endio_read(struct inode *inode,
8011 struct btrfs_io_bio *io_bio, int err)
8012{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308013 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008014 struct bio_vec *bvec;
8015 struct btrfs_retry_complete done;
8016 u64 start;
8017 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308018 u32 sectorsize;
8019 int nr_sectors;
8020 unsigned int pgoff;
8021 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008022 int i;
8023 int ret;
8024
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308025 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008026 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308027
Miao Xie8b110e32014-09-12 18:44:03 +08008028 err = 0;
8029 start = io_bio->logical;
8030 done.inode = inode;
8031
Miao Xiec1dc0892014-09-12 18:43:56 +08008032 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308033 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8034
8035 pgoff = bvec->bv_offset;
8036next_block:
8037 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8038 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8039 bvec->bv_page, pgoff, start,
8040 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008041 if (likely(!ret))
8042 goto next;
8043try_again:
8044 done.uptodate = 0;
8045 done.start = start;
8046 init_completion(&done.done);
8047
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308048 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8049 pgoff, start, start + sectorsize - 1,
8050 io_bio->mirror_num,
8051 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008052 if (ret) {
8053 err = ret;
8054 goto next;
8055 }
8056
8057 wait_for_completion(&done.done);
8058
8059 if (!done.uptodate) {
8060 /* We might have another mirror, so try again */
8061 goto try_again;
8062 }
8063next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308064 offset += sectorsize;
8065 start += sectorsize;
8066
8067 ASSERT(nr_sectors);
8068
8069 if (--nr_sectors) {
8070 pgoff += sectorsize;
8071 goto next_block;
8072 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008073 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008074
8075 return err;
8076}
8077
Miao Xie8b110e32014-09-12 18:44:03 +08008078static int btrfs_subio_endio_read(struct inode *inode,
8079 struct btrfs_io_bio *io_bio, int err)
8080{
8081 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8082
8083 if (skip_csum) {
8084 if (unlikely(err))
8085 return __btrfs_correct_data_nocsum(inode, io_bio);
8086 else
8087 return 0;
8088 } else {
8089 return __btrfs_subio_endio_read(inode, io_bio, err);
8090 }
8091}
8092
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008093static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008094{
8095 struct btrfs_dio_private *dip = bio->bi_private;
8096 struct inode *inode = dip->inode;
8097 struct bio *dio_bio;
8098 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008099 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008100
Miao Xie8b110e32014-09-12 18:44:03 +08008101 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8102 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008103
Josef Bacik4b46fce2010-05-23 11:00:55 -04008104 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008105 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008106 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008107
Josef Bacik4b46fce2010-05-23 11:00:55 -04008108 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008109
Filipe Manana1636d1d2016-02-15 16:20:26 +00008110 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008111 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008112
8113 if (io_bio->end_io)
8114 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008115 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008116}
8117
Filipe Manana14543772015-11-24 16:23:54 +00008118static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8119 const u64 offset,
8120 const u64 bytes,
8121 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008122{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008123 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008124 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008125 u64 ordered_offset = offset;
8126 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008127 int ret;
8128
Chris Mason163cf092010-11-28 19:56:33 -05008129again:
8130 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8131 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008132 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008133 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008134 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008135 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008136
Liu Bo9e0af232014-08-15 23:36:53 +08008137 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8138 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008139 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008140out_test:
8141 /*
8142 * our bio might span multiple ordered extents. If we haven't
8143 * completed the accounting for the whole dio, go back and try again
8144 */
Filipe Manana14543772015-11-24 16:23:54 +00008145 if (ordered_offset < offset + bytes) {
8146 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008147 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008148 goto again;
8149 }
Filipe Manana14543772015-11-24 16:23:54 +00008150}
8151
8152static void btrfs_endio_direct_write(struct bio *bio)
8153{
8154 struct btrfs_dio_private *dip = bio->bi_private;
8155 struct bio *dio_bio = dip->dio_bio;
8156
8157 btrfs_endio_direct_write_update_ordered(dip->inode,
8158 dip->logical_offset,
8159 dip->bytes,
8160 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008161
Josef Bacik4b46fce2010-05-23 11:00:55 -04008162 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008163
Filipe Manana1636d1d2016-02-15 16:20:26 +00008164 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008165 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008166 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008167}
8168
Mike Christie81a75f672016-06-05 14:31:54 -05008169static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008170 struct bio *bio, int mirror_num,
8171 unsigned long bio_flags, u64 offset)
8172{
8173 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008174 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008175 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008176 return 0;
8177}
8178
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008179static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008180{
8181 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008182 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008183
Miao Xie8b110e32014-09-12 18:44:03 +08008184 if (err)
8185 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008186 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008187 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8188 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008189 (unsigned long long)bio->bi_iter.bi_sector,
8190 bio->bi_iter.bi_size, err);
8191
8192 if (dip->subio_endio)
8193 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008194
8195 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008196 dip->errors = 1;
8197
8198 /*
8199 * before atomic variable goto zero, we must make sure
8200 * dip->errors is perceived to be set.
8201 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008202 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008203 }
8204
8205 /* if there are more bios still pending for this dio, just exit */
8206 if (!atomic_dec_and_test(&dip->pending_bios))
8207 goto out;
8208
Chris Mason9be33952013-05-17 18:30:14 -04008209 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008210 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008211 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008212 dip->dio_bio->bi_error = 0;
8213 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008214 }
8215out:
8216 bio_put(bio);
8217}
8218
8219static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8220 u64 first_sector, gfp_t gfp_flags)
8221{
Chris Masonda2f0f72015-07-02 13:57:22 -07008222 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008223 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008224 if (bio)
8225 bio_associate_current(bio);
8226 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008227}
8228
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008229static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008230 struct btrfs_dio_private *dip,
8231 struct bio *bio,
8232 u64 file_offset)
8233{
8234 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8235 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8236 int ret;
8237
8238 /*
8239 * We load all the csum data we need when we submit
8240 * the first bio to reduce the csum tree search and
8241 * contention.
8242 */
8243 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008244 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008245 file_offset);
8246 if (ret)
8247 return ret;
8248 }
8249
8250 if (bio == dip->orig_bio)
8251 return 0;
8252
8253 file_offset -= dip->logical_offset;
8254 file_offset >>= inode->i_sb->s_blocksize_bits;
8255 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8256
8257 return 0;
8258}
8259
Miao Xiee65e1532010-11-22 03:04:43 +00008260static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008261 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008262 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008264 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008265 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008266 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008267 int ret;
8268
Josef Bacikb812ce22012-11-16 13:56:32 -05008269 if (async_submit)
8270 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8271
Miao Xiee65e1532010-11-22 03:04:43 +00008272 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008273
8274 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008275 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008276 if (ret)
8277 goto err;
8278 }
Miao Xiee65e1532010-11-22 03:04:43 +00008279
Josef Bacik1ae39932011-04-06 14:41:34 -04008280 if (skip_sum)
8281 goto map;
8282
8283 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008284 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8285 file_offset,
8286 __btrfs_submit_bio_start_direct_io,
8287 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008288 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008289 } else if (write) {
8290 /*
8291 * If we aren't doing async submit, calculate the csum of the
8292 * bio now.
8293 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008294 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008295 if (ret)
8296 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008297 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008298 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008299 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008300 if (ret)
8301 goto err;
8302 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008303map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008304 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008305err:
8306 bio_put(bio);
8307 return ret;
8308}
8309
Mike Christie81a75f672016-06-05 14:31:54 -05008310static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008311 int skip_sum)
8312{
8313 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008314 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008315 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008316 struct bio *bio;
8317 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008318 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008319 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008320 u64 file_offset = dip->logical_offset;
8321 u64 submit_len = 0;
8322 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008323 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008324 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308325 int nr_sectors;
8326 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008327 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008328
Kent Overstreet4f024f32013-10-11 15:44:27 -07008329 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008330 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8331 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008332 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008333 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008334
Kent Overstreet4f024f32013-10-11 15:44:27 -07008335 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008336 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008337 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008338 goto submit;
8339 }
8340
David Woodhouse53b381b2013-01-29 18:40:14 -05008341 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008342 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008343 async_submit = 0;
8344 else
8345 async_submit = 1;
8346
Josef Bacik02f57c72011-04-06 14:25:44 -04008347 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8348 if (!bio)
8349 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008350
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008351 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008352 bio->bi_private = dip;
8353 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008354 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008355 atomic_inc(&dip->pending_bios);
8356
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008357 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008358 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308359 i = 0;
8360next_block:
8361 if (unlikely(map_length < submit_len + blocksize ||
8362 bio_add_page(bio, bvec->bv_page, blocksize,
8363 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008364 /*
8365 * inc the count before we submit the bio so
8366 * we know the end IO handler won't happen before
8367 * we inc the count. Otherwise, the dip might get freed
8368 * before we're done setting it up
8369 */
8370 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008371 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008372 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008373 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008374 if (ret) {
8375 bio_put(bio);
8376 atomic_dec(&dip->pending_bios);
8377 goto out_err;
8378 }
8379
Miao Xiee65e1532010-11-22 03:04:43 +00008380 start_sector += submit_len >> 9;
8381 file_offset += submit_len;
8382
8383 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008384
8385 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8386 start_sector, GFP_NOFS);
8387 if (!bio)
8388 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008389 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008390 bio->bi_private = dip;
8391 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008392 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008393
Kent Overstreet4f024f32013-10-11 15:44:27 -07008394 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008395 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008396 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008397 &map_length, NULL, 0);
8398 if (ret) {
8399 bio_put(bio);
8400 goto out_err;
8401 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308402
8403 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008404 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308405 submit_len += blocksize;
8406 if (--nr_sectors) {
8407 i++;
8408 goto next_block;
8409 }
Miao Xiee65e1532010-11-22 03:04:43 +00008410 }
8411 }
8412
Josef Bacik02f57c72011-04-06 14:25:44 -04008413submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008414 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008415 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008416 if (!ret)
8417 return 0;
8418
8419 bio_put(bio);
8420out_err:
8421 dip->errors = 1;
8422 /*
8423 * before atomic variable goto zero, we must
8424 * make sure dip->errors is perceived to be set.
8425 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008426 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008427 if (atomic_dec_and_test(&dip->pending_bios))
8428 bio_io_error(dip->orig_bio);
8429
8430 /* bio_end_io() will handle error, so we needn't return it */
8431 return 0;
8432}
8433
Mike Christie8a4c1e42016-06-05 14:31:50 -05008434static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8435 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008436{
Filipe Manana61de7182015-07-01 12:13:10 +01008437 struct btrfs_dio_private *dip = NULL;
8438 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008439 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008440 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008441 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008442 int ret = 0;
8443
8444 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8445
Chris Mason9be33952013-05-17 18:30:14 -04008446 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008447 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008448 ret = -ENOMEM;
8449 goto free_ordered;
8450 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008451
Miao Xiec1dc0892014-09-12 18:43:56 +08008452 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008453 if (!dip) {
8454 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008455 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008456 }
8457
8458 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008459 dip->inode = inode;
8460 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008461 dip->bytes = dio_bio->bi_iter.bi_size;
8462 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008463 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008464 dip->orig_bio = io_bio;
8465 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008466 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008467 btrfs_bio = btrfs_io_bio(io_bio);
8468 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008469
Miao Xiec1dc0892014-09-12 18:43:56 +08008470 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008471 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008472 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008473 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008474 dip->subio_endio = btrfs_subio_endio_read;
8475 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008476
Filipe Mananaf28a4922015-12-08 19:23:20 +00008477 /*
8478 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8479 * even if we fail to submit a bio, because in such case we do the
8480 * corresponding error handling below and it must not be done a second
8481 * time by btrfs_direct_IO().
8482 */
8483 if (write) {
8484 struct btrfs_dio_data *dio_data = current->journal_info;
8485
8486 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8487 dip->bytes;
8488 dio_data->unsubmitted_oe_range_start =
8489 dio_data->unsubmitted_oe_range_end;
8490 }
8491
Mike Christie81a75f672016-06-05 14:31:54 -05008492 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008493 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008494 return;
Chris Mason9be33952013-05-17 18:30:14 -04008495
Miao Xie23ea8e52014-09-12 18:43:54 +08008496 if (btrfs_bio->end_io)
8497 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008498
Josef Bacik4b46fce2010-05-23 11:00:55 -04008499free_ordered:
8500 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008501 * If we arrived here it means either we failed to submit the dip
8502 * or we either failed to clone the dio_bio or failed to allocate the
8503 * dip. If we cloned the dio_bio and allocated the dip, we can just
8504 * call bio_endio against our io_bio so that we get proper resource
8505 * cleanup if we fail to submit the dip, otherwise, we must do the
8506 * same as btrfs_endio_direct_[write|read] because we can't call these
8507 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008508 */
Filipe Manana61de7182015-07-01 12:13:10 +01008509 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008510 io_bio->bi_error = -EIO;
8511 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008512 /*
8513 * The end io callbacks free our dip, do the final put on io_bio
8514 * and all the cleanup and final put for dio_bio (through
8515 * dio_end_io()).
8516 */
8517 dip = NULL;
8518 io_bio = NULL;
8519 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008520 if (write)
8521 btrfs_endio_direct_write_update_ordered(inode,
8522 file_offset,
8523 dio_bio->bi_iter.bi_size,
8524 0);
8525 else
Filipe Manana61de7182015-07-01 12:13:10 +01008526 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8527 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008528
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008529 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008530 /*
8531 * Releases and cleans up our dio_bio, no need to bio_put()
8532 * nor bio_endio()/bio_io_error() against dio_bio.
8533 */
8534 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008535 }
Filipe Manana61de7182015-07-01 12:13:10 +01008536 if (io_bio)
8537 bio_put(io_bio);
8538 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008539}
8540
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008541static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8542 struct kiocb *iocb,
8543 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008544{
8545 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008546 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008547 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008548 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008549
8550 if (offset & blocksize_mask)
8551 goto out;
8552
Al Viro28060d52014-03-22 05:15:17 -04008553 if (iov_iter_alignment(iter) & blocksize_mask)
8554 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008555
Al Viro28060d52014-03-22 05:15:17 -04008556 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008557 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008558 return 0;
8559 /*
8560 * Check to make sure we don't have duplicate iov_base's in this
8561 * iovec, if so return EINVAL, otherwise we'll get csum errors
8562 * when reading back.
8563 */
8564 for (seg = 0; seg < iter->nr_segs; seg++) {
8565 for (i = seg + 1; i < iter->nr_segs; i++) {
8566 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008567 goto out;
8568 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008569 }
8570 retval = 0;
8571out:
8572 return retval;
8573}
Josef Bacikeb838e72012-07-31 16:28:48 -04008574
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008575static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008576{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008577 struct file *file = iocb->ki_filp;
8578 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008579 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308580 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008581 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008582 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008583 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008584 bool wakeup = true;
8585 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008586 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008587
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008588 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008589 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008590
Jens Axboefe0f07d2015-04-15 17:05:48 -06008591 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008592 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008593
Josef Bacik0e267c42013-07-02 10:38:02 -04008594 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008595 * The generic stuff only does filemap_write_and_wait_range, which
8596 * isn't enough if we've written compressed pages to this area, so
8597 * we need to flush the dirty pages again to make absolutely sure
8598 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008599 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008600 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008601 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8602 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008603 filemap_fdatawrite_range(inode->i_mapping, offset,
8604 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008605
Omar Sandoval6f673762015-03-16 04:33:52 -07008606 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008607 /*
8608 * If the write DIO is beyond the EOF, we need update
8609 * the isize, but it is protected by i_mutex. So we can
8610 * not unlock the i_mutex at this case.
8611 */
8612 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008613 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008614 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008615 relock = true;
8616 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008617 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008618 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008619 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008620 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008621
8622 /*
8623 * We need to know how many extents we reserved so that we can
8624 * do the accounting properly if we go over the number we
8625 * originally calculated. Abuse current->journal_info for this.
8626 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008627 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008628 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008629 dio_data.unsubmitted_oe_range_start = (u64)offset;
8630 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308631 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308632 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008633 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8634 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008635 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008636 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8637 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008638 }
8639
Omar Sandoval17f8c842015-03-16 04:33:50 -07008640 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008641 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008642 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008643 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008644 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308645 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008646 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008647 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308648 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008649 btrfs_delalloc_release_space(inode, offset,
8650 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008651 /*
8652 * On error we might have left some ordered extents
8653 * without submitting corresponding bios for them, so
8654 * cleanup them up to avoid other tasks getting them
8655 * and waiting for them to complete forever.
8656 */
8657 if (dio_data.unsubmitted_oe_range_start <
8658 dio_data.unsubmitted_oe_range_end)
8659 btrfs_endio_direct_write_update_ordered(inode,
8660 dio_data.unsubmitted_oe_range_start,
8661 dio_data.unsubmitted_oe_range_end -
8662 dio_data.unsubmitted_oe_range_start,
8663 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008664 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008665 btrfs_delalloc_release_space(inode, offset,
8666 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008667 }
Miao Xie38851cc2013-02-08 07:04:11 +00008668out:
Miao Xie2e60a512013-02-08 07:01:08 +00008669 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008670 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008671 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008672 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008673
8674 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008675}
8676
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008677#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8678
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008679static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8680 __u64 start, __u64 len)
8681{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008682 int ret;
8683
8684 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8685 if (ret)
8686 return ret;
8687
Chris Masonec29ed52011-02-23 16:23:20 -05008688 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008689}
8690
Chris Mason9ebefb182007-06-15 13:50:00 -04008691int btrfs_readpage(struct file *file, struct page *page)
8692{
Chris Masond1310b22008-01-24 16:13:08 -05008693 struct extent_io_tree *tree;
8694 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008695 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008696}
Chris Mason1832a6d2007-12-21 16:27:21 -05008697
Chris Mason39279cc2007-06-12 06:35:45 -04008698static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8699{
Chris Masond1310b22008-01-24 16:13:08 -05008700 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008701 struct inode *inode = page->mapping->host;
8702 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008703
8704 if (current->flags & PF_MEMALLOC) {
8705 redirty_page_for_writepage(wbc, page);
8706 unlock_page(page);
8707 return 0;
8708 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008709
8710 /*
8711 * If we are under memory pressure we will call this directly from the
8712 * VM, we need to make sure we have the inode referenced for the ordered
8713 * extent. If not just return like we didn't do anything.
8714 */
8715 if (!igrab(inode)) {
8716 redirty_page_for_writepage(wbc, page);
8717 return AOP_WRITEPAGE_ACTIVATE;
8718 }
Chris Masond1310b22008-01-24 16:13:08 -05008719 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008720 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8721 btrfs_add_delayed_iput(inode);
8722 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008723}
Chris Mason39279cc2007-06-12 06:35:45 -04008724
Eric Sandeen48a3b632013-04-25 20:41:01 +00008725static int btrfs_writepages(struct address_space *mapping,
8726 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008727{
Chris Masond1310b22008-01-24 16:13:08 -05008728 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008729
Chris Masond1310b22008-01-24 16:13:08 -05008730 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008731 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8732}
8733
Chris Mason3ab2fb52007-11-08 10:59:22 -05008734static int
8735btrfs_readpages(struct file *file, struct address_space *mapping,
8736 struct list_head *pages, unsigned nr_pages)
8737{
Chris Masond1310b22008-01-24 16:13:08 -05008738 struct extent_io_tree *tree;
8739 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008740 return extent_readpages(tree, mapping, pages, nr_pages,
8741 btrfs_get_extent);
8742}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008743static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008744{
Chris Masond1310b22008-01-24 16:13:08 -05008745 struct extent_io_tree *tree;
8746 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008747 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008748
Chris Masond1310b22008-01-24 16:13:08 -05008749 tree = &BTRFS_I(page->mapping->host)->io_tree;
8750 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008751 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008752 if (ret == 1) {
8753 ClearPagePrivate(page);
8754 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008755 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008756 }
8757 return ret;
8758}
Chris Mason39279cc2007-06-12 06:35:45 -04008759
Chris Masone6dcd2d2008-07-17 12:53:50 -04008760static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8761{
Chris Mason98509cf2008-09-11 15:51:43 -04008762 if (PageWriteback(page) || PageDirty(page))
8763 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008764 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008765}
8766
Lukas Czernerd47992f2013-05-21 23:17:23 -04008767static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8768 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008769{
Josef Bacik5fd02042012-05-02 14:00:54 -04008770 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008771 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008772 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008773 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008774 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008775 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308776 u64 start;
8777 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008778 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008779
Chris Mason8b62b722009-09-02 16:53:46 -04008780 /*
8781 * we have the page locked, so new writeback can't start,
8782 * and the dirty bit won't be cleared while we are here.
8783 *
8784 * Wait for IO on this page so that we can safely clear
8785 * the PagePrivate2 bit and do ordered accounting
8786 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008787 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008788
Josef Bacik5fd02042012-05-02 14:00:54 -04008789 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008790 if (offset) {
8791 btrfs_releasepage(page, GFP_NOFS);
8792 return;
8793 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008794
8795 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008796 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308797again:
8798 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008799 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308800 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008801 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308802 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008803 /*
8804 * IO on this page will never be started, so we need
8805 * to account for any ordered extents now
8806 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008807 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308808 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008809 EXTENT_DIRTY | EXTENT_DELALLOC |
8810 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8811 EXTENT_DEFRAG, 1, 0, &cached_state,
8812 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008813 /*
8814 * whoever cleared the private bit is responsible
8815 * for the finish_ordered_io
8816 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008817 if (TestClearPagePrivate2(page)) {
8818 struct btrfs_ordered_inode_tree *tree;
8819 u64 new_len;
8820
8821 tree = &BTRFS_I(inode)->ordered_tree;
8822
8823 spin_lock_irq(&tree->lock);
8824 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308825 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008826 if (new_len < ordered->truncated_len)
8827 ordered->truncated_len = new_len;
8828 spin_unlock_irq(&tree->lock);
8829
8830 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308831 start,
8832 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008833 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008834 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008835 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008836 if (!inode_evicting) {
8837 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308838 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008839 &cached_state);
8840 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308841
8842 start = end + 1;
8843 if (start < page_end)
8844 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008845 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008846
Qu Wenruob9d0b382015-09-29 10:35:16 +08008847 /*
8848 * Qgroup reserved space handler
8849 * Page here will be either
8850 * 1) Already written to disk
8851 * In this case, its reserved space is released from data rsv map
8852 * and will be freed by delayed_ref handler finally.
8853 * So even we call qgroup_free_data(), it won't decrease reserved
8854 * space.
8855 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008856 * This means the reserved space should be freed here. However,
8857 * if a truncate invalidates the page (by clearing PageDirty)
8858 * and the page is accounted for while allocating extent
8859 * in btrfs_check_data_free_space() we let delayed_ref to
8860 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008861 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008862 if (PageDirty(page))
8863 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008864 if (!inode_evicting) {
8865 clear_extent_bit(tree, page_start, page_end,
8866 EXTENT_LOCKED | EXTENT_DIRTY |
8867 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8868 EXTENT_DEFRAG, 1, 1,
8869 &cached_state, GFP_NOFS);
8870
8871 __btrfs_releasepage(page, GFP_NOFS);
8872 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008873
Chris Mason4a096752008-07-21 10:29:44 -04008874 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008875 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008876 ClearPagePrivate(page);
8877 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008878 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008879 }
Chris Mason39279cc2007-06-12 06:35:45 -04008880}
8881
Chris Mason9ebefb182007-06-15 13:50:00 -04008882/*
8883 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8884 * called from a page fault handler when a page is first dirtied. Hence we must
8885 * be careful to check for EOF conditions here. We set the page up correctly
8886 * for a written page which means we get ENOSPC checking when writing into
8887 * holes and correct delalloc and unwritten extent mapping on filesystems that
8888 * support these features.
8889 *
8890 * We are not allowed to take the i_mutex here so we have to play games to
8891 * protect against truncate races as the page could now be beyond EOF. Because
8892 * vmtruncate() writes the inode size before removing pages, once we have the
8893 * page lock we can determine safely if the page is beyond EOF. If it is not
8894 * beyond EOF, then the page is guaranteed safe against truncation until we
8895 * unlock the page.
8896 */
Dave Jiang11bac802017-02-24 14:56:41 -08008897int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008898{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008899 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008900 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008901 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008902 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8903 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008904 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008905 char *kaddr;
8906 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008907 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008908 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008909 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308910 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008911 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008912 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308913 u64 end;
8914
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008915 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008916
Jan Karab2b5ef52012-06-12 16:20:45 +02008917 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008918 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008919 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308920 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008921
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308922 /*
8923 * Reserving delalloc space after obtaining the page lock can lead to
8924 * deadlock. For example, if a dirty page is locked by this function
8925 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8926 * dirty page write out, then the btrfs_writepage() function could
8927 * end up waiting indefinitely to get a lock on the page currently
8928 * being processed by btrfs_page_mkwrite() function.
8929 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008930 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308931 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008932 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08008933 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008934 reserved = 1;
8935 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008936 if (ret) {
8937 if (ret == -ENOMEM)
8938 ret = VM_FAULT_OOM;
8939 else /* -ENOSPC, -EIO, etc */
8940 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008941 if (reserved)
8942 goto out;
8943 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008944 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008945
Nick Piggin56a76f82009-03-31 15:23:23 -07008946 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008947again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008948 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008949 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008950
Chris Mason9ebefb182007-06-15 13:50:00 -04008951 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008952 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008953 /* page got truncated out from underneath us */
8954 goto out_unlock;
8955 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008956 wait_on_page_writeback(page);
8957
David Sterbaff13db42015-12-03 14:30:40 +01008958 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008959 set_page_extent_mapped(page);
8960
Chris Masoneb84ae02008-07-17 13:53:27 -04008961 /*
8962 * we can't set the delalloc bits if there are pending ordered
8963 * extents. Drop our locks and wait for them to finish
8964 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008965 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8966 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008967 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008968 unlock_extent_cached(io_tree, page_start, page_end,
8969 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008970 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008971 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008972 btrfs_put_ordered_extent(ordered);
8973 goto again;
8974 }
8975
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008976 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008977 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008978 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008979 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308980 end = page_start + reserved_space - 1;
8981 spin_lock(&BTRFS_I(inode)->lock);
8982 BTRFS_I(inode)->outstanding_extents++;
8983 spin_unlock(&BTRFS_I(inode)->lock);
8984 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008985 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308986 }
8987 }
8988
Josef Bacikfbf19082009-10-01 17:10:23 -04008989 /*
Liu Bo54160342016-12-13 12:15:19 -08008990 * page_mkwrite gets called when the page is firstly dirtied after it's
8991 * faulted in, but write(2) could also dirty a page and set delalloc
8992 * bits, thus in this case for space account reason, we still need to
8993 * clear any delalloc bits within this page range since we have to
8994 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008995 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308996 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008997 EXTENT_DIRTY | EXTENT_DELALLOC |
8998 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008999 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009000
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309001 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009002 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009003 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009004 unlock_extent_cached(io_tree, page_start, page_end,
9005 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009006 ret = VM_FAULT_SIGBUS;
9007 goto out_unlock;
9008 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009009 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009010
9011 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009012 if (page_start + PAGE_SIZE > size)
9013 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009014 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009015 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009016
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009017 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009018 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009019 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009020 flush_dcache_page(page);
9021 kunmap(page);
9022 }
Chris Mason247e7432008-07-17 12:53:51 -04009023 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009024 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009025 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009026
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009027 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009028 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009029 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009030
Josef Bacik2ac55d42010-02-03 19:33:23 +00009031 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009032
9033out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009034 if (!ret) {
9035 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009036 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009037 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009038 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009039out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309040 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009041out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009042 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009043 return ret;
9044}
9045
Josef Bacika41ad392011-01-31 15:30:16 -05009046static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009047{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009048 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009049 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009050 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009051 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009052 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009053 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009054 u64 mask = fs_info->sectorsize - 1;
9055 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009056
Josef Bacik0ef8b722013-10-25 16:13:35 -04009057 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9058 (u64)-1);
9059 if (ret)
9060 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009061
Josef Bacikfcb80c22011-05-03 10:40:22 -04009062 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009063 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009064 * 3 things going on here
9065 *
9066 * 1) We need to reserve space for our orphan item and the space to
9067 * delete our orphan item. Lord knows we don't want to have a dangling
9068 * orphan item because we didn't reserve space to remove it.
9069 *
9070 * 2) We need to reserve space to update our inode.
9071 *
9072 * 3) We need to have something to cache all the space that is going to
9073 * be free'd up by the truncate operation, but also have some slack
9074 * space reserved in case it uses space during the truncate (thank you
9075 * very much snapshotting).
9076 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009077 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009078 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009079 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009080 * doesn't end up using space reserved for updating the inode or
9081 * removing the orphan item. We also need to be able to stop the
9082 * transaction and start a new one, which means we need to be able to
9083 * update the inode several times, and we have no idea of knowing how
9084 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009085 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009086 * Then there is the orphan item, which does indeed need to be held on
9087 * to for the whole operation, and we need nobody to touch this reserved
9088 * space except the orphan code.
9089 *
9090 * So that leaves us with
9091 *
9092 * 1) root->orphan_block_rsv - for the orphan deletion.
9093 * 2) rsv - for the truncate reservation, which we will steal from the
9094 * transaction reservation.
9095 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9096 * updating the inode.
9097 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009098 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009099 if (!rsv)
9100 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009101 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009102 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009103
Josef Bacik907cbce2011-08-08 13:46:15 -04009104 /*
Josef Bacik07127182011-08-19 10:29:59 -04009105 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009106 * 1 for updating the inode.
9107 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009108 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009109 if (IS_ERR(trans)) {
9110 err = PTR_ERR(trans);
9111 goto out;
9112 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009113
Josef Bacik907cbce2011-08-08 13:46:15 -04009114 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009115 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009116 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009117 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009118
Chris Mason5a3f23d2009-03-31 13:27:11 -04009119 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009120 * So if we truncate and then write and fsync we normally would just
9121 * write the extents that changed, which is a problem if we need to
9122 * first truncate that entire inode. So set this flag so we write out
9123 * all of the extents in the inode to the sync log so we're completely
9124 * safe.
9125 */
9126 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009127 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009128
Yan, Zheng80825102009-11-12 09:35:36 +00009129 while (1) {
9130 ret = btrfs_truncate_inode_items(trans, root, inode,
9131 inode->i_size,
9132 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009133 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009134 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009135 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009136 }
Chris Mason39279cc2007-06-12 06:35:45 -04009137
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009138 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009139 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009140 if (ret) {
9141 err = ret;
9142 break;
9143 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009144
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009145 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009146 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009147
9148 trans = btrfs_start_transaction(root, 2);
9149 if (IS_ERR(trans)) {
9150 ret = err = PTR_ERR(trans);
9151 trans = NULL;
9152 break;
9153 }
9154
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009155 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009156 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009157 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009158 BUG_ON(ret); /* shouldn't happen */
9159 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009160 }
9161
9162 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009163 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009164 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009165 if (ret)
9166 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009167 }
9168
Chris Mason917c16b2011-11-08 14:49:59 -05009169 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009170 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009171 ret = btrfs_update_inode(trans, root, inode);
9172 if (ret && !err)
9173 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009174
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009175 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009176 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009177 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009178out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009179 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009180
Josef Bacik3893e332011-01-31 16:03:11 -05009181 if (ret && !err)
9182 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009183
Josef Bacik3893e332011-01-31 16:03:11 -05009184 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009185}
9186
Sven Wegener3b963622008-06-09 21:57:42 -04009187/*
Chris Masond352ac62008-09-29 15:18:18 -04009188 * create a new subvolume directory/inode (helper for the ioctl).
9189 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009190int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009191 struct btrfs_root *new_root,
9192 struct btrfs_root *parent_root,
9193 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009194{
Chris Mason39279cc2007-06-12 06:35:45 -04009195 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009196 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009197 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009198
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009199 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9200 new_dirid, new_dirid,
9201 S_IFDIR | (~current_umask() & S_IRWXUGO),
9202 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009203 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009204 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009205 inode->i_op = &btrfs_dir_inode_operations;
9206 inode->i_fop = &btrfs_dir_file_operations;
9207
Miklos Szeredibfe86842011-10-28 14:13:29 +02009208 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009209 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009210 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009211
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009212 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9213 if (err)
9214 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009215 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009216 new_root->root_key.objectid, err);
9217
Yan, Zheng76dda932009-09-21 16:00:26 -04009218 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009219
Yan, Zheng76dda932009-09-21 16:00:26 -04009220 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009221 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009222}
9223
Chris Mason39279cc2007-06-12 06:35:45 -04009224struct inode *btrfs_alloc_inode(struct super_block *sb)
9225{
9226 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009227 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009228
9229 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9230 if (!ei)
9231 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009232
9233 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009234 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009235 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009236 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009237 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009238 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009239 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009240 ei->disk_i_size = 0;
9241 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009242 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009243 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009244 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009245 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009246 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009247 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009248
Josef Bacik9e0baf62011-07-15 15:16:44 +00009249 spin_lock_init(&ei->lock);
9250 ei->outstanding_extents = 0;
9251 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009252
Josef Bacik72ac3c02012-05-23 14:13:11 -04009253 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009254 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009255
Miao Xie16cdcec2011-04-22 18:12:22 +08009256 ei->delayed_node = NULL;
9257
chandan r9cc97d62012-07-04 12:48:07 +05309258 ei->i_otime.tv_sec = 0;
9259 ei->i_otime.tv_nsec = 0;
9260
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009261 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009262 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009263 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9264 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009265 ei->io_tree.track_uptodate = 1;
9266 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009267 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009268 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009269 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009270 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009271 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009272 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009273 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009274 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009275
9276 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009277}
9278
Josef Bacikaaedb552013-10-11 14:44:09 -04009279#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9280void btrfs_test_destroy_inode(struct inode *inode)
9281{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009282 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009283 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9284}
9285#endif
9286
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009287static void btrfs_i_callback(struct rcu_head *head)
9288{
9289 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009290 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9291}
9292
Chris Mason39279cc2007-06-12 06:35:45 -04009293void btrfs_destroy_inode(struct inode *inode)
9294{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009295 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009296 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009297 struct btrfs_root *root = BTRFS_I(inode)->root;
9298
Al Virob3d9b7a2012-06-09 13:51:19 -04009299 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009300 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009301 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9302 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009303 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9304 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009305 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009306
Chris Mason5a3f23d2009-03-31 13:27:11 -04009307 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009308 * This can happen where we create an inode, but somebody else also
9309 * created the same inode and we need to destroy the one we already
9310 * created.
9311 */
9312 if (!root)
9313 goto free;
9314
Josef Bacik8a35d952012-05-23 14:26:42 -04009315 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9316 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009317 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009318 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009319 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009320 }
Josef Bacik7b128762008-07-24 12:17:14 -04009321
Chris Masond3977122009-01-05 21:25:51 -05009322 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009323 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9324 if (!ordered)
9325 break;
9326 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009327 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009328 "found ordered extent %llu %llu on inode cleanup",
9329 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009330 btrfs_remove_ordered_extent(inode, ordered);
9331 btrfs_put_ordered_extent(ordered);
9332 btrfs_put_ordered_extent(ordered);
9333 }
9334 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009335 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009336 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009337 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009338free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009339 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009340}
9341
Al Viro45321ac2010-06-07 13:43:19 -04009342int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009343{
9344 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009345
Naohiro Aota6379ef92013-06-06 09:56:34 +00009346 if (root == NULL)
9347 return 1;
9348
Liu Bofa6ac872013-02-20 14:10:23 +00009349 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009350 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009351 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009352 else
Al Viro45321ac2010-06-07 13:43:19 -04009353 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009354}
9355
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009356static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009357{
9358 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9359
9360 inode_init_once(&ei->vfs_inode);
9361}
9362
9363void btrfs_destroy_cachep(void)
9364{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009365 /*
9366 * Make sure all delayed rcu free inodes are flushed before we
9367 * destroy cache.
9368 */
9369 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009370 kmem_cache_destroy(btrfs_inode_cachep);
9371 kmem_cache_destroy(btrfs_trans_handle_cachep);
9372 kmem_cache_destroy(btrfs_transaction_cachep);
9373 kmem_cache_destroy(btrfs_path_cachep);
9374 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009375}
9376
9377int btrfs_init_cachep(void)
9378{
David Sterba837e1972012-09-07 03:00:48 -06009379 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009380 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009381 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9382 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009383 if (!btrfs_inode_cachep)
9384 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009385
David Sterba837e1972012-09-07 03:00:48 -06009386 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009387 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009388 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009389 if (!btrfs_trans_handle_cachep)
9390 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009391
David Sterba837e1972012-09-07 03:00:48 -06009392 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009393 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009394 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009395 if (!btrfs_transaction_cachep)
9396 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009397
David Sterba837e1972012-09-07 03:00:48 -06009398 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009399 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009400 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009401 if (!btrfs_path_cachep)
9402 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009403
David Sterba837e1972012-09-07 03:00:48 -06009404 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009405 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009406 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009407 if (!btrfs_free_space_cachep)
9408 goto fail;
9409
Chris Mason39279cc2007-06-12 06:35:45 -04009410 return 0;
9411fail:
9412 btrfs_destroy_cachep();
9413 return -ENOMEM;
9414}
9415
David Howellsa528d352017-01-31 16:46:22 +00009416static int btrfs_getattr(const struct path *path, struct kstat *stat,
9417 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009418{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009419 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009420 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009421 u32 blocksize = inode->i_sb->s_blocksize;
9422
Chris Mason39279cc2007-06-12 06:35:45 -04009423 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009424 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009425
9426 spin_lock(&BTRFS_I(inode)->lock);
9427 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9428 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009429 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009430 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009431 return 0;
9432}
9433
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009434static int btrfs_rename_exchange(struct inode *old_dir,
9435 struct dentry *old_dentry,
9436 struct inode *new_dir,
9437 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009438{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009439 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009440 struct btrfs_trans_handle *trans;
9441 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009442 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009443 struct inode *new_inode = new_dentry->d_inode;
9444 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009445 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009446 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009447 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9448 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009449 u64 old_idx = 0;
9450 u64 new_idx = 0;
9451 u64 root_objectid;
9452 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009453 bool root_log_pinned = false;
9454 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009455
9456 /* we only allow rename subvolume link between subvolumes */
9457 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9458 return -EXDEV;
9459
9460 /* close the race window with snapshot create/destroy ioctl */
9461 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009462 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009463 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009464 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009465
9466 /*
9467 * We want to reserve the absolute worst case amount of items. So if
9468 * both inodes are subvols and we need to unlink them then that would
9469 * require 4 item modifications, but if they are both normal inodes it
9470 * would require 5 item modifications, so we'll assume their normal
9471 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9472 * should cover the worst case number of items we'll modify.
9473 */
9474 trans = btrfs_start_transaction(root, 12);
9475 if (IS_ERR(trans)) {
9476 ret = PTR_ERR(trans);
9477 goto out_notrans;
9478 }
9479
9480 /*
9481 * We need to find a free sequence number both in the source and
9482 * in the destination directory for the exchange.
9483 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009484 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009485 if (ret)
9486 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009487 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009488 if (ret)
9489 goto out_fail;
9490
9491 BTRFS_I(old_inode)->dir_index = 0ULL;
9492 BTRFS_I(new_inode)->dir_index = 0ULL;
9493
9494 /* Reference for the source. */
9495 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9496 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009497 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009498 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009499 btrfs_pin_log_trans(root);
9500 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009501 ret = btrfs_insert_inode_ref(trans, dest,
9502 new_dentry->d_name.name,
9503 new_dentry->d_name.len,
9504 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009505 btrfs_ino(BTRFS_I(new_dir)),
9506 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009507 if (ret)
9508 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009509 }
9510
9511 /* And now for the dest. */
9512 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9513 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009514 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009515 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009516 btrfs_pin_log_trans(dest);
9517 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009518 ret = btrfs_insert_inode_ref(trans, root,
9519 old_dentry->d_name.name,
9520 old_dentry->d_name.len,
9521 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009522 btrfs_ino(BTRFS_I(old_dir)),
9523 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009524 if (ret)
9525 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009526 }
9527
9528 /* Update inode version and ctime/mtime. */
9529 inode_inc_iversion(old_dir);
9530 inode_inc_iversion(new_dir);
9531 inode_inc_iversion(old_inode);
9532 inode_inc_iversion(new_inode);
9533 old_dir->i_ctime = old_dir->i_mtime = ctime;
9534 new_dir->i_ctime = new_dir->i_mtime = ctime;
9535 old_inode->i_ctime = ctime;
9536 new_inode->i_ctime = ctime;
9537
9538 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009539 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9540 BTRFS_I(old_inode), 1);
9541 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9542 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009543 }
9544
9545 /* src is a subvolume */
9546 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9547 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9548 ret = btrfs_unlink_subvol(trans, root, old_dir,
9549 root_objectid,
9550 old_dentry->d_name.name,
9551 old_dentry->d_name.len);
9552 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009553 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9554 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009555 old_dentry->d_name.name,
9556 old_dentry->d_name.len);
9557 if (!ret)
9558 ret = btrfs_update_inode(trans, root, old_inode);
9559 }
9560 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009561 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009562 goto out_fail;
9563 }
9564
9565 /* dest is a subvolume */
9566 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9567 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9568 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9569 root_objectid,
9570 new_dentry->d_name.name,
9571 new_dentry->d_name.len);
9572 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009573 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9574 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009575 new_dentry->d_name.name,
9576 new_dentry->d_name.len);
9577 if (!ret)
9578 ret = btrfs_update_inode(trans, dest, new_inode);
9579 }
9580 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009581 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009582 goto out_fail;
9583 }
9584
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009585 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009586 new_dentry->d_name.name,
9587 new_dentry->d_name.len, 0, old_idx);
9588 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009589 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009590 goto out_fail;
9591 }
9592
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009593 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009594 old_dentry->d_name.name,
9595 old_dentry->d_name.len, 0, new_idx);
9596 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009597 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598 goto out_fail;
9599 }
9600
9601 if (old_inode->i_nlink == 1)
9602 BTRFS_I(old_inode)->dir_index = old_idx;
9603 if (new_inode->i_nlink == 1)
9604 BTRFS_I(new_inode)->dir_index = new_idx;
9605
Filipe Manana86e8aa02016-05-05 02:02:27 +01009606 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009607 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009608 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9609 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009610 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009611 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009612 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009613 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009615 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9616 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009618 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009619 }
9620out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009621 /*
9622 * If we have pinned a log and an error happened, we unpin tasks
9623 * trying to sync the log and force them to fallback to a transaction
9624 * commit if the log currently contains any of the inodes involved in
9625 * this rename operation (to ensure we do not persist a log with an
9626 * inconsistent state for any of these inodes or leading to any
9627 * inconsistencies when replayed). If the transaction was aborted, the
9628 * abortion reason is propagated to userspace when attempting to commit
9629 * the transaction. If the log does not contain any of these inodes, we
9630 * allow the tasks to sync it.
9631 */
9632 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009633 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9634 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9635 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009636 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009637 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009638 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009639
9640 if (root_log_pinned) {
9641 btrfs_end_log_trans(root);
9642 root_log_pinned = false;
9643 }
9644 if (dest_log_pinned) {
9645 btrfs_end_log_trans(dest);
9646 dest_log_pinned = false;
9647 }
9648 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009649 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650out_notrans:
9651 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009652 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009653 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009654 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009655
9656 return ret;
9657}
9658
9659static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9660 struct btrfs_root *root,
9661 struct inode *dir,
9662 struct dentry *dentry)
9663{
9664 int ret;
9665 struct inode *inode;
9666 u64 objectid;
9667 u64 index;
9668
9669 ret = btrfs_find_free_ino(root, &objectid);
9670 if (ret)
9671 return ret;
9672
9673 inode = btrfs_new_inode(trans, root, dir,
9674 dentry->d_name.name,
9675 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009676 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009677 objectid,
9678 S_IFCHR | WHITEOUT_MODE,
9679 &index);
9680
9681 if (IS_ERR(inode)) {
9682 ret = PTR_ERR(inode);
9683 return ret;
9684 }
9685
9686 inode->i_op = &btrfs_special_inode_operations;
9687 init_special_inode(inode, inode->i_mode,
9688 WHITEOUT_DEV);
9689
9690 ret = btrfs_init_inode_security(trans, inode, dir,
9691 &dentry->d_name);
9692 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009693 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009695 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9696 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009697 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009698 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009699
9700 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009701out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009702 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009703 if (ret)
9704 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009705 iput(inode);
9706
Filipe Mananac9901612016-05-05 01:41:57 +01009707 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708}
9709
Chris Mason39279cc2007-06-12 06:35:45 -04009710static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009711 struct inode *new_dir, struct dentry *new_dentry,
9712 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009713{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009714 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009715 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009716 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009717 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9718 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009719 struct inode *new_inode = d_inode(new_dentry);
9720 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009721 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009722 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009723 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009724 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009725 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009726
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009727 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009728 return -EPERM;
9729
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009730 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009731 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009732 return -EXDEV;
9733
Li Zefan33345d012011-04-20 10:31:50 +08009734 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009735 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009736 return -ENOTEMPTY;
9737
Chris Mason39279cc2007-06-12 06:35:45 -04009738 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009739 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009740 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009741
9742
9743 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009744 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009745 new_dentry->d_name.name,
9746 new_dentry->d_name.len);
9747
9748 if (ret) {
9749 if (ret == -EEXIST) {
9750 /* we shouldn't get
9751 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309752 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009753 return ret;
9754 }
9755 } else {
9756 /* maybe -EOVERFLOW */
9757 return ret;
9758 }
9759 }
9760 ret = 0;
9761
Chris Mason5a3f23d2009-03-31 13:27:11 -04009762 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009763 * we're using rename to replace one file with another. Start IO on it
9764 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009765 */
Chris Mason8d875f92014-08-12 10:47:42 -07009766 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009767 filemap_flush(old_inode->i_mapping);
9768
Yan, Zheng76dda932009-09-21 16:00:26 -04009769 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009770 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009771 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009772 /*
9773 * We want to reserve the absolute worst case amount of items. So if
9774 * both inodes are subvols and we need to unlink them then that would
9775 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009776 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009777 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9778 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009779 * If our rename has the whiteout flag, we need more 5 units for the
9780 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9781 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009782 */
Filipe Manana5062af32016-05-05 10:26:26 +01009783 trans_num_items = 11;
9784 if (flags & RENAME_WHITEOUT)
9785 trans_num_items += 5;
9786 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009787 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009788 ret = PTR_ERR(trans);
9789 goto out_notrans;
9790 }
Chris Mason5f39d392007-10-15 16:14:19 -04009791
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009792 if (dest != root)
9793 btrfs_record_root_in_trans(trans, dest);
9794
Nikolay Borisov877574e2017-02-20 13:50:33 +02009795 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009796 if (ret)
9797 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009798
Miao Xie67de1172013-12-26 13:07:06 +08009799 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009800 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009801 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009802 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009803 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009804 btrfs_pin_log_trans(root);
9805 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009806 ret = btrfs_insert_inode_ref(trans, dest,
9807 new_dentry->d_name.name,
9808 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009809 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009810 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009811 if (ret)
9812 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009813 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009814
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009815 inode_inc_iversion(old_dir);
9816 inode_inc_iversion(new_dir);
9817 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009818 old_dir->i_ctime = old_dir->i_mtime =
9819 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009820 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009821
Chris Mason12fcfd22009-03-24 10:24:20 -04009822 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009823 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9824 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009825
Li Zefan33345d012011-04-20 10:31:50 +08009826 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009827 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9828 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9829 old_dentry->d_name.name,
9830 old_dentry->d_name.len);
9831 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009832 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9833 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009834 old_dentry->d_name.name,
9835 old_dentry->d_name.len);
9836 if (!ret)
9837 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009838 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009839 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009840 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009841 goto out_fail;
9842 }
Chris Mason39279cc2007-06-12 06:35:45 -04009843
9844 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009845 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009846 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009847 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009848 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9849 root_objectid = BTRFS_I(new_inode)->location.objectid;
9850 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9851 root_objectid,
9852 new_dentry->d_name.name,
9853 new_dentry->d_name.len);
9854 BUG_ON(new_inode->i_nlink == 0);
9855 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009856 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9857 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009858 new_dentry->d_name.name,
9859 new_dentry->d_name.len);
9860 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009861 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009862 ret = btrfs_orphan_add(trans,
9863 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009864 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009865 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009866 goto out_fail;
9867 }
Chris Mason39279cc2007-06-12 06:35:45 -04009868 }
Josef Bacikaec74772008-07-24 12:12:38 -04009869
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009870 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009871 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009872 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009873 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009874 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009875 goto out_fail;
9876 }
Chris Mason39279cc2007-06-12 06:35:45 -04009877
Miao Xie67de1172013-12-26 13:07:06 +08009878 if (old_inode->i_nlink == 1)
9879 BTRFS_I(old_inode)->dir_index = index;
9880
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009881 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009882 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009883
David Sterbaf85b7372017-01-20 14:54:07 +01009884 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9885 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009886 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009887 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009888 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009889
9890 if (flags & RENAME_WHITEOUT) {
9891 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9892 old_dentry);
9893
9894 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009895 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009896 goto out_fail;
9897 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009898 }
Chris Mason39279cc2007-06-12 06:35:45 -04009899out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009900 /*
9901 * If we have pinned the log and an error happened, we unpin tasks
9902 * trying to sync the log and force them to fallback to a transaction
9903 * commit if the log currently contains any of the inodes involved in
9904 * this rename operation (to ensure we do not persist a log with an
9905 * inconsistent state for any of these inodes or leading to any
9906 * inconsistencies when replayed). If the transaction was aborted, the
9907 * abortion reason is propagated to userspace when attempting to commit
9908 * the transaction. If the log does not contain any of these inodes, we
9909 * allow the tasks to sync it.
9910 */
9911 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009912 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9913 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9914 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009915 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009916 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009917 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009918
9919 btrfs_end_log_trans(root);
9920 log_pinned = false;
9921 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009922 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009923out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009924 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009925 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009926
Chris Mason39279cc2007-06-12 06:35:45 -04009927 return ret;
9928}
9929
Miklos Szeredi80ace852014-07-23 15:15:32 +02009930static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9931 struct inode *new_dir, struct dentry *new_dentry,
9932 unsigned int flags)
9933{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009934 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009935 return -EINVAL;
9936
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009937 if (flags & RENAME_EXCHANGE)
9938 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9939 new_dentry);
9940
9941 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009942}
9943
Miao Xie8ccf6f192012-10-25 09:28:04 +00009944static void btrfs_run_delalloc_work(struct btrfs_work *work)
9945{
9946 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009947 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009948
9949 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9950 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009951 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009952 filemap_flush(inode->i_mapping);
9953 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9954 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009955 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009956
9957 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009958 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009959 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009960 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009961 complete(&delalloc_work->completion);
9962}
9963
9964struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009965 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009966{
9967 struct btrfs_delalloc_work *work;
9968
David Sterba100d5702015-12-08 14:39:32 +01009969 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009970 if (!work)
9971 return NULL;
9972
9973 init_completion(&work->completion);
9974 INIT_LIST_HEAD(&work->list);
9975 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009976 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009977 WARN_ON_ONCE(!inode);
9978 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9979 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009980
9981 return work;
9982}
9983
9984void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9985{
9986 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009987 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009988}
9989
Chris Masond352ac62008-09-29 15:18:18 -04009990/*
9991 * some fairly slow code that needs optimization. This walks the list
9992 * of all the inodes with pending delalloc and forces them to disk.
9993 */
Miao Xie6c255e62014-03-06 13:55:01 +08009994static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9995 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009996{
Chris Masonea8c2812008-08-04 23:17:27 -04009997 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009998 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009999 struct btrfs_delalloc_work *work, *next;
10000 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010001 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010002 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010003
Miao Xie8ccf6f192012-10-25 09:28:04 +000010004 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010005 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010006
Miao Xie573bfb72014-03-06 13:55:03 +080010007 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010008 spin_lock(&root->delalloc_lock);
10009 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010010 while (!list_empty(&splice)) {
10011 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010012 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010013
Miao Xieeb73c1b2013-05-15 07:48:22 +000010014 list_move_tail(&binode->delalloc_inodes,
10015 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010016 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010017 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010018 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010019 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010020 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010021 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010022
David Sterba651d4942015-11-27 19:24:16 +010010023 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010024 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010025 if (delay_iput)
10026 btrfs_add_delayed_iput(inode);
10027 else
10028 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010029 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010030 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010031 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010032 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010033 btrfs_queue_work(root->fs_info->flush_workers,
10034 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010035 ret++;
10036 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010037 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010038 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010039 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010040 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010041 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010042
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010043out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010044 list_for_each_entry_safe(work, next, &works, list) {
10045 list_del_init(&work->list);
10046 btrfs_wait_and_free_delalloc_work(work);
10047 }
10048
Miao Xieeb73c1b2013-05-15 07:48:22 +000010049 if (!list_empty_careful(&splice)) {
10050 spin_lock(&root->delalloc_lock);
10051 list_splice_tail(&splice, &root->delalloc_inodes);
10052 spin_unlock(&root->delalloc_lock);
10053 }
Miao Xie573bfb72014-03-06 13:55:03 +080010054 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010055 return ret;
10056}
10057
10058int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10059{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010060 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010061 int ret;
10062
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010063 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010064 return -EROFS;
10065
Miao Xie6c255e62014-03-06 13:55:01 +080010066 ret = __start_delalloc_inodes(root, delay_iput, -1);
10067 if (ret > 0)
10068 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010069 /*
10070 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010071 * we have to make sure the IO is actually started and that
10072 * ordered extents get created before we return
10073 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010074 atomic_inc(&fs_info->async_submit_draining);
10075 while (atomic_read(&fs_info->nr_async_submits) ||
10076 atomic_read(&fs_info->async_delalloc_pages)) {
10077 wait_event(fs_info->async_submit_wait,
10078 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10079 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010080 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010081 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010082 return ret;
10083}
10084
Miao Xie6c255e62014-03-06 13:55:01 +080010085int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10086 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010087{
10088 struct btrfs_root *root;
10089 struct list_head splice;
10090 int ret;
10091
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010092 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010093 return -EROFS;
10094
10095 INIT_LIST_HEAD(&splice);
10096
Miao Xie573bfb72014-03-06 13:55:03 +080010097 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010098 spin_lock(&fs_info->delalloc_root_lock);
10099 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010100 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010101 root = list_first_entry(&splice, struct btrfs_root,
10102 delalloc_root);
10103 root = btrfs_grab_fs_root(root);
10104 BUG_ON(!root);
10105 list_move_tail(&root->delalloc_root,
10106 &fs_info->delalloc_roots);
10107 spin_unlock(&fs_info->delalloc_root_lock);
10108
Miao Xie6c255e62014-03-06 13:55:01 +080010109 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010111 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010112 goto out;
10113
Miao Xie6c255e62014-03-06 13:55:01 +080010114 if (nr != -1) {
10115 nr -= ret;
10116 WARN_ON(nr < 0);
10117 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010118 spin_lock(&fs_info->delalloc_root_lock);
10119 }
10120 spin_unlock(&fs_info->delalloc_root_lock);
10121
Miao Xie6c255e62014-03-06 13:55:01 +080010122 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010123 atomic_inc(&fs_info->async_submit_draining);
10124 while (atomic_read(&fs_info->nr_async_submits) ||
10125 atomic_read(&fs_info->async_delalloc_pages)) {
10126 wait_event(fs_info->async_submit_wait,
10127 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10128 atomic_read(&fs_info->async_delalloc_pages) == 0));
10129 }
10130 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010131out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010132 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010133 spin_lock(&fs_info->delalloc_root_lock);
10134 list_splice_tail(&splice, &fs_info->delalloc_roots);
10135 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010136 }
Miao Xie573bfb72014-03-06 13:55:03 +080010137 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010138 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010139}
10140
Chris Mason39279cc2007-06-12 06:35:45 -040010141static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10142 const char *symname)
10143{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010144 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010145 struct btrfs_trans_handle *trans;
10146 struct btrfs_root *root = BTRFS_I(dir)->root;
10147 struct btrfs_path *path;
10148 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010149 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010150 int err;
10151 int drop_inode = 0;
10152 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010153 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010154 int name_len;
10155 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010156 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010157 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010158 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010159
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010160 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010161 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010162 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010163
Josef Bacik9ed74f22009-09-11 16:12:44 -040010164 /*
10165 * 2 items for inode item and ref
10166 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010167 * 1 item for updating parent inode item
10168 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010169 * 1 item for xattr if selinux is on
10170 */
Filipe Manana9269d122015-12-31 18:16:29 +000010171 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010172 if (IS_ERR(trans))
10173 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010174
Li Zefan581bb052011-04-20 10:06:11 +080010175 err = btrfs_find_free_ino(root, &objectid);
10176 if (err)
10177 goto out_unlock;
10178
Josef Bacikaec74772008-07-24 12:12:38 -040010179 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010180 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10181 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010182 if (IS_ERR(inode)) {
10183 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010184 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010185 }
Chris Mason39279cc2007-06-12 06:35:45 -040010186
Casey Schauflerad19db72011-12-15 10:09:07 -050010187 /*
10188 * If the active LSM wants to access the inode during
10189 * d_instantiate it needs these. Smack checks to see
10190 * if the filesystem supports xattrs by looking at the
10191 * ops vector.
10192 */
10193 inode->i_fop = &btrfs_file_operations;
10194 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010195 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010196 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10197
10198 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10199 if (err)
10200 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010201
Chris Mason39279cc2007-06-12 06:35:45 -040010202 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010203 if (!path) {
10204 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010205 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010206 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010207 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010208 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010209 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010210 datasize = btrfs_file_extent_calc_inline_size(name_len);
10211 err = btrfs_insert_empty_item(trans, root, path, &key,
10212 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010213 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010214 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010215 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010216 }
Chris Mason5f39d392007-10-15 16:14:19 -040010217 leaf = path->nodes[0];
10218 ei = btrfs_item_ptr(leaf, path->slots[0],
10219 struct btrfs_file_extent_item);
10220 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10221 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010222 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010223 btrfs_set_file_extent_encryption(leaf, ei, 0);
10224 btrfs_set_file_extent_compression(leaf, ei, 0);
10225 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10226 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10227
Chris Mason39279cc2007-06-12 06:35:45 -040010228 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010229 write_extent_buffer(leaf, symname, ptr, name_len);
10230 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010231 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010232
Chris Mason39279cc2007-06-12 06:35:45 -040010233 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010234 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010235 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010236 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010237 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010238 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010239 /*
10240 * Last step, add directory indexes for our symlink inode. This is the
10241 * last step to avoid extra cleanup of these indexes if an error happens
10242 * elsewhere above.
10243 */
10244 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010245 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10246 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010247 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010248 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010249 goto out_unlock_inode;
10250 }
10251
10252 unlock_new_inode(inode);
10253 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010254
10255out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010256 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010257 if (drop_inode) {
10258 inode_dec_link_count(inode);
10259 iput(inode);
10260 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010261 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010262 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010263
10264out_unlock_inode:
10265 drop_inode = 1;
10266 unlock_new_inode(inode);
10267 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010268}
Chris Mason16432982008-04-10 10:23:21 -040010269
Josef Bacik0af3d002010-06-21 14:48:16 -040010270static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10271 u64 start, u64 num_bytes, u64 min_size,
10272 loff_t actual_len, u64 *alloc_hint,
10273 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010274{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010275 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010276 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10277 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010278 struct btrfs_root *root = BTRFS_I(inode)->root;
10279 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010280 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010281 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010282 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010283 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010284 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010285 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010286 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010287
Josef Bacik0af3d002010-06-21 14:48:16 -040010288 if (trans)
10289 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010290 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010291 if (own_trans) {
10292 trans = btrfs_start_transaction(root, 3);
10293 if (IS_ERR(trans)) {
10294 ret = PTR_ERR(trans);
10295 break;
10296 }
Yan Zhengd899e052008-10-30 14:25:28 -040010297 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010298
Byongho Leeee221842015-12-15 01:42:10 +090010299 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010300 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010301 /*
10302 * If we are severely fragmented we could end up with really
10303 * small allocations, so if the allocator is returning small
10304 * chunks lets make its job easier by only searching for those
10305 * sized chunks.
10306 */
10307 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010308 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10309 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010310 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010311 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010312 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010313 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010314 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010315 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010316
Josef Bacik0b670dc2015-09-23 17:11:16 -040010317 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010318 ret = insert_reserved_file_extent(trans, inode,
10319 cur_offset, ins.objectid,
10320 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010321 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010322 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010323 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010324 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010325 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010326 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010327 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010328 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010329 break;
10330 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010331
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010332 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010333 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010334
Josef Bacik5dc562c2012-08-17 13:14:17 -040010335 em = alloc_extent_map();
10336 if (!em) {
10337 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10338 &BTRFS_I(inode)->runtime_flags);
10339 goto next;
10340 }
10341
10342 em->start = cur_offset;
10343 em->orig_start = cur_offset;
10344 em->len = ins.offset;
10345 em->block_start = ins.objectid;
10346 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010347 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010348 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010349 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010350 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10351 em->generation = trans->transid;
10352
10353 while (1) {
10354 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010355 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010356 write_unlock(&em_tree->lock);
10357 if (ret != -EEXIST)
10358 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010359 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010360 cur_offset + ins.offset - 1,
10361 0);
10362 }
10363 free_extent_map(em);
10364next:
Yan Zhengd899e052008-10-30 14:25:28 -040010365 num_bytes -= ins.offset;
10366 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010367 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010368
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010369 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010370 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010371 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010372 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010373 (actual_len > inode->i_size) &&
10374 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010375 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010376 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010377 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010378 i_size = cur_offset;
10379 i_size_write(inode, i_size);
10380 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010381 }
10382
Yan Zhengd899e052008-10-30 14:25:28 -040010383 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010384
10385 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010386 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010387 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010388 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010389 break;
10390 }
Yan Zhengd899e052008-10-30 14:25:28 -040010391
Josef Bacik0af3d002010-06-21 14:48:16 -040010392 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010393 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010394 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010395 if (cur_offset < end)
10396 btrfs_free_reserved_data_space(inode, cur_offset,
10397 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010398 return ret;
10399}
10400
Josef Bacik0af3d002010-06-21 14:48:16 -040010401int btrfs_prealloc_file_range(struct inode *inode, int mode,
10402 u64 start, u64 num_bytes, u64 min_size,
10403 loff_t actual_len, u64 *alloc_hint)
10404{
10405 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10406 min_size, actual_len, alloc_hint,
10407 NULL);
10408}
10409
10410int btrfs_prealloc_file_range_trans(struct inode *inode,
10411 struct btrfs_trans_handle *trans, int mode,
10412 u64 start, u64 num_bytes, u64 min_size,
10413 loff_t actual_len, u64 *alloc_hint)
10414{
10415 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10416 min_size, actual_len, alloc_hint, trans);
10417}
10418
Chris Masone6dcd2d2008-07-17 12:53:50 -040010419static int btrfs_set_page_dirty(struct page *page)
10420{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010421 return __set_page_dirty_nobuffers(page);
10422}
10423
Al Viro10556cb22011-06-20 19:28:19 -040010424static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010425{
Li Zefanb83cc962010-12-20 16:04:08 +080010426 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010427 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010428
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010429 if (mask & MAY_WRITE &&
10430 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10431 if (btrfs_root_readonly(root))
10432 return -EROFS;
10433 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10434 return -EACCES;
10435 }
Al Viro2830ba72011-06-20 19:16:29 -040010436 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010437}
Chris Mason39279cc2007-06-12 06:35:45 -040010438
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010439static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10440{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010441 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010442 struct btrfs_trans_handle *trans;
10443 struct btrfs_root *root = BTRFS_I(dir)->root;
10444 struct inode *inode = NULL;
10445 u64 objectid;
10446 u64 index;
10447 int ret = 0;
10448
10449 /*
10450 * 5 units required for adding orphan entry
10451 */
10452 trans = btrfs_start_transaction(root, 5);
10453 if (IS_ERR(trans))
10454 return PTR_ERR(trans);
10455
10456 ret = btrfs_find_free_ino(root, &objectid);
10457 if (ret)
10458 goto out;
10459
10460 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010461 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010462 if (IS_ERR(inode)) {
10463 ret = PTR_ERR(inode);
10464 inode = NULL;
10465 goto out;
10466 }
10467
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010468 inode->i_fop = &btrfs_file_operations;
10469 inode->i_op = &btrfs_file_inode_operations;
10470
10471 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010472 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10473
Chris Masonb0d5d102014-09-08 13:08:51 -070010474 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10475 if (ret)
10476 goto out_inode;
10477
10478 ret = btrfs_update_inode(trans, root, inode);
10479 if (ret)
10480 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010481 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010482 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010483 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010484
Filipe Manana5762b5c2014-08-01 00:10:32 +010010485 /*
10486 * We set number of links to 0 in btrfs_new_inode(), and here we set
10487 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10488 * through:
10489 *
10490 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10491 */
10492 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010493 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010494 d_tmpfile(dentry, inode);
10495 mark_inode_dirty(inode);
10496
10497out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010498 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010499 if (ret)
10500 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010501 btrfs_balance_delayed_items(fs_info);
10502 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010503 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010504
10505out_inode:
10506 unlock_new_inode(inode);
10507 goto out;
10508
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010509}
10510
David Sterba20a7db82017-02-17 16:24:29 +010010511__attribute__((const))
10512static int dummy_readpage_io_failed_hook(struct page *page, int failed_mirror)
10513{
10514 return 0;
10515}
10516
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010517static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010518 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010519 .lookup = btrfs_lookup,
10520 .create = btrfs_create,
10521 .unlink = btrfs_unlink,
10522 .link = btrfs_link,
10523 .mkdir = btrfs_mkdir,
10524 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010525 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010526 .symlink = btrfs_symlink,
10527 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010528 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010529 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010530 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010531 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010532 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010533 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010534 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010535};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010536static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010537 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010538 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010539 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010540};
Yan, Zheng76dda932009-09-21 16:00:26 -040010541
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010542static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010543 .llseek = generic_file_llseek,
10544 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010545 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010546 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010547#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010548 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010549#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010550 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010551 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010552};
10553
David Sterba20e55062015-11-19 11:42:28 +010010554static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010555 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010556 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010557 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010558 .merge_bio_hook = btrfs_merge_bio_hook,
David Sterba20a7db82017-02-17 16:24:29 +010010559 .readpage_io_failed_hook = dummy_readpage_io_failed_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010560
10561 /* optional callbacks */
10562 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010563 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010564 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010565 .set_bit_hook = btrfs_set_bit_hook,
10566 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010567 .merge_extent_hook = btrfs_merge_extent_hook,
10568 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010569};
10570
Chris Mason35054392009-01-21 13:11:13 -050010571/*
10572 * btrfs doesn't support the bmap operation because swapfiles
10573 * use bmap to make a mapping of extents in the file. They assume
10574 * these extents won't change over the life of the file and they
10575 * use the bmap result to do IO directly to the drive.
10576 *
10577 * the btrfs bmap call would return logical addresses that aren't
10578 * suitable for IO and they also will change frequently as COW
10579 * operations happen. So, swapfile + btrfs == corruption.
10580 *
10581 * For now we're avoiding this by dropping bmap.
10582 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010583static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010584 .readpage = btrfs_readpage,
10585 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010586 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010587 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010588 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010589 .invalidatepage = btrfs_invalidatepage,
10590 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010591 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010592 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010593};
10594
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010595static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010596 .readpage = btrfs_readpage,
10597 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010598 .invalidatepage = btrfs_invalidatepage,
10599 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010600};
10601
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010602static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010603 .getattr = btrfs_getattr,
10604 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010605 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010606 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010607 .fiemap = btrfs_fiemap,
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,
Chris Mason39279cc2007-06-12 06:35:45 -040010611};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010612static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010613 .getattr = btrfs_getattr,
10614 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010615 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010616 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010617 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010618 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010619 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010620};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010621static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010622 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010623 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010624 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010625 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010626 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010627 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010628};
Yan, Zheng76dda932009-09-21 16:00:26 -040010629
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010630const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010631 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010632 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010633};