blob: 11a4eeadf662b29253d8915e382dc271b8c6335b [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,
513 &total_compressed,
David Sterba069eac72017-02-14 19:36:54 +0100514 BTRFS_MAX_COMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400515
516 if (!ret) {
517 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300518 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100519 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400520 char *kaddr;
521
522 /* zero the tail end of the last page, we might be
523 * sending it down to disk
524 */
525 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800526 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400527 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300528 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800529 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400530 }
531 will_compress = 1;
532 }
533 }
Li Zefan560f7d72011-09-08 10:22:01 +0800534cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400535 if (start == 0) {
536 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500537 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400538 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500539 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400540 */
Josef Bacik00361582013-08-14 14:02:47 -0400541 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800542 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400543 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500544 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400545 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000546 total_compressed,
547 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400548 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100549 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400550 unsigned long clear_flags = EXTENT_DELALLOC |
551 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100552 unsigned long page_error_op;
553
Josef Bacik151a41b2013-07-29 13:22:24 -0400554 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100555 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400556
Chris Mason771ed682008-11-06 22:02:51 -0500557 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100558 * inline extent creation worked or returned error,
559 * we don't need to create any more async work items.
560 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500561 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800562 extent_clear_unlock_delalloc(inode, start, end, end,
563 NULL, clear_flags,
564 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400565 PAGE_CLEAR_DIRTY |
566 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100567 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400568 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800569 btrfs_free_reserved_data_space_noquota(inode, start,
570 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 goto free_pages_out;
572 }
573 }
574
575 if (will_compress) {
576 /*
577 * we aren't doing an inline extent round the compressed size
578 * up to a block size boundary so the allocator does sane
579 * things
580 */
Qu Wenruofda28322013-02-26 08:10:22 +0000581 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400582
583 /*
584 * one last check to make sure the compression is really a
585 * win, compare the page count read with the blocks on disk
586 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300587 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400588 if (total_compressed >= total_in) {
589 will_compress = 0;
590 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400591 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700592 *num_added += 1;
593
594 /*
595 * The async work queues will take care of doing actual
596 * allocation on disk for these compressed pages, and
597 * will submit them to the elevator.
598 */
599 add_async_extent(async_cow, start, num_bytes,
David Sterba4d3a8002017-02-14 19:04:07 +0100600 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700601 compress_type);
602
603 if (start + num_bytes < end) {
604 start += num_bytes;
605 pages = NULL;
606 cond_resched();
607 goto again;
608 }
609 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400610 }
611 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700612 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400613 /*
614 * the compression code ran but failed to make things smaller,
615 * free any pages it allocated and our page pointer array
616 */
David Sterba4d3a8002017-02-14 19:04:07 +0100617 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400618 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300619 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400620 }
621 kfree(pages);
622 pages = NULL;
623 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100624 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400625
626 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400627 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500628 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500629 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500630 }
Chris Masonc8b97812008-10-29 14:49:59 -0400631 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500632cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700633 /*
634 * No compression, but we still need to write the pages in the file
635 * we've been given so far. redirty the locked page if it corresponds
636 * to our extent and set things up for the async work queue to run
637 * cow_file_range to do the normal delalloc dance.
638 */
639 if (page_offset(locked_page) >= start &&
640 page_offset(locked_page) <= end)
641 __set_page_dirty_nobuffers(locked_page);
642 /* unlocked later on in the async handlers */
643
644 if (redirty)
645 extent_range_redirty_for_io(inode, start, end);
646 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
647 BTRFS_COMPRESS_NONE);
648 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500649
Filipe Mananac44f6492014-10-09 21:15:44 +0100650 return;
Chris Mason771ed682008-11-06 22:02:51 -0500651
652free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100653 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500654 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300655 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500656 }
Chris Masond3977122009-01-05 21:25:51 -0500657 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500658}
Chris Mason771ed682008-11-06 22:02:51 -0500659
Filipe Manana40ae8372014-10-06 22:14:24 +0100660static void free_async_extent_pages(struct async_extent *async_extent)
661{
662 int i;
663
664 if (!async_extent->pages)
665 return;
666
667 for (i = 0; i < async_extent->nr_pages; i++) {
668 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300669 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100670 }
671 kfree(async_extent->pages);
672 async_extent->nr_pages = 0;
673 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500674}
675
676/*
677 * phase two of compressed writeback. This is the ordered portion
678 * of the code, which only gets called in the order the work was
679 * queued. We walk all the async extents created by compress_file_range
680 * and send them down to the disk.
681 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100682static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500683 struct async_cow *async_cow)
684{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400685 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500686 struct async_extent *async_extent;
687 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500688 struct btrfs_key ins;
689 struct extent_map *em;
690 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500691 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500693
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500694again:
Chris Masond3977122009-01-05 21:25:51 -0500695 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500696 async_extent = list_entry(async_cow->extents.next,
697 struct async_extent, list);
698 list_del(&async_extent->list);
699
700 io_tree = &BTRFS_I(inode)->io_tree;
701
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500702retry:
Chris Mason771ed682008-11-06 22:02:51 -0500703 /* did the compression code fall back to uncompressed IO? */
704 if (!async_extent->pages) {
705 int page_started = 0;
706 unsigned long nr_written = 0;
707
708 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000709 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100710 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500711
712 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500713 ret = cow_file_range(inode, async_cow->locked_page,
714 async_extent->start,
715 async_extent->start +
716 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800717 async_extent->start +
718 async_extent->ram_size - 1,
719 &page_started, &nr_written, 0,
720 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500721
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100722 /* JDM XXX */
723
Chris Mason771ed682008-11-06 22:02:51 -0500724 /*
725 * if page_started, cow_file_range inserted an
726 * inline extent and took care of all the unlocking
727 * and IO for us. Otherwise, we need to submit
728 * all those pages down to the drive.
729 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500730 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500731 extent_write_locked_range(io_tree,
732 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500733 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500734 async_extent->ram_size - 1,
735 btrfs_get_extent,
736 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500737 else if (ret)
738 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500739 kfree(async_extent);
740 cond_resched();
741 continue;
742 }
743
744 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100745 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500746
Wang Xiaoguang18513092016-07-25 15:51:40 +0800747 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500748 async_extent->compressed_size,
749 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800750 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500751 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100752 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500753
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400754 if (ret == -ENOSPC) {
755 unlock_extent(io_tree, async_extent->start,
756 async_extent->start +
757 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800758
759 /*
760 * we need to redirty the pages if we decide to
761 * fallback to uncompressed IO, otherwise we
762 * will not submit these pages down to lower
763 * layers.
764 */
765 extent_range_redirty_for_io(inode,
766 async_extent->start,
767 async_extent->start +
768 async_extent->ram_size - 1);
769
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100770 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400771 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500772 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500773 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000774 /*
775 * here we're doing allocation and writeback of the
776 * compressed pages
777 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800778 em = create_io_em(inode, async_extent->start,
779 async_extent->ram_size, /* len */
780 async_extent->start, /* orig_start */
781 ins.objectid, /* block_start */
782 ins.offset, /* block_len */
783 ins.offset, /* orig_block_len */
784 async_extent->ram_size, /* ram_bytes */
785 async_extent->compress_type,
786 BTRFS_ORDERED_COMPRESSED);
787 if (IS_ERR(em))
788 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500789 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800790 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500791
Li Zefan261507a02010-12-17 14:21:50 +0800792 ret = btrfs_add_ordered_extent_compress(inode,
793 async_extent->start,
794 ins.objectid,
795 async_extent->ram_size,
796 ins.offset,
797 BTRFS_ORDERED_COMPRESSED,
798 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100799 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200800 btrfs_drop_extent_cache(BTRFS_I(inode),
801 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100802 async_extent->start +
803 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500804 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100805 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400806 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500807
Chris Mason771ed682008-11-06 22:02:51 -0500808 /*
809 * clear dirty, set writeback and unlock the pages.
810 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400811 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400812 async_extent->start +
813 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800814 async_extent->start +
815 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400816 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
817 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400818 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500819 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500820 async_extent->start,
821 async_extent->ram_size,
822 ins.objectid,
823 ins.offset, async_extent->pages,
824 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100825 if (ret) {
826 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
827 struct page *p = async_extent->pages[0];
828 const u64 start = async_extent->start;
829 const u64 end = start + async_extent->ram_size - 1;
830
831 p->mapping = inode->i_mapping;
832 tree->ops->writepage_end_io_hook(p, start, end,
833 NULL, 0);
834 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800835 extent_clear_unlock_delalloc(inode, start, end, end,
836 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100837 PAGE_END_WRITEBACK |
838 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100839 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100840 }
Chris Mason771ed682008-11-06 22:02:51 -0500841 alloc_hint = ins.objectid + ins.offset;
842 kfree(async_extent);
843 cond_resched();
844 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100845 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500846out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400847 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400848 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100849out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400850 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500851 async_extent->start +
852 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800853 async_extent->start +
854 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400855 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400856 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
857 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100858 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
859 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100860 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100861 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500862 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500863}
864
Josef Bacik4b46fce2010-05-23 11:00:55 -0400865static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
866 u64 num_bytes)
867{
868 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
869 struct extent_map *em;
870 u64 alloc_hint = 0;
871
872 read_lock(&em_tree->lock);
873 em = search_extent_mapping(em_tree, start, num_bytes);
874 if (em) {
875 /*
876 * if block start isn't an actual block number then find the
877 * first block in this inode and use that as a hint. If that
878 * block is also bogus then just don't worry about it.
879 */
880 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
881 free_extent_map(em);
882 em = search_extent_mapping(em_tree, 0, 0);
883 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
884 alloc_hint = em->block_start;
885 if (em)
886 free_extent_map(em);
887 } else {
888 alloc_hint = em->block_start;
889 free_extent_map(em);
890 }
891 }
892 read_unlock(&em_tree->lock);
893
894 return alloc_hint;
895}
896
Chris Mason771ed682008-11-06 22:02:51 -0500897/*
898 * when extent_io.c finds a delayed allocation range in the file,
899 * the call backs end up in this code. The basic idea is to
900 * allocate extents on disk for the range, and create ordered data structs
901 * in ram to track those extents.
902 *
903 * locked_page is the page that writepage had locked already. We use
904 * it to make sure we don't do extra locks or unlocks.
905 *
906 * *page_started is set to one if we unlock locked_page and do everything
907 * required to start IO on it. It may be clean and already done with
908 * IO when we return.
909 */
Josef Bacik00361582013-08-14 14:02:47 -0400910static noinline int cow_file_range(struct inode *inode,
911 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800912 u64 start, u64 end, u64 delalloc_end,
913 int *page_started, unsigned long *nr_written,
914 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500915{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400916 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400917 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500918 u64 alloc_hint = 0;
919 u64 num_bytes;
920 unsigned long ram_size;
921 u64 disk_num_bytes;
922 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400923 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500924 struct btrfs_key ins;
925 struct extent_map *em;
Chris Mason771ed682008-11-06 22:02:51 -0500926 int ret = 0;
927
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200928 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400929 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500930 ret = -EINVAL;
931 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400932 }
Chris Mason771ed682008-11-06 22:02:51 -0500933
Qu Wenruofda28322013-02-26 08:10:22 +0000934 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500935 num_bytes = max(blocksize, num_bytes);
936 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500937
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200938 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400939
Chris Mason771ed682008-11-06 22:02:51 -0500940 if (start == 0) {
941 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800942 ret = cow_file_range_inline(root, inode, start, end, 0,
943 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500944 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800945 extent_clear_unlock_delalloc(inode, start, end,
946 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400947 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400948 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400949 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
950 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800951 btrfs_free_reserved_data_space_noquota(inode, start,
952 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500953 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300954 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500955 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500956 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100957 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100958 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500959 }
960 }
Chris Masonc8b97812008-10-29 14:49:59 -0400961
962 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400963 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400964
Josef Bacik4b46fce2010-05-23 11:00:55 -0400965 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200966 btrfs_drop_extent_cache(BTRFS_I(inode), start,
967 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400968
Chris Masond3977122009-01-05 21:25:51 -0500969 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400970 unsigned long op;
971
Josef Bacik287a0ab2010-03-19 18:07:23 +0000972 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800973 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400974 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800975 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400976 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500978
Chris Mason771ed682008-11-06 22:02:51 -0500979 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -0800980 em = create_io_em(inode, start, ins.offset, /* len */
981 start, /* orig_start */
982 ins.objectid, /* block_start */
983 ins.offset, /* block_len */
984 ins.offset, /* orig_block_len */
985 ram_size, /* ram_bytes */
986 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -0800987 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -0800988 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +0000989 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800990 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400991
Chris Mason98d20f62008-04-14 09:46:10 -0400992 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400993 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500994 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000995 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +0100996 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -0400997
Yan Zheng17d217f2008-12-12 10:03:38 -0500998 if (root->root_key.objectid ==
999 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1000 ret = btrfs_reloc_clone_csums(inode, start,
1001 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001002 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001003 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001004 }
1005
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001006 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001007
Chris Masond3977122009-01-05 21:25:51 -05001008 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001009 break;
Chris Masond3977122009-01-05 21:25:51 -05001010
Chris Masonc8b97812008-10-29 14:49:59 -04001011 /* we're not doing compressed IO, don't unlock the first
1012 * page (which the caller expects to stay locked), don't
1013 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001014 *
1015 * Do set the Private2 bit so we know this page was properly
1016 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001017 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001018 op = unlock ? PAGE_UNLOCK : 0;
1019 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001020
Josef Bacikc2790a22013-07-29 11:20:47 -04001021 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001022 start + ram_size - 1,
1023 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001024 EXTENT_LOCKED | EXTENT_DELALLOC,
1025 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001026 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001027 num_bytes -= cur_alloc_size;
1028 alloc_hint = ins.objectid + ins.offset;
1029 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001030 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001031out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001032 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001033
Filipe Mananad9f85962014-08-25 10:43:00 +01001034out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001035 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001036out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001037 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001038 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001039out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001040 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1041 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001042 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1043 EXTENT_DELALLOC | EXTENT_DEFRAG,
1044 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1045 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001046 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001047}
Chris Masonc8b97812008-10-29 14:49:59 -04001048
Chris Mason771ed682008-11-06 22:02:51 -05001049/*
1050 * work queue call back to started compression on a file and pages
1051 */
1052static noinline void async_cow_start(struct btrfs_work *work)
1053{
1054 struct async_cow *async_cow;
1055 int num_added = 0;
1056 async_cow = container_of(work, struct async_cow, work);
1057
1058 compress_file_range(async_cow->inode, async_cow->locked_page,
1059 async_cow->start, async_cow->end, async_cow,
1060 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001061 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001062 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001063 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001064 }
Chris Mason771ed682008-11-06 22:02:51 -05001065}
1066
1067/*
1068 * work queue call back to submit previously compressed pages
1069 */
1070static noinline void async_cow_submit(struct btrfs_work *work)
1071{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001072 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001073 struct async_cow *async_cow;
1074 struct btrfs_root *root;
1075 unsigned long nr_pages;
1076
1077 async_cow = container_of(work, struct async_cow, work);
1078
1079 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001080 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001081 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1082 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001083
David Sterbaee863952015-02-16 19:41:40 +01001084 /*
1085 * atomic_sub_return implies a barrier for waitqueue_active
1086 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001087 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001088 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001089 waitqueue_active(&fs_info->async_submit_wait))
1090 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001091
Chris Masond3977122009-01-05 21:25:51 -05001092 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001093 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001094}
Chris Masonc8b97812008-10-29 14:49:59 -04001095
Chris Mason771ed682008-11-06 22:02:51 -05001096static noinline void async_cow_free(struct btrfs_work *work)
1097{
1098 struct async_cow *async_cow;
1099 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001100 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001101 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001102 kfree(async_cow);
1103}
1104
1105static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1106 u64 start, u64 end, int *page_started,
1107 unsigned long *nr_written)
1108{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001109 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001110 struct async_cow *async_cow;
1111 struct btrfs_root *root = BTRFS_I(inode)->root;
1112 unsigned long nr_pages;
1113 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001114
Chris Masona3429ab2009-10-08 12:30:20 -04001115 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1116 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001117 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001118 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001119 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001120 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001121 async_cow->root = root;
1122 async_cow->locked_page = locked_page;
1123 async_cow->start = start;
1124
Wang Shilongf79707b2014-07-17 11:44:09 +08001125 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001126 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001127 cur_end = end;
1128 else
Byongho Leeee221842015-12-15 01:42:10 +09001129 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001130
1131 async_cow->end = cur_end;
1132 INIT_LIST_HEAD(&async_cow->extents);
1133
Liu Bo9e0af232014-08-15 23:36:53 +08001134 btrfs_init_work(&async_cow->work,
1135 btrfs_delalloc_helper,
1136 async_cow_start, async_cow_submit,
1137 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001138
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001139 nr_pages = (cur_end - start + PAGE_SIZE) >>
1140 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001141 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001142
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001143 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001144
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001145 while (atomic_read(&fs_info->async_submit_draining) &&
1146 atomic_read(&fs_info->async_delalloc_pages)) {
1147 wait_event(fs_info->async_submit_wait,
1148 (atomic_read(&fs_info->async_delalloc_pages) ==
1149 0));
Chris Mason771ed682008-11-06 22:02:51 -05001150 }
1151
1152 *nr_written += nr_pages;
1153 start = cur_end + 1;
1154 }
1155 *page_started = 1;
1156 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001157}
1158
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001159static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001160 u64 bytenr, u64 num_bytes)
1161{
1162 int ret;
1163 struct btrfs_ordered_sum *sums;
1164 LIST_HEAD(list);
1165
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001166 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001167 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001168 if (ret == 0 && list_empty(&list))
1169 return 0;
1170
1171 while (!list_empty(&list)) {
1172 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1173 list_del(&sums->list);
1174 kfree(sums);
1175 }
1176 return 1;
1177}
1178
Chris Masond352ac62008-09-29 15:18:18 -04001179/*
1180 * when nowcow writeback call back. This checks for snapshots or COW copies
1181 * of the extents that exist in the file, and COWs the file as required.
1182 *
1183 * If no cow copies or snapshots exist, we write directly to the existing
1184 * blocks on disk
1185 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001186static noinline int run_delalloc_nocow(struct inode *inode,
1187 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001188 u64 start, u64 end, int *page_started, int force,
1189 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001190{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001191 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001192 struct btrfs_root *root = BTRFS_I(inode)->root;
1193 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001194 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001195 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001196 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001197 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001198 u64 cow_start;
1199 u64 cur_offset;
1200 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001201 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001202 u64 disk_bytenr;
1203 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001204 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001205 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001206 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001207 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001208 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 int nocow;
1210 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001211 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001212 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001213
1214 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001215 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001216 extent_clear_unlock_delalloc(inode, start, end, end,
1217 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001218 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001219 EXTENT_DO_ACCOUNTING |
1220 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001221 PAGE_CLEAR_DIRTY |
1222 PAGE_SET_WRITEBACK |
1223 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001224 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001225 }
Li Zefan82d59022011-04-20 10:33:24 +08001226
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001227 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001228
Yan Zheng80ff3852008-10-30 14:20:02 -04001229 cow_start = (u64)-1;
1230 cur_offset = start;
1231 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001232 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001233 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001234 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001235 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1237 leaf = path->nodes[0];
1238 btrfs_item_key_to_cpu(leaf, &found_key,
1239 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001240 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 found_key.type == BTRFS_EXTENT_DATA_KEY)
1242 path->slots[0]--;
1243 }
1244 check_prev = 0;
1245next_slot:
1246 leaf = path->nodes[0];
1247 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1248 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001249 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001250 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 if (ret > 0)
1252 break;
1253 leaf = path->nodes[0];
1254 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001255
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 nocow = 0;
1257 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001258 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001260
Filipe Manana1d512cb2015-11-09 00:33:58 +00001261 if (found_key.objectid > ino)
1262 break;
1263 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1264 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1265 path->slots[0]++;
1266 goto next_slot;
1267 }
1268 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 found_key.offset > end)
1270 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001271
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 if (found_key.offset > cur_offset) {
1273 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001274 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 goto out_check;
1276 }
Chris Masonc31f8832008-01-08 15:46:31 -05001277
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 fi = btrfs_item_ptr(leaf, path->slots[0],
1279 struct btrfs_file_extent_item);
1280 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001281
Josef Bacikcc95bef2013-04-04 14:31:27 -04001282 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001283 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1284 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001286 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 extent_end = found_key.offset +
1288 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001289 disk_num_bytes =
1290 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 if (extent_end <= start) {
1292 path->slots[0]++;
1293 goto next_slot;
1294 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001295 if (disk_bytenr == 0)
1296 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 if (btrfs_file_extent_compression(leaf, fi) ||
1298 btrfs_file_extent_encryption(leaf, fi) ||
1299 btrfs_file_extent_other_encoding(leaf, fi))
1300 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001301 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1302 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001303 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001305 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001306 found_key.offset -
1307 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001308 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001309 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001310 disk_bytenr += cur_offset - found_key.offset;
1311 num_bytes = min(end + 1, extent_end) - cur_offset;
1312 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001313 * if there are pending snapshots for this root,
1314 * we fall into common COW way.
1315 */
1316 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001317 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001318 if (!err)
1319 goto out_check;
1320 }
1321 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001322 * force cow if csum exists in the range.
1323 * this ensure that csum for a given extent are
1324 * either valid or do not exist.
1325 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001326 if (csum_exist_in_range(fs_info, disk_bytenr,
1327 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001328 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001329 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001330 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 nocow = 1;
1332 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1333 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001334 btrfs_file_extent_inline_len(leaf,
1335 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001336 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001337 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 } else {
1339 BUG_ON(1);
1340 }
1341out_check:
1342 if (extent_end <= start) {
1343 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001344 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001345 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001346 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001347 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 goto next_slot;
1349 }
1350 if (!nocow) {
1351 if (cow_start == (u64)-1)
1352 cow_start = cur_offset;
1353 cur_offset = extent_end;
1354 if (cur_offset > end)
1355 break;
1356 path->slots[0]++;
1357 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001358 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001359
David Sterbab3b4aa72011-04-21 01:20:15 +02001360 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001362 ret = cow_file_range(inode, locked_page,
1363 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001364 end, page_started, nr_written, 1,
1365 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001366 if (ret) {
1367 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001368 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001369 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001370 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001371 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001372 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001373 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001374 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001375 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001376
Yan Zhengd899e052008-10-30 14:25:28 -04001377 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001378 u64 orig_start = found_key.offset - extent_offset;
1379
1380 em = create_io_em(inode, cur_offset, num_bytes,
1381 orig_start,
1382 disk_bytenr, /* block_start */
1383 num_bytes, /* block_len */
1384 disk_num_bytes, /* orig_block_len */
1385 ram_bytes, BTRFS_COMPRESS_NONE,
1386 BTRFS_ORDERED_PREALLOC);
1387 if (IS_ERR(em)) {
1388 if (!nolock && nocow)
1389 btrfs_end_write_no_snapshoting(root);
1390 if (nocow)
1391 btrfs_dec_nocow_writers(fs_info,
1392 disk_bytenr);
1393 ret = PTR_ERR(em);
1394 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001395 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001396 free_extent_map(em);
1397 }
1398
1399 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001400 type = BTRFS_ORDERED_PREALLOC;
1401 } else {
1402 type = BTRFS_ORDERED_NOCOW;
1403 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001404
1405 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001406 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001407 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001408 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001409 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001410
Yan, Zhengefa56462010-05-16 10:49:59 -04001411 if (root->root_key.objectid ==
1412 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1413 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1414 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001415 if (ret) {
1416 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001417 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001418 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001419 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001420 }
1421
Josef Bacikc2790a22013-07-29 11:20:47 -04001422 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001423 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001424 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001425 EXTENT_DELALLOC |
1426 EXTENT_CLEAR_DATA_RESV,
1427 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1428
Wang Shilonge9894fd2014-03-27 11:12:25 +08001429 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001430 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001431 cur_offset = extent_end;
1432 if (cur_offset > end)
1433 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001434 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001435 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001436
Josef Bacik17ca04a2012-05-31 15:58:55 -04001437 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001438 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001439 cur_offset = end;
1440 }
1441
Yan Zheng80ff3852008-10-30 14:20:02 -04001442 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001443 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1444 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001445 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001446 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001447 }
1448
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001449error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001450 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001451 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001452 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001453 EXTENT_DELALLOC | EXTENT_DEFRAG |
1454 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1455 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001456 PAGE_SET_WRITEBACK |
1457 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001458 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001459 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001460}
1461
Wang Shilong47059d92014-07-03 18:22:07 +08001462static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1463{
1464
1465 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1466 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1467 return 0;
1468
1469 /*
1470 * @defrag_bytes is a hint value, no spinlock held here,
1471 * if is not zero, it means the file is defragging.
1472 * Force cow if given extent needs to be defragged.
1473 */
1474 if (BTRFS_I(inode)->defrag_bytes &&
1475 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1476 EXTENT_DEFRAG, 0, NULL))
1477 return 1;
1478
1479 return 0;
1480}
1481
Chris Masond352ac62008-09-29 15:18:18 -04001482/*
1483 * extent_io.c call back to do delayed allocation processing
1484 */
Chris Masonc8b97812008-10-29 14:49:59 -04001485static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001486 u64 start, u64 end, int *page_started,
1487 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001488{
Chris Masonbe20aa92007-12-17 20:14:01 -05001489 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001490 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001491
Wang Shilong47059d92014-07-03 18:22:07 +08001492 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001493 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001494 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001495 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001496 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001497 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001498 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001499 ret = cow_file_range(inode, locked_page, start, end, end,
1500 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001501 } else {
1502 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1503 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001504 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001505 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001506 }
Chris Masonb888db22007-08-27 16:49:44 -04001507 return ret;
1508}
1509
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001510static void btrfs_split_extent_hook(struct inode *inode,
1511 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001512{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001513 u64 size;
1514
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001515 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001516 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001517 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001518
Josef Bacikdcab6a32015-02-11 15:08:59 -05001519 size = orig->end - orig->start + 1;
1520 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001521 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001522 u64 new_size;
1523
1524 /*
Josef Bacikba117212015-03-13 15:01:24 -04001525 * See the explanation in btrfs_merge_extent_hook, the same
1526 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001527 */
1528 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001529 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001530 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001531 num_extents += count_max_extents(new_size);
1532 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001533 return;
1534 }
1535
Josef Bacik9e0baf62011-07-15 15:16:44 +00001536 spin_lock(&BTRFS_I(inode)->lock);
1537 BTRFS_I(inode)->outstanding_extents++;
1538 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001539}
1540
1541/*
1542 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1543 * extents so we can keep track of new extents that are just merged onto old
1544 * extents, such as when we are doing sequential writes, so we can properly
1545 * account for the metadata space we'll need.
1546 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001547static void btrfs_merge_extent_hook(struct inode *inode,
1548 struct extent_state *new,
1549 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001550{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001551 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001552 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001553
Josef Bacik9ed74f22009-09-11 16:12:44 -04001554 /* not delalloc, ignore it */
1555 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001556 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001557
Josef Bacik8461a3d2015-03-13 15:12:08 -04001558 if (new->start > other->start)
1559 new_size = new->end - other->start + 1;
1560 else
1561 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001562
1563 /* we're not bigger than the max, unreserve the space and go */
1564 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1565 spin_lock(&BTRFS_I(inode)->lock);
1566 BTRFS_I(inode)->outstanding_extents--;
1567 spin_unlock(&BTRFS_I(inode)->lock);
1568 return;
1569 }
1570
1571 /*
Josef Bacikba117212015-03-13 15:01:24 -04001572 * We have to add up either side to figure out how many extents were
1573 * accounted for before we merged into one big extent. If the number of
1574 * extents we accounted for is <= the amount we need for the new range
1575 * then we can return, otherwise drop. Think of it like this
1576 *
1577 * [ 4k][MAX_SIZE]
1578 *
1579 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1580 * need 2 outstanding extents, on one side we have 1 and the other side
1581 * we have 1 so they are == and we can return. But in this case
1582 *
1583 * [MAX_SIZE+4k][MAX_SIZE+4k]
1584 *
1585 * Each range on their own accounts for 2 extents, but merged together
1586 * they are only 3 extents worth of accounting, so we need to drop in
1587 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001588 */
Josef Bacikba117212015-03-13 15:01:24 -04001589 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001590 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001591 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001592 num_extents += count_max_extents(old_size);
1593 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001594 return;
1595
Josef Bacik9e0baf62011-07-15 15:16:44 +00001596 spin_lock(&BTRFS_I(inode)->lock);
1597 BTRFS_I(inode)->outstanding_extents--;
1598 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001599}
1600
Miao Xieeb73c1b2013-05-15 07:48:22 +00001601static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1602 struct inode *inode)
1603{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001604 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1605
Miao Xieeb73c1b2013-05-15 07:48:22 +00001606 spin_lock(&root->delalloc_lock);
1607 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1608 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1609 &root->delalloc_inodes);
1610 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1611 &BTRFS_I(inode)->runtime_flags);
1612 root->nr_delalloc_inodes++;
1613 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001614 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001615 BUG_ON(!list_empty(&root->delalloc_root));
1616 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001617 &fs_info->delalloc_roots);
1618 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001619 }
1620 }
1621 spin_unlock(&root->delalloc_lock);
1622}
1623
1624static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001625 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001626{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001627 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001628
Miao Xieeb73c1b2013-05-15 07:48:22 +00001629 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001630 if (!list_empty(&inode->delalloc_inodes)) {
1631 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001632 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001633 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001634 root->nr_delalloc_inodes--;
1635 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001636 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001637 BUG_ON(list_empty(&root->delalloc_root));
1638 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001639 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001640 }
1641 }
1642 spin_unlock(&root->delalloc_lock);
1643}
1644
Chris Masond352ac62008-09-29 15:18:18 -04001645/*
1646 * extent_io.c set_bit_hook, used to track delayed allocation
1647 * bytes in this file, and to maintain the list of inodes that
1648 * have pending delalloc work to be done.
1649 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001650static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001651 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001652{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001653
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001654 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1655
Wang Shilong47059d92014-07-03 18:22:07 +08001656 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1657 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001658 /*
1659 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001660 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001661 * bit, which is only set or cleared with irqs on
1662 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001663 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001664 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001665 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001666 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001667
Josef Bacik9e0baf62011-07-15 15:16:44 +00001668 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001669 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001670 } else {
1671 spin_lock(&BTRFS_I(inode)->lock);
1672 BTRFS_I(inode)->outstanding_extents++;
1673 spin_unlock(&BTRFS_I(inode)->lock);
1674 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001675
Josef Bacik6a3891c2015-03-16 17:38:52 -04001676 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001677 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001678 return;
1679
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001680 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1681 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001682 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001683 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001684 if (*bits & EXTENT_DEFRAG)
1685 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001686 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001687 &BTRFS_I(inode)->runtime_flags))
1688 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001689 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001690 }
Chris Mason291d6732008-01-29 15:55:23 -05001691}
1692
Chris Masond352ac62008-09-29 15:18:18 -04001693/*
1694 * extent_io.c clear_bit_hook, see set_bit_hook for why
1695 */
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001696static void btrfs_clear_bit_hook(struct btrfs_inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001697 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001698 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001699{
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001700 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001701 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001702 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001703
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001704 spin_lock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001705 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001706 inode->defrag_bytes -= len;
1707 spin_unlock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001708
Chris Mason75eff682008-12-15 15:54:40 -05001709 /*
1710 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001711 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001712 * bit, which is only set or cleared with irqs on
1713 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001714 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001715 struct btrfs_root *root = inode->root;
1716 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001717
Josef Bacik9e0baf62011-07-15 15:16:44 +00001718 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001719 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001720 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001721 spin_lock(&inode->lock);
1722 inode->outstanding_extents -= num_extents;
1723 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001724 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001725
Josef Bacikb6d08f02013-09-27 14:57:43 -04001726 /*
1727 * We don't reserve metadata space for space cache inodes so we
1728 * don't need to call dellalloc_release_metadata if there is an
1729 * error.
1730 */
1731 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001732 root != fs_info->tree_root)
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001733 btrfs_delalloc_release_metadata(inode, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001734
Josef Bacik6a3891c2015-03-16 17:38:52 -04001735 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001736 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001737 return;
1738
Josef Bacik0cb59c92010-07-02 12:14:14 -04001739 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001740 && do_list && !(state->state & EXTENT_NORESERVE)
1741 && (*bits & (EXTENT_DO_ACCOUNTING |
1742 EXTENT_CLEAR_DATA_RESV)))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001743 btrfs_free_reserved_data_space_noquota(
1744 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001745 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001746
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001747 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1748 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001749 spin_lock(&inode->lock);
1750 inode->delalloc_bytes -= len;
1751 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001752 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001753 &inode->runtime_flags))
1754 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001755 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001756 }
Chris Mason291d6732008-01-29 15:55:23 -05001757}
1758
Chris Masond352ac62008-09-29 15:18:18 -04001759/*
1760 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1761 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001762 *
1763 * return 1 if page cannot be merged to bio
1764 * return 0 if page can be merged to bio
1765 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001766 */
Mike Christie81a75f672016-06-05 14:31:54 -05001767int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001768 size_t size, struct bio *bio,
1769 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001770{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001771 struct inode *inode = page->mapping->host;
1772 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001773 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001774 u64 length = 0;
1775 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001776 int ret;
1777
Chris Mason771ed682008-11-06 22:02:51 -05001778 if (bio_flags & EXTENT_BIO_COMPRESSED)
1779 return 0;
1780
Kent Overstreet4f024f32013-10-11 15:44:27 -07001781 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001782 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001783 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1784 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001785 if (ret < 0)
1786 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001787 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001788 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001789 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001790}
1791
Chris Masond352ac62008-09-29 15:18:18 -04001792/*
1793 * in order to insert checksums into the metadata in large chunks,
1794 * we wait until bio submission time. All the pages in the bio are
1795 * checksummed and sums are attached onto the ordered extent record.
1796 *
1797 * At IO completion time the cums attached on the ordered extent record
1798 * are inserted into the btree
1799 */
Mike Christie81a75f672016-06-05 14:31:54 -05001800static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1801 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001802 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001803{
Chris Mason065631f2008-02-20 12:07:25 -05001804 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001805
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001806 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001807 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001808 return 0;
1809}
Chris Masone0156402008-04-16 11:15:20 -04001810
Chris Mason4a69a412008-11-06 22:03:00 -05001811/*
1812 * in order to insert checksums into the metadata in large chunks,
1813 * we wait until bio submission time. All the pages in the bio are
1814 * checksummed and sums are attached onto the ordered extent record.
1815 *
1816 * At IO completion time the cums attached on the ordered extent record
1817 * are inserted into the btree
1818 */
Mike Christie81a75f672016-06-05 14:31:54 -05001819static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001820 int mirror_num, unsigned long bio_flags,
1821 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001822{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001823 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001824 int ret;
1825
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001826 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001827 if (ret) {
1828 bio->bi_error = ret;
1829 bio_endio(bio);
1830 }
Stefan Behrens61891922012-11-05 18:51:52 +01001831 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001832}
1833
Chris Masond352ac62008-09-29 15:18:18 -04001834/*
Chris Masoncad321a2008-12-17 14:51:42 -05001835 * extent_io.c submission hook. This does the right thing for csum calculation
1836 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001837 */
Mike Christie81a75f672016-06-05 14:31:54 -05001838static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001839 int mirror_num, unsigned long bio_flags,
1840 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001841{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001842 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001843 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301844 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001845 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001846 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001847 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001848
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001849 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001850
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001851 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301852 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001853
Mike Christie37226b22016-06-05 14:31:52 -05001854 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001855 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001856 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001857 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001858
Chris Masond20f7042008-12-08 16:58:54 -05001859 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001860 ret = btrfs_submit_compressed_read(inode, bio,
1861 mirror_num,
1862 bio_flags);
1863 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001864 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001865 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001866 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001867 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001868 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001869 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001870 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001871 /* csum items have already been cloned */
1872 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1873 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001874 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001875 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1876 bio_flags, bio_offset,
1877 __btrfs_submit_bio_start,
1878 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001879 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001880 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001881 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001882 if (ret)
1883 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001884 }
1885
Chris Mason0b86a832008-03-24 15:01:56 -04001886mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001887 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001888
1889out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001890 if (ret < 0) {
1891 bio->bi_error = ret;
1892 bio_endio(bio);
1893 }
Stefan Behrens61891922012-11-05 18:51:52 +01001894 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001895}
Chris Mason6885f302008-02-20 16:11:05 -05001896
Chris Masond352ac62008-09-29 15:18:18 -04001897/*
1898 * given a list of ordered sums record them in the inode. This happens
1899 * at IO completion time based on sums calculated at bio submission time.
1900 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001901static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01001902 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001903{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001904 struct btrfs_ordered_sum *sum;
1905
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001906 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001907 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001908 btrfs_csum_file_blocks(trans,
1909 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001910 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001911 }
1912 return 0;
1913}
1914
Josef Bacik2ac55d42010-02-03 19:33:23 +00001915int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001916 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04001917{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001918 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001919 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001920 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001921}
1922
Chris Masond352ac62008-09-29 15:18:18 -04001923/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001924struct btrfs_writepage_fixup {
1925 struct page *page;
1926 struct btrfs_work work;
1927};
1928
Christoph Hellwigb2950862008-12-02 09:54:17 -05001929static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001930{
1931 struct btrfs_writepage_fixup *fixup;
1932 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001933 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001934 struct page *page;
1935 struct inode *inode;
1936 u64 page_start;
1937 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001938 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001939
1940 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1941 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001942again:
Chris Mason247e7432008-07-17 12:53:51 -04001943 lock_page(page);
1944 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1945 ClearPageChecked(page);
1946 goto out_page;
1947 }
1948
1949 inode = page->mapping->host;
1950 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001951 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04001952
David Sterbaff13db42015-12-03 14:30:40 +01001953 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001954 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001955
1956 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001957 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001958 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001959
Nikolay Borisova776c6f2017-02-20 13:50:49 +02001960 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001961 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04001962 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001963 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1964 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001965 unlock_page(page);
1966 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001967 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001968 goto again;
1969 }
Chris Mason247e7432008-07-17 12:53:51 -04001970
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001971 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001972 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001973 if (ret) {
1974 mapping_set_error(page->mapping, ret);
1975 end_extent_writepage(page, ret, page_start, page_end);
1976 ClearPageChecked(page);
1977 goto out;
1978 }
1979
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001980 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
1981 0);
Chris Mason247e7432008-07-17 12:53:51 -04001982 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001983 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001984out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001985 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1986 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001987out_page:
1988 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001989 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001990 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001991}
1992
1993/*
1994 * There are a few paths in the higher layers of the kernel that directly
1995 * set the page dirty bit without asking the filesystem if it is a
1996 * good idea. This causes problems because we want to make sure COW
1997 * properly happens and the data=ordered rules are followed.
1998 *
Chris Masonc8b97812008-10-29 14:49:59 -04001999 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002000 * hasn't been properly setup for IO. We kick off an async process
2001 * to fix it up. The async helper will wait for ordered extents, set
2002 * the delalloc bit and make it safe to write the page.
2003 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002004static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002005{
2006 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002007 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002008 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002009
Chris Mason8b62b722009-09-02 16:53:46 -04002010 /* this page is properly in the ordered list */
2011 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002012 return 0;
2013
2014 if (PageChecked(page))
2015 return -EAGAIN;
2016
2017 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2018 if (!fixup)
2019 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002020
Chris Mason247e7432008-07-17 12:53:51 -04002021 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002022 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002023 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2024 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002025 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002026 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002027 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002028}
2029
Yan Zhengd899e052008-10-30 14:25:28 -04002030static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2031 struct inode *inode, u64 file_pos,
2032 u64 disk_bytenr, u64 disk_num_bytes,
2033 u64 num_bytes, u64 ram_bytes,
2034 u8 compression, u8 encryption,
2035 u16 other_encoding, int extent_type)
2036{
2037 struct btrfs_root *root = BTRFS_I(inode)->root;
2038 struct btrfs_file_extent_item *fi;
2039 struct btrfs_path *path;
2040 struct extent_buffer *leaf;
2041 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002042 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002043 int ret;
2044
2045 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002046 if (!path)
2047 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002048
Chris Masona1ed8352009-09-11 12:27:37 -04002049 /*
2050 * we may be replacing one extent in the tree with another.
2051 * The new extent is pinned in the extent map, and we don't want
2052 * to drop it from the cache until it is completely in the btree.
2053 *
2054 * So, tell btrfs_drop_extents to leave this extent in the cache.
2055 * the caller is expected to unpin it and allow it to be merged
2056 * with the others.
2057 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002058 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2059 file_pos + num_bytes, NULL, 0,
2060 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002061 if (ret)
2062 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002063
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002064 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002065 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002066 ins.offset = file_pos;
2067 ins.type = BTRFS_EXTENT_DATA_KEY;
2068
2069 path->leave_spinning = 1;
2070 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2071 sizeof(*fi));
2072 if (ret)
2073 goto out;
2074 }
Yan Zhengd899e052008-10-30 14:25:28 -04002075 leaf = path->nodes[0];
2076 fi = btrfs_item_ptr(leaf, path->slots[0],
2077 struct btrfs_file_extent_item);
2078 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2079 btrfs_set_file_extent_type(leaf, fi, extent_type);
2080 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2081 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2082 btrfs_set_file_extent_offset(leaf, fi, 0);
2083 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2084 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2085 btrfs_set_file_extent_compression(leaf, fi, compression);
2086 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2087 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002088
Yan Zhengd899e052008-10-30 14:25:28 -04002089 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002090 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002091
2092 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002093
2094 ins.objectid = disk_bytenr;
2095 ins.offset = disk_num_bytes;
2096 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002097 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002098 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002099 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002100 * Release the reserved range from inode dirty range map, as it is
2101 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002102 */
2103 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002104out:
Yan Zhengd899e052008-10-30 14:25:28 -04002105 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002106
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002107 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002108}
2109
Liu Bo38c227d2013-01-29 03:18:40 +00002110/* snapshot-aware defrag */
2111struct sa_defrag_extent_backref {
2112 struct rb_node node;
2113 struct old_sa_defrag_extent *old;
2114 u64 root_id;
2115 u64 inum;
2116 u64 file_pos;
2117 u64 extent_offset;
2118 u64 num_bytes;
2119 u64 generation;
2120};
2121
2122struct old_sa_defrag_extent {
2123 struct list_head list;
2124 struct new_sa_defrag_extent *new;
2125
2126 u64 extent_offset;
2127 u64 bytenr;
2128 u64 offset;
2129 u64 len;
2130 int count;
2131};
2132
2133struct new_sa_defrag_extent {
2134 struct rb_root root;
2135 struct list_head head;
2136 struct btrfs_path *path;
2137 struct inode *inode;
2138 u64 file_pos;
2139 u64 len;
2140 u64 bytenr;
2141 u64 disk_len;
2142 u8 compress_type;
2143};
2144
2145static int backref_comp(struct sa_defrag_extent_backref *b1,
2146 struct sa_defrag_extent_backref *b2)
2147{
2148 if (b1->root_id < b2->root_id)
2149 return -1;
2150 else if (b1->root_id > b2->root_id)
2151 return 1;
2152
2153 if (b1->inum < b2->inum)
2154 return -1;
2155 else if (b1->inum > b2->inum)
2156 return 1;
2157
2158 if (b1->file_pos < b2->file_pos)
2159 return -1;
2160 else if (b1->file_pos > b2->file_pos)
2161 return 1;
2162
2163 /*
2164 * [------------------------------] ===> (a range of space)
2165 * |<--->| |<---->| =============> (fs/file tree A)
2166 * |<---------------------------->| ===> (fs/file tree B)
2167 *
2168 * A range of space can refer to two file extents in one tree while
2169 * refer to only one file extent in another tree.
2170 *
2171 * So we may process a disk offset more than one time(two extents in A)
2172 * and locate at the same extent(one extent in B), then insert two same
2173 * backrefs(both refer to the extent in B).
2174 */
2175 return 0;
2176}
2177
2178static void backref_insert(struct rb_root *root,
2179 struct sa_defrag_extent_backref *backref)
2180{
2181 struct rb_node **p = &root->rb_node;
2182 struct rb_node *parent = NULL;
2183 struct sa_defrag_extent_backref *entry;
2184 int ret;
2185
2186 while (*p) {
2187 parent = *p;
2188 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2189
2190 ret = backref_comp(backref, entry);
2191 if (ret < 0)
2192 p = &(*p)->rb_left;
2193 else
2194 p = &(*p)->rb_right;
2195 }
2196
2197 rb_link_node(&backref->node, parent, p);
2198 rb_insert_color(&backref->node, root);
2199}
2200
2201/*
2202 * Note the backref might has changed, and in this case we just return 0.
2203 */
2204static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2205 void *ctx)
2206{
2207 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002208 struct old_sa_defrag_extent *old = ctx;
2209 struct new_sa_defrag_extent *new = old->new;
2210 struct btrfs_path *path = new->path;
2211 struct btrfs_key key;
2212 struct btrfs_root *root;
2213 struct sa_defrag_extent_backref *backref;
2214 struct extent_buffer *leaf;
2215 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002216 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002217 int slot;
2218 int ret;
2219 u64 extent_offset;
2220 u64 num_bytes;
2221
2222 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002223 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002224 return 0;
2225
2226 key.objectid = root_id;
2227 key.type = BTRFS_ROOT_ITEM_KEY;
2228 key.offset = (u64)-1;
2229
Liu Bo38c227d2013-01-29 03:18:40 +00002230 root = btrfs_read_fs_root_no_name(fs_info, &key);
2231 if (IS_ERR(root)) {
2232 if (PTR_ERR(root) == -ENOENT)
2233 return 0;
2234 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002235 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002236 inum, offset, root_id);
2237 return PTR_ERR(root);
2238 }
2239
2240 key.objectid = inum;
2241 key.type = BTRFS_EXTENT_DATA_KEY;
2242 if (offset > (u64)-1 << 32)
2243 key.offset = 0;
2244 else
2245 key.offset = offset;
2246
2247 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302248 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002249 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002250 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002251
2252 while (1) {
2253 cond_resched();
2254
2255 leaf = path->nodes[0];
2256 slot = path->slots[0];
2257
2258 if (slot >= btrfs_header_nritems(leaf)) {
2259 ret = btrfs_next_leaf(root, path);
2260 if (ret < 0) {
2261 goto out;
2262 } else if (ret > 0) {
2263 ret = 0;
2264 goto out;
2265 }
2266 continue;
2267 }
2268
2269 path->slots[0]++;
2270
2271 btrfs_item_key_to_cpu(leaf, &key, slot);
2272
2273 if (key.objectid > inum)
2274 goto out;
2275
2276 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2277 continue;
2278
2279 extent = btrfs_item_ptr(leaf, slot,
2280 struct btrfs_file_extent_item);
2281
2282 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2283 continue;
2284
Liu Boe68afa42013-07-01 22:13:26 +08002285 /*
2286 * 'offset' refers to the exact key.offset,
2287 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2288 * (key.offset - extent_offset).
2289 */
2290 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002291 continue;
2292
Liu Boe68afa42013-07-01 22:13:26 +08002293 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002294 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002295
Liu Bo38c227d2013-01-29 03:18:40 +00002296 if (extent_offset >= old->extent_offset + old->offset +
2297 old->len || extent_offset + num_bytes <=
2298 old->extent_offset + old->offset)
2299 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002300 break;
2301 }
2302
2303 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2304 if (!backref) {
2305 ret = -ENOENT;
2306 goto out;
2307 }
2308
2309 backref->root_id = root_id;
2310 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002311 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002312 backref->num_bytes = num_bytes;
2313 backref->extent_offset = extent_offset;
2314 backref->generation = btrfs_file_extent_generation(leaf, extent);
2315 backref->old = old;
2316 backref_insert(&new->root, backref);
2317 old->count++;
2318out:
2319 btrfs_release_path(path);
2320 WARN_ON(ret);
2321 return ret;
2322}
2323
2324static noinline bool record_extent_backrefs(struct btrfs_path *path,
2325 struct new_sa_defrag_extent *new)
2326{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002327 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002328 struct old_sa_defrag_extent *old, *tmp;
2329 int ret;
2330
2331 new->path = path;
2332
2333 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002334 ret = iterate_inodes_from_logical(old->bytenr +
2335 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002336 path, record_one_backref,
2337 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002338 if (ret < 0 && ret != -ENOENT)
2339 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002340
2341 /* no backref to be processed for this extent */
2342 if (!old->count) {
2343 list_del(&old->list);
2344 kfree(old);
2345 }
2346 }
2347
2348 if (list_empty(&new->head))
2349 return false;
2350
2351 return true;
2352}
2353
2354static int relink_is_mergable(struct extent_buffer *leaf,
2355 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002356 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002357{
Liu Bo116e0022013-08-02 16:30:40 +08002358 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002359 return 0;
2360
2361 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2362 return 0;
2363
Liu Bo116e0022013-08-02 16:30:40 +08002364 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2365 return 0;
2366
2367 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002368 btrfs_file_extent_other_encoding(leaf, fi))
2369 return 0;
2370
2371 return 1;
2372}
2373
2374/*
2375 * Note the backref might has changed, and in this case we just return 0.
2376 */
2377static noinline int relink_extent_backref(struct btrfs_path *path,
2378 struct sa_defrag_extent_backref *prev,
2379 struct sa_defrag_extent_backref *backref)
2380{
2381 struct btrfs_file_extent_item *extent;
2382 struct btrfs_file_extent_item *item;
2383 struct btrfs_ordered_extent *ordered;
2384 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002385 struct btrfs_root *root;
2386 struct btrfs_key key;
2387 struct extent_buffer *leaf;
2388 struct old_sa_defrag_extent *old = backref->old;
2389 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002390 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002391 struct inode *inode;
2392 struct extent_state *cached = NULL;
2393 int ret = 0;
2394 u64 start;
2395 u64 len;
2396 u64 lock_start;
2397 u64 lock_end;
2398 bool merge = false;
2399 int index;
2400
2401 if (prev && prev->root_id == backref->root_id &&
2402 prev->inum == backref->inum &&
2403 prev->file_pos + prev->num_bytes == backref->file_pos)
2404 merge = true;
2405
2406 /* step 1: get root */
2407 key.objectid = backref->root_id;
2408 key.type = BTRFS_ROOT_ITEM_KEY;
2409 key.offset = (u64)-1;
2410
Liu Bo38c227d2013-01-29 03:18:40 +00002411 index = srcu_read_lock(&fs_info->subvol_srcu);
2412
2413 root = btrfs_read_fs_root_no_name(fs_info, &key);
2414 if (IS_ERR(root)) {
2415 srcu_read_unlock(&fs_info->subvol_srcu, index);
2416 if (PTR_ERR(root) == -ENOENT)
2417 return 0;
2418 return PTR_ERR(root);
2419 }
Liu Bo38c227d2013-01-29 03:18:40 +00002420
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002421 if (btrfs_root_readonly(root)) {
2422 srcu_read_unlock(&fs_info->subvol_srcu, index);
2423 return 0;
2424 }
2425
Liu Bo38c227d2013-01-29 03:18:40 +00002426 /* step 2: get inode */
2427 key.objectid = backref->inum;
2428 key.type = BTRFS_INODE_ITEM_KEY;
2429 key.offset = 0;
2430
2431 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2432 if (IS_ERR(inode)) {
2433 srcu_read_unlock(&fs_info->subvol_srcu, index);
2434 return 0;
2435 }
2436
2437 srcu_read_unlock(&fs_info->subvol_srcu, index);
2438
2439 /* step 3: relink backref */
2440 lock_start = backref->file_pos;
2441 lock_end = backref->file_pos + backref->num_bytes - 1;
2442 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002443 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002444
2445 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2446 if (ordered) {
2447 btrfs_put_ordered_extent(ordered);
2448 goto out_unlock;
2449 }
2450
2451 trans = btrfs_join_transaction(root);
2452 if (IS_ERR(trans)) {
2453 ret = PTR_ERR(trans);
2454 goto out_unlock;
2455 }
2456
2457 key.objectid = backref->inum;
2458 key.type = BTRFS_EXTENT_DATA_KEY;
2459 key.offset = backref->file_pos;
2460
2461 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2462 if (ret < 0) {
2463 goto out_free_path;
2464 } else if (ret > 0) {
2465 ret = 0;
2466 goto out_free_path;
2467 }
2468
2469 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2470 struct btrfs_file_extent_item);
2471
2472 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2473 backref->generation)
2474 goto out_free_path;
2475
2476 btrfs_release_path(path);
2477
2478 start = backref->file_pos;
2479 if (backref->extent_offset < old->extent_offset + old->offset)
2480 start += old->extent_offset + old->offset -
2481 backref->extent_offset;
2482
2483 len = min(backref->extent_offset + backref->num_bytes,
2484 old->extent_offset + old->offset + old->len);
2485 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2486
2487 ret = btrfs_drop_extents(trans, root, inode, start,
2488 start + len, 1);
2489 if (ret)
2490 goto out_free_path;
2491again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002492 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002493 key.type = BTRFS_EXTENT_DATA_KEY;
2494 key.offset = start;
2495
Liu Boa09a0a72013-03-11 09:20:58 +00002496 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002497 if (merge) {
2498 struct btrfs_file_extent_item *fi;
2499 u64 extent_len;
2500 struct btrfs_key found_key;
2501
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002502 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002503 if (ret < 0)
2504 goto out_free_path;
2505
2506 path->slots[0]--;
2507 leaf = path->nodes[0];
2508 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2509
2510 fi = btrfs_item_ptr(leaf, path->slots[0],
2511 struct btrfs_file_extent_item);
2512 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2513
Liu Bo116e0022013-08-02 16:30:40 +08002514 if (extent_len + found_key.offset == start &&
2515 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002516 btrfs_set_file_extent_num_bytes(leaf, fi,
2517 extent_len + len);
2518 btrfs_mark_buffer_dirty(leaf);
2519 inode_add_bytes(inode, len);
2520
2521 ret = 1;
2522 goto out_free_path;
2523 } else {
2524 merge = false;
2525 btrfs_release_path(path);
2526 goto again;
2527 }
2528 }
2529
2530 ret = btrfs_insert_empty_item(trans, root, path, &key,
2531 sizeof(*extent));
2532 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002533 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002534 goto out_free_path;
2535 }
2536
2537 leaf = path->nodes[0];
2538 item = btrfs_item_ptr(leaf, path->slots[0],
2539 struct btrfs_file_extent_item);
2540 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2541 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2542 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2543 btrfs_set_file_extent_num_bytes(leaf, item, len);
2544 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2545 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2546 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2547 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2548 btrfs_set_file_extent_encryption(leaf, item, 0);
2549 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2550
2551 btrfs_mark_buffer_dirty(leaf);
2552 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002553 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002554
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002555 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002556 new->disk_len, 0,
2557 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002558 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002559 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002560 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002561 goto out_free_path;
2562 }
2563
2564 ret = 1;
2565out_free_path:
2566 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002567 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002568 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002569out_unlock:
2570 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2571 &cached, GFP_NOFS);
2572 iput(inode);
2573 return ret;
2574}
2575
Liu Bo6f519562013-10-29 10:45:05 +08002576static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2577{
2578 struct old_sa_defrag_extent *old, *tmp;
2579
2580 if (!new)
2581 return;
2582
2583 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002584 kfree(old);
2585 }
2586 kfree(new);
2587}
2588
Liu Bo38c227d2013-01-29 03:18:40 +00002589static void relink_file_extents(struct new_sa_defrag_extent *new)
2590{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002591 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002592 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002593 struct sa_defrag_extent_backref *backref;
2594 struct sa_defrag_extent_backref *prev = NULL;
2595 struct inode *inode;
2596 struct btrfs_root *root;
2597 struct rb_node *node;
2598 int ret;
2599
2600 inode = new->inode;
2601 root = BTRFS_I(inode)->root;
2602
2603 path = btrfs_alloc_path();
2604 if (!path)
2605 return;
2606
2607 if (!record_extent_backrefs(path, new)) {
2608 btrfs_free_path(path);
2609 goto out;
2610 }
2611 btrfs_release_path(path);
2612
2613 while (1) {
2614 node = rb_first(&new->root);
2615 if (!node)
2616 break;
2617 rb_erase(node, &new->root);
2618
2619 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2620
2621 ret = relink_extent_backref(path, prev, backref);
2622 WARN_ON(ret < 0);
2623
2624 kfree(prev);
2625
2626 if (ret == 1)
2627 prev = backref;
2628 else
2629 prev = NULL;
2630 cond_resched();
2631 }
2632 kfree(prev);
2633
2634 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002635out:
Liu Bo6f519562013-10-29 10:45:05 +08002636 free_sa_defrag_extent(new);
2637
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002638 atomic_dec(&fs_info->defrag_running);
2639 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002640}
2641
2642static struct new_sa_defrag_extent *
2643record_old_file_extents(struct inode *inode,
2644 struct btrfs_ordered_extent *ordered)
2645{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002646 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002647 struct btrfs_root *root = BTRFS_I(inode)->root;
2648 struct btrfs_path *path;
2649 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002650 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002651 struct new_sa_defrag_extent *new;
2652 int ret;
2653
2654 new = kmalloc(sizeof(*new), GFP_NOFS);
2655 if (!new)
2656 return NULL;
2657
2658 new->inode = inode;
2659 new->file_pos = ordered->file_offset;
2660 new->len = ordered->len;
2661 new->bytenr = ordered->start;
2662 new->disk_len = ordered->disk_len;
2663 new->compress_type = ordered->compress_type;
2664 new->root = RB_ROOT;
2665 INIT_LIST_HEAD(&new->head);
2666
2667 path = btrfs_alloc_path();
2668 if (!path)
2669 goto out_kfree;
2670
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002671 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002672 key.type = BTRFS_EXTENT_DATA_KEY;
2673 key.offset = new->file_pos;
2674
2675 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2676 if (ret < 0)
2677 goto out_free_path;
2678 if (ret > 0 && path->slots[0] > 0)
2679 path->slots[0]--;
2680
2681 /* find out all the old extents for the file range */
2682 while (1) {
2683 struct btrfs_file_extent_item *extent;
2684 struct extent_buffer *l;
2685 int slot;
2686 u64 num_bytes;
2687 u64 offset;
2688 u64 end;
2689 u64 disk_bytenr;
2690 u64 extent_offset;
2691
2692 l = path->nodes[0];
2693 slot = path->slots[0];
2694
2695 if (slot >= btrfs_header_nritems(l)) {
2696 ret = btrfs_next_leaf(root, path);
2697 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002698 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002699 else if (ret > 0)
2700 break;
2701 continue;
2702 }
2703
2704 btrfs_item_key_to_cpu(l, &key, slot);
2705
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002706 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002707 break;
2708 if (key.type != BTRFS_EXTENT_DATA_KEY)
2709 break;
2710 if (key.offset >= new->file_pos + new->len)
2711 break;
2712
2713 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2714
2715 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2716 if (key.offset + num_bytes < new->file_pos)
2717 goto next;
2718
2719 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2720 if (!disk_bytenr)
2721 goto next;
2722
2723 extent_offset = btrfs_file_extent_offset(l, extent);
2724
2725 old = kmalloc(sizeof(*old), GFP_NOFS);
2726 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002727 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002728
2729 offset = max(new->file_pos, key.offset);
2730 end = min(new->file_pos + new->len, key.offset + num_bytes);
2731
2732 old->bytenr = disk_bytenr;
2733 old->extent_offset = extent_offset;
2734 old->offset = offset - key.offset;
2735 old->len = end - offset;
2736 old->new = new;
2737 old->count = 0;
2738 list_add_tail(&old->list, &new->head);
2739next:
2740 path->slots[0]++;
2741 cond_resched();
2742 }
2743
2744 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002745 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002746
2747 return new;
2748
Liu Bo38c227d2013-01-29 03:18:40 +00002749out_free_path:
2750 btrfs_free_path(path);
2751out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002752 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002753 return NULL;
2754}
2755
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002756static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002757 u64 start, u64 len)
2758{
2759 struct btrfs_block_group_cache *cache;
2760
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002761 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002762 ASSERT(cache);
2763
2764 spin_lock(&cache->lock);
2765 cache->delalloc_bytes -= len;
2766 spin_unlock(&cache->lock);
2767
2768 btrfs_put_block_group(cache);
2769}
2770
Chris Masond352ac62008-09-29 15:18:18 -04002771/* as ordered data IO finishes, this gets called so we can finish
2772 * an ordered extent if the range of bytes in the file it covers are
2773 * fully written.
2774 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002775static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002776{
Josef Bacik5fd02042012-05-02 14:00:54 -04002777 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002778 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002779 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002780 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002781 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002782 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002783 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002784 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002785 int ret = 0;
2786 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002787 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002788 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002789
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002790 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002791
Josef Bacik5fd02042012-05-02 14:00:54 -04002792 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2793 ret = -EIO;
2794 goto out;
2795 }
2796
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002797 btrfs_free_io_failure_record(BTRFS_I(inode),
2798 ordered_extent->file_offset,
2799 ordered_extent->file_offset +
2800 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002801
Josef Bacik77cef2e2013-08-29 13:57:21 -04002802 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2803 truncated = true;
2804 logical_len = ordered_extent->truncated_len;
2805 /* Truncated the entire extent, don't bother adding */
2806 if (!logical_len)
2807 goto out;
2808 }
2809
Yan, Zhengc2167752009-11-12 09:34:21 +00002810 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002811 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002812
2813 /*
2814 * For mwrite(mmap + memset to write) case, we still reserve
2815 * space for NOCOW range.
2816 * As NOCOW won't cause a new delayed ref, just free the space
2817 */
2818 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2819 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002820 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2821 if (nolock)
2822 trans = btrfs_join_transaction_nolock(root);
2823 else
2824 trans = btrfs_join_transaction(root);
2825 if (IS_ERR(trans)) {
2826 ret = PTR_ERR(trans);
2827 trans = NULL;
2828 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002829 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002830 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002831 ret = btrfs_update_inode_fallback(trans, root, inode);
2832 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002833 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002834 goto out;
2835 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002836
Josef Bacik2ac55d42010-02-03 19:33:23 +00002837 lock_extent_bits(io_tree, ordered_extent->file_offset,
2838 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002839 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002840
Liu Bo38c227d2013-01-29 03:18:40 +00002841 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2842 ordered_extent->file_offset + ordered_extent->len - 1,
2843 EXTENT_DEFRAG, 1, cached_state);
2844 if (ret) {
2845 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002846 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002847 /* the inode is shared */
2848 new = record_old_file_extents(inode, ordered_extent);
2849
2850 clear_extent_bit(io_tree, ordered_extent->file_offset,
2851 ordered_extent->file_offset + ordered_extent->len - 1,
2852 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2853 }
2854
Josef Bacik0cb59c92010-07-02 12:14:14 -04002855 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002856 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002857 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002858 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002859 if (IS_ERR(trans)) {
2860 ret = PTR_ERR(trans);
2861 trans = NULL;
2862 goto out_unlock;
2863 }
Chris Masona79b7d42014-05-22 16:18:52 -07002864
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002865 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002866
Chris Masonc8b97812008-10-29 14:49:59 -04002867 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002868 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002869 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002870 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002871 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002872 ordered_extent->file_offset,
2873 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002874 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002875 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002876 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002877 ret = insert_reserved_file_extent(trans, inode,
2878 ordered_extent->file_offset,
2879 ordered_extent->start,
2880 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002881 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002882 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002883 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002884 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002885 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002886 ordered_extent->start,
2887 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002888 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002889 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2890 ordered_extent->file_offset, ordered_extent->len,
2891 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002892 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002893 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002894 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002895 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002896
David Sterbadf9f6282017-02-10 19:35:37 +01002897 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002898
Josef Bacik6c760c02012-11-09 10:53:21 -05002899 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2900 ret = btrfs_update_inode_fallback(trans, root, inode);
2901 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002902 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002903 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002904 }
2905 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002906out_unlock:
2907 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2908 ordered_extent->file_offset +
2909 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002910out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002911 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02002912 btrfs_delalloc_release_metadata(BTRFS_I(inode),
2913 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002914 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002915 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002916
Josef Bacik77cef2e2013-08-29 13:57:21 -04002917 if (ret || truncated) {
2918 u64 start, end;
2919
2920 if (truncated)
2921 start = ordered_extent->file_offset + logical_len;
2922 else
2923 start = ordered_extent->file_offset;
2924 end = ordered_extent->file_offset + ordered_extent->len - 1;
2925 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2926
2927 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02002928 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002929
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002930 /*
2931 * If the ordered extent had an IOERR or something else went
2932 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002933 * back to the allocator. We only free the extent in the
2934 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002935 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002936 if ((ret || !logical_len) &&
2937 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002938 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002939 btrfs_free_reserved_extent(fs_info,
2940 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002941 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002942 }
2943
2944
Josef Bacik5fd02042012-05-02 14:00:54 -04002945 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002946 * This needs to be done to make sure anybody waiting knows we are done
2947 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002948 */
2949 btrfs_remove_ordered_extent(inode, ordered_extent);
2950
Liu Bo38c227d2013-01-29 03:18:40 +00002951 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002952 if (new) {
2953 if (ret) {
2954 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002955 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08002956 } else {
2957 relink_file_extents(new);
2958 }
2959 }
Liu Bo38c227d2013-01-29 03:18:40 +00002960
Chris Masone6dcd2d2008-07-17 12:53:50 -04002961 /* once for us */
2962 btrfs_put_ordered_extent(ordered_extent);
2963 /* once for the tree */
2964 btrfs_put_ordered_extent(ordered_extent);
2965
Josef Bacik5fd02042012-05-02 14:00:54 -04002966 return ret;
2967}
2968
2969static void finish_ordered_fn(struct btrfs_work *work)
2970{
2971 struct btrfs_ordered_extent *ordered_extent;
2972 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2973 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002974}
2975
Christoph Hellwigb2950862008-12-02 09:54:17 -05002976static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002977 struct extent_state *state, int uptodate)
2978{
Josef Bacik5fd02042012-05-02 14:00:54 -04002979 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002980 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002981 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002982 struct btrfs_workqueue *wq;
2983 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002984
liubo1abe9b82011-03-24 11:18:59 +00002985 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2986
Chris Mason8b62b722009-09-02 16:53:46 -04002987 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002988 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2989 end - start + 1, uptodate))
2990 return 0;
2991
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002992 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002993 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08002994 func = btrfs_freespace_write_helper;
2995 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002996 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08002997 func = btrfs_endio_write_helper;
2998 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002999
Liu Bo9e0af232014-08-15 23:36:53 +08003000 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3001 NULL);
3002 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003003
3004 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003005}
3006
Miao Xiedc380ae2014-09-12 18:43:55 +08003007static int __readpage_endio_check(struct inode *inode,
3008 struct btrfs_io_bio *io_bio,
3009 int icsum, struct page *page,
3010 int pgoff, u64 start, size_t len)
3011{
3012 char *kaddr;
3013 u32 csum_expected;
3014 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003015
3016 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3017
3018 kaddr = kmap_atomic(page);
3019 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003020 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003021 if (csum != csum_expected)
3022 goto zeroit;
3023
3024 kunmap_atomic(kaddr);
3025 return 0;
3026zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003027 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003028 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003029 memset(kaddr + pgoff, 1, len);
3030 flush_dcache_page(page);
3031 kunmap_atomic(kaddr);
3032 if (csum_expected == 0)
3033 return 0;
3034 return -EIO;
3035}
3036
Chris Masond352ac62008-09-29 15:18:18 -04003037/*
Chris Masond352ac62008-09-29 15:18:18 -04003038 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003039 * if there's a match, we allow the bio to finish. If not, the code in
3040 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003041 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003042static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3043 u64 phy_offset, struct page *page,
3044 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003045{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003046 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003047 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003048 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003049 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003050
Chris Masond20f7042008-12-08 16:58:54 -05003051 if (PageChecked(page)) {
3052 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003053 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003054 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003055
3056 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003057 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003058
Yan Zheng17d217f2008-12-12 10:03:38 -05003059 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003060 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003061 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003062 return 0;
3063 }
3064
Miao Xiefacc8a222013-07-25 19:22:34 +08003065 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003066 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3067 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003068}
Chris Masonb888db22007-08-27 16:49:44 -04003069
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003070void btrfs_add_delayed_iput(struct inode *inode)
3071{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003072 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003073 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003074
3075 if (atomic_add_unless(&inode->i_count, -1, 1))
3076 return;
3077
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003078 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003079 if (binode->delayed_iput_count == 0) {
3080 ASSERT(list_empty(&binode->delayed_iput));
3081 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3082 } else {
3083 binode->delayed_iput_count++;
3084 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003085 spin_unlock(&fs_info->delayed_iput_lock);
3086}
3087
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003088void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003089{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003090
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003091 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003092 while (!list_empty(&fs_info->delayed_iputs)) {
3093 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003094
David Sterba8089fe62015-11-19 14:15:51 +01003095 inode = list_first_entry(&fs_info->delayed_iputs,
3096 struct btrfs_inode, delayed_iput);
3097 if (inode->delayed_iput_count) {
3098 inode->delayed_iput_count--;
3099 list_move_tail(&inode->delayed_iput,
3100 &fs_info->delayed_iputs);
3101 } else {
3102 list_del_init(&inode->delayed_iput);
3103 }
3104 spin_unlock(&fs_info->delayed_iput_lock);
3105 iput(&inode->vfs_inode);
3106 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003107 }
David Sterba8089fe62015-11-19 14:15:51 +01003108 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003109}
3110
Yan, Zhengd68fc572010-05-16 10:49:58 -04003111/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003112 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003113 * files in the subvolume, it removes orphan item and frees block_rsv
3114 * structure.
3115 */
3116void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3117 struct btrfs_root *root)
3118{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003119 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003120 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003121 int ret;
3122
Josef Bacik8a35d952012-05-23 14:26:42 -04003123 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003124 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3125 return;
3126
Josef Bacik90290e12011-12-02 15:44:12 -05003127 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003128 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003129 spin_unlock(&root->orphan_lock);
3130 return;
3131 }
3132
3133 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3134 spin_unlock(&root->orphan_lock);
3135 return;
3136 }
3137
3138 block_rsv = root->orphan_block_rsv;
3139 root->orphan_block_rsv = NULL;
3140 spin_unlock(&root->orphan_lock);
3141
Miao Xie27cdeb72014-04-02 19:51:05 +08003142 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003143 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003144 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003145 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003146 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003147 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003148 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003149 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3150 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003151 }
3152
Josef Bacik90290e12011-12-02 15:44:12 -05003153 if (block_rsv) {
3154 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003155 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003156 }
3157}
3158
3159/*
Josef Bacik7b128762008-07-24 12:17:14 -04003160 * This creates an orphan entry for the given inode in case something goes
3161 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003162 *
3163 * NOTE: caller of this function should reserve 5 units of metadata for
3164 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003165 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003166int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3167 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003168{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003169 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3170 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003171 struct btrfs_block_rsv *block_rsv = NULL;
3172 int reserve = 0;
3173 int insert = 0;
3174 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003175
Yan, Zhengd68fc572010-05-16 10:49:58 -04003176 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003177 block_rsv = btrfs_alloc_block_rsv(fs_info,
3178 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003179 if (!block_rsv)
3180 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003181 }
3182
Yan, Zhengd68fc572010-05-16 10:49:58 -04003183 spin_lock(&root->orphan_lock);
3184 if (!root->orphan_block_rsv) {
3185 root->orphan_block_rsv = block_rsv;
3186 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003187 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003188 block_rsv = NULL;
3189 }
Josef Bacik7b128762008-07-24 12:17:14 -04003190
Josef Bacik8a35d952012-05-23 14:26:42 -04003191 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003192 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003193#if 0
3194 /*
3195 * For proper ENOSPC handling, we should do orphan
3196 * cleanup when mounting. But this introduces backward
3197 * compatibility issue.
3198 */
3199 if (!xchg(&root->orphan_item_inserted, 1))
3200 insert = 2;
3201 else
3202 insert = 1;
3203#endif
3204 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003205 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003206 }
Josef Bacik7b128762008-07-24 12:17:14 -04003207
Josef Bacik72ac3c02012-05-23 14:13:11 -04003208 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003209 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003210 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003211 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003212
Yan, Zhengd68fc572010-05-16 10:49:58 -04003213 /* grab metadata reservation from transaction handle */
3214 if (reserve) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003215 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003216 ASSERT(!ret);
3217 if (ret) {
3218 atomic_dec(&root->orphan_inodes);
3219 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003220 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003221 if (insert)
3222 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003223 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003224 return ret;
3225 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003226 }
3227
3228 /* insert an orphan item to track this unlinked/truncated file */
3229 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003230 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003231 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003232 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003233 if (reserve) {
3234 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003235 &inode->runtime_flags);
3236 btrfs_orphan_release_metadata(inode);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003237 }
3238 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003239 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003240 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003241 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003242 return ret;
3243 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003244 }
3245 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003246 }
3247
3248 /* insert an orphan item to track subvolume contains orphan files */
3249 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003250 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003251 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003252 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003253 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003254 return ret;
3255 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003256 }
3257 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003258}
3259
3260/*
3261 * We have done the truncate/delete so we can go ahead and remove the orphan
3262 * item for this particular inode.
3263 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003264static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003265 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003266{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003267 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003268 int delete_item = 0;
3269 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003270 int ret = 0;
3271
Yan, Zhengd68fc572010-05-16 10:49:58 -04003272 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003273 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003274 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003276
Josef Bacik72ac3c02012-05-23 14:13:11 -04003277 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003278 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003279 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003280 spin_unlock(&root->orphan_lock);
3281
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003282 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003283 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003284 if (trans)
3285 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003286 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003287 }
Josef Bacik7b128762008-07-24 12:17:14 -04003288
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003289 if (release_rsv)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003290 btrfs_orphan_release_metadata(inode);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003291
Josef Bacik4ef31a42013-08-13 14:10:08 -04003292 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003293}
3294
3295/*
3296 * this cleans up any orphans that may be left on the list from the last use
3297 * of this root.
3298 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003299int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003300{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003301 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003302 struct btrfs_path *path;
3303 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003304 struct btrfs_key key, found_key;
3305 struct btrfs_trans_handle *trans;
3306 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003307 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003308 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3309
Yan, Zhengd68fc572010-05-16 10:49:58 -04003310 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003311 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003312
3313 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003314 if (!path) {
3315 ret = -ENOMEM;
3316 goto out;
3317 }
David Sterbae4058b52015-11-27 16:31:35 +01003318 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003319
3320 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003321 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003322 key.offset = (u64)-1;
3323
Josef Bacik7b128762008-07-24 12:17:14 -04003324 while (1) {
3325 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003326 if (ret < 0)
3327 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003328
3329 /*
3330 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003331 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003332 * find the key and see if we have stuff that matches
3333 */
3334 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003335 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003336 if (path->slots[0] == 0)
3337 break;
3338 path->slots[0]--;
3339 }
3340
3341 /* pull out the item */
3342 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003343 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3344
3345 /* make sure the item matches what we want */
3346 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3347 break;
David Sterba962a2982014-06-04 18:41:45 +02003348 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003349 break;
3350
3351 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003352 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003353
3354 /*
3355 * this is where we are basically btrfs_lookup, without the
3356 * crossing root thing. we store the inode number in the
3357 * offset of the orphan item.
3358 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003359
3360 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003361 btrfs_err(fs_info,
3362 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003363 ret = -EINVAL;
3364 goto out;
3365 }
3366
3367 last_objectid = found_key.offset;
3368
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003369 found_key.objectid = found_key.offset;
3370 found_key.type = BTRFS_INODE_ITEM_KEY;
3371 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003372 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303373 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003374 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003375 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003376
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003377 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003378 struct btrfs_root *dead_root;
3379 struct btrfs_fs_info *fs_info = root->fs_info;
3380 int is_dead_root = 0;
3381
3382 /*
3383 * this is an orphan in the tree root. Currently these
3384 * could come from 2 sources:
3385 * a) a snapshot deletion in progress
3386 * b) a free space cache inode
3387 * We need to distinguish those two, as the snapshot
3388 * orphan must not get deleted.
3389 * find_dead_roots already ran before us, so if this
3390 * is a snapshot deletion, we should find the root
3391 * in the dead_roots list
3392 */
3393 spin_lock(&fs_info->trans_lock);
3394 list_for_each_entry(dead_root, &fs_info->dead_roots,
3395 root_list) {
3396 if (dead_root->root_key.objectid ==
3397 found_key.objectid) {
3398 is_dead_root = 1;
3399 break;
3400 }
3401 }
3402 spin_unlock(&fs_info->trans_lock);
3403 if (is_dead_root) {
3404 /* prevent this orphan from being found again */
3405 key.offset = found_key.objectid - 1;
3406 continue;
3407 }
3408 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003409 /*
3410 * Inode is already gone but the orphan item is still there,
3411 * kill the orphan item.
3412 */
Filipe Manana67710892016-06-06 11:51:25 +01003413 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003414 trans = btrfs_start_transaction(root, 1);
3415 if (IS_ERR(trans)) {
3416 ret = PTR_ERR(trans);
3417 goto out;
3418 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003419 btrfs_debug(fs_info, "auto deleting %Lu",
3420 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003421 ret = btrfs_del_orphan_item(trans, root,
3422 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003423 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003424 if (ret)
3425 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003426 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003427 }
Josef Bacik7b128762008-07-24 12:17:14 -04003428
Josef Bacik7b128762008-07-24 12:17:14 -04003429 /*
3430 * add this inode to the orphan list so btrfs_orphan_del does
3431 * the proper thing when we hit it
3432 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003433 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3434 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003435 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003436
Josef Bacik7b128762008-07-24 12:17:14 -04003437 /* if we have links, this was a truncate, lets do that */
3438 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303439 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003440 iput(inode);
3441 continue;
3442 }
Josef Bacik7b128762008-07-24 12:17:14 -04003443 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003444
3445 /* 1 for the orphan item deletion. */
3446 trans = btrfs_start_transaction(root, 1);
3447 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003448 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003449 ret = PTR_ERR(trans);
3450 goto out;
3451 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003452 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003453 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003454 if (ret) {
3455 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003456 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003457 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003458
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003459 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003460 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003461 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003462 } else {
3463 nr_unlink++;
3464 }
3465
3466 /* this will do delete_inode and everything for us */
3467 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003468 if (ret)
3469 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003470 }
Miao Xie3254c872011-11-10 20:45:05 -05003471 /* release the path since we're done with it */
3472 btrfs_release_path(path);
3473
Yan, Zhengd68fc572010-05-16 10:49:58 -04003474 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3475
3476 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003477 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003478 (u64)-1);
3479
Miao Xie27cdeb72014-04-02 19:51:05 +08003480 if (root->orphan_block_rsv ||
3481 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003482 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003483 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003484 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003485 }
Josef Bacik7b128762008-07-24 12:17:14 -04003486
3487 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003488 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003489 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003490 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003491
3492out:
3493 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003494 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495 btrfs_free_path(path);
3496 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003497}
3498
Chris Masond352ac62008-09-29 15:18:18 -04003499/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003500 * very simple check to peek ahead in the leaf looking for xattrs. If we
3501 * don't find any xattrs, we know there can't be any acls.
3502 *
3503 * slot is the slot the inode is in, objectid is the objectid of the inode
3504 */
3505static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003506 int slot, u64 objectid,
3507 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003508{
3509 u32 nritems = btrfs_header_nritems(leaf);
3510 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003511 static u64 xattr_access = 0;
3512 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003513 int scanned = 0;
3514
Josef Bacikf23b5a52013-06-19 10:16:26 -04003515 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003516 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3517 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3518 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3519 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003520 }
3521
Chris Mason46a53cc2009-04-27 11:47:50 -04003522 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003523 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003524 while (slot < nritems) {
3525 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3526
3527 /* we found a different objectid, there must not be acls */
3528 if (found_key.objectid != objectid)
3529 return 0;
3530
3531 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003532 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003533 if (*first_xattr_slot == -1)
3534 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003535 if (found_key.offset == xattr_access ||
3536 found_key.offset == xattr_default)
3537 return 1;
3538 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003539
3540 /*
3541 * we found a key greater than an xattr key, there can't
3542 * be any acls later on
3543 */
3544 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3545 return 0;
3546
3547 slot++;
3548 scanned++;
3549
3550 /*
3551 * it goes inode, inode backrefs, xattrs, extents,
3552 * so if there are a ton of hard links to an inode there can
3553 * be a lot of backrefs. Don't waste time searching too hard,
3554 * this is just an optimization
3555 */
3556 if (scanned >= 8)
3557 break;
3558 }
3559 /* we hit the end of the leaf before we found an xattr or
3560 * something larger than an xattr. We have to assume the inode
3561 * has acls
3562 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003563 if (*first_xattr_slot == -1)
3564 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003565 return 1;
3566}
3567
3568/*
Chris Masond352ac62008-09-29 15:18:18 -04003569 * read an inode from the btree into the in-memory inode
3570 */
Filipe Manana67710892016-06-06 11:51:25 +01003571static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003572{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003573 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003574 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003575 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003576 struct btrfs_inode_item *inode_item;
3577 struct btrfs_root *root = BTRFS_I(inode)->root;
3578 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003579 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003580 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003581 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003582 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003583 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003584 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003585
3586 ret = btrfs_fill_inode(inode, &rdev);
3587 if (!ret)
3588 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003589
3590 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003591 if (!path) {
3592 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003593 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003594 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003595
Chris Mason39279cc2007-06-12 06:35:45 -04003596 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003597
Chris Mason39279cc2007-06-12 06:35:45 -04003598 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003599 if (ret) {
3600 if (ret > 0)
3601 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003602 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003603 }
Chris Mason39279cc2007-06-12 06:35:45 -04003604
Chris Mason5f39d392007-10-15 16:14:19 -04003605 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003606
3607 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003608 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003609
Chris Mason5f39d392007-10-15 16:14:19 -04003610 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3611 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003612 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003613 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003614 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3615 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003616 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003617
David Sterbaa937b972014-12-12 17:39:12 +01003618 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3619 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003620
David Sterbaa937b972014-12-12 17:39:12 +01003621 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3622 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003623
David Sterbaa937b972014-12-12 17:39:12 +01003624 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3625 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003626
chandan r9cc97d62012-07-04 12:48:07 +05303627 BTRFS_I(inode)->i_otime.tv_sec =
3628 btrfs_timespec_sec(leaf, &inode_item->otime);
3629 BTRFS_I(inode)->i_otime.tv_nsec =
3630 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003631
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003632 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003633 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003634 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3635
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003636 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003637 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003638 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003639 rdev = btrfs_inode_rdev(leaf, inode_item);
3640
Josef Bacikaec74772008-07-24 12:12:38 -04003641 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003642 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003643
3644cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003645 /*
3646 * If we were modified in the current generation and evicted from memory
3647 * and then re-read we need to do a full sync since we don't have any
3648 * idea about which extents were modified before we were evicted from
3649 * cache.
3650 *
3651 * This is required for both inode re-read from disk and delayed inode
3652 * in delayed_nodes_tree.
3653 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003654 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003655 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3656 &BTRFS_I(inode)->runtime_flags);
3657
Filipe Mananabde6c242015-07-24 00:00:19 +01003658 /*
3659 * We don't persist the id of the transaction where an unlink operation
3660 * against the inode was last made. So here we assume the inode might
3661 * have been evicted, and therefore the exact value of last_unlink_trans
3662 * lost, and set it to last_trans to avoid metadata inconsistencies
3663 * between the inode and its parent if the inode is fsync'ed and the log
3664 * replayed. For example, in the scenario:
3665 *
3666 * touch mydir/foo
3667 * ln mydir/foo mydir/bar
3668 * sync
3669 * unlink mydir/bar
3670 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3671 * xfs_io -c fsync mydir/foo
3672 * <power failure>
3673 * mount fs, triggers fsync log replay
3674 *
3675 * We must make sure that when we fsync our inode foo we also log its
3676 * parent inode, otherwise after log replay the parent still has the
3677 * dentry with the "bar" name but our inode foo has a link count of 1
3678 * and doesn't have an inode ref with the name "bar" anymore.
3679 *
3680 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003681 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003682 * transaction commits on fsync if our inode is a directory, or if our
3683 * inode is not a directory, logging its parent unnecessarily.
3684 */
3685 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3686
Miao Xie67de1172013-12-26 13:07:06 +08003687 path->slots[0]++;
3688 if (inode->i_nlink != 1 ||
3689 path->slots[0] >= btrfs_header_nritems(leaf))
3690 goto cache_acl;
3691
3692 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003693 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003694 goto cache_acl;
3695
3696 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3697 if (location.type == BTRFS_INODE_REF_KEY) {
3698 struct btrfs_inode_ref *ref;
3699
3700 ref = (struct btrfs_inode_ref *)ptr;
3701 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3702 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3703 struct btrfs_inode_extref *extref;
3704
3705 extref = (struct btrfs_inode_extref *)ptr;
3706 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3707 extref);
3708 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003709cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003710 /*
3711 * try to precache a NULL acl entry for files that don't have
3712 * any xattrs or acls
3713 */
Li Zefan33345d012011-04-20 10:31:50 +08003714 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003715 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003716 if (first_xattr_slot != -1) {
3717 path->slots[0] = first_xattr_slot;
3718 ret = btrfs_load_inode_props(inode, path);
3719 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003720 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003721 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003722 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003723 root->root_key.objectid, ret);
3724 }
3725 btrfs_free_path(path);
3726
Al Viro72c04902009-06-24 16:58:48 -04003727 if (!maybe_acls)
3728 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003729
Chris Mason39279cc2007-06-12 06:35:45 -04003730 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003731 case S_IFREG:
3732 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003733 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003734 inode->i_fop = &btrfs_file_operations;
3735 inode->i_op = &btrfs_file_inode_operations;
3736 break;
3737 case S_IFDIR:
3738 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003739 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003740 break;
3741 case S_IFLNK:
3742 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003743 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003744 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3745 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003746 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003747 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003748 init_special_inode(inode, inode->i_mode, rdev);
3749 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003750 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003751
3752 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003753 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003754
3755make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003756 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003757 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003758 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003759}
3760
Chris Masond352ac62008-09-29 15:18:18 -04003761/*
3762 * given a leaf and an inode, copy the inode fields into the leaf
3763 */
Chris Masone02119d2008-09-05 16:13:11 -04003764static void fill_inode_item(struct btrfs_trans_handle *trans,
3765 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003766 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003767 struct inode *inode)
3768{
Liu Bo51fab692012-12-27 09:01:21 +00003769 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003770
Liu Bo51fab692012-12-27 09:01:21 +00003771 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003772
Liu Bo51fab692012-12-27 09:01:21 +00003773 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3774 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3775 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3776 &token);
3777 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3778 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003779
David Sterbaa937b972014-12-12 17:39:12 +01003780 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003781 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003782 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003783 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003784
David Sterbaa937b972014-12-12 17:39:12 +01003785 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003786 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003787 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003788 inode->i_mtime.tv_nsec, &token);
3789
David Sterbaa937b972014-12-12 17:39:12 +01003790 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003791 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003792 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003793 inode->i_ctime.tv_nsec, &token);
3794
chandan r9cc97d62012-07-04 12:48:07 +05303795 btrfs_set_token_timespec_sec(leaf, &item->otime,
3796 BTRFS_I(inode)->i_otime.tv_sec, &token);
3797 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3798 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3799
Liu Bo51fab692012-12-27 09:01:21 +00003800 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3801 &token);
3802 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3803 &token);
3804 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3805 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3806 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3807 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3808 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003809}
3810
Chris Masond352ac62008-09-29 15:18:18 -04003811/*
3812 * copy everything in the in-memory inode into the btree.
3813 */
Chris Mason21151332011-11-10 20:39:08 -05003814static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003815 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003816{
3817 struct btrfs_inode_item *inode_item;
3818 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003819 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003820 int ret;
3821
3822 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003823 if (!path)
3824 return -ENOMEM;
3825
Chris Masonb9473432009-03-13 11:00:37 -04003826 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003827 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3828 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003829 if (ret) {
3830 if (ret > 0)
3831 ret = -ENOENT;
3832 goto failed;
3833 }
3834
Chris Mason5f39d392007-10-15 16:14:19 -04003835 leaf = path->nodes[0];
3836 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003837 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003838
Chris Masone02119d2008-09-05 16:13:11 -04003839 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003840 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003841 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003842 ret = 0;
3843failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003844 btrfs_free_path(path);
3845 return ret;
3846}
3847
Chris Masond352ac62008-09-29 15:18:18 -04003848/*
Chris Mason21151332011-11-10 20:39:08 -05003849 * copy everything in the in-memory inode into the btree.
3850 */
3851noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3852 struct btrfs_root *root, struct inode *inode)
3853{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003854 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003855 int ret;
3856
3857 /*
3858 * If the inode is a free space inode, we can deadlock during commit
3859 * if we put it into the delayed code.
3860 *
3861 * The data relocation inode should also be directly updated
3862 * without delay
3863 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003864 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003865 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003866 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003867 btrfs_update_root_times(trans, root);
3868
Chris Mason21151332011-11-10 20:39:08 -05003869 ret = btrfs_delayed_update_inode(trans, root, inode);
3870 if (!ret)
3871 btrfs_set_inode_last_trans(trans, inode);
3872 return ret;
3873 }
3874
3875 return btrfs_update_inode_item(trans, root, inode);
3876}
3877
Josef Bacikbe6aef62012-10-22 15:43:12 -04003878noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3879 struct btrfs_root *root,
3880 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003881{
3882 int ret;
3883
3884 ret = btrfs_update_inode(trans, root, inode);
3885 if (ret == -ENOSPC)
3886 return btrfs_update_inode_item(trans, root, inode);
3887 return ret;
3888}
3889
3890/*
Chris Masond352ac62008-09-29 15:18:18 -04003891 * unlink helper that gets used here in inode.c and in the tree logging
3892 * recovery code. It remove a link in a directory with a given name, and
3893 * also drops the back refs in the inode to the directory
3894 */
Al Viro92986792011-03-04 17:14:37 +00003895static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3896 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003897 struct btrfs_inode *dir,
3898 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003899 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003900{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003901 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003902 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003903 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003904 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003905 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003906 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003907 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003908 u64 ino = btrfs_ino(inode);
3909 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003910
3911 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003912 if (!path) {
3913 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003914 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003915 }
3916
Chris Masonb9473432009-03-13 11:00:37 -04003917 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003918 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003919 name, name_len, -1);
3920 if (IS_ERR(di)) {
3921 ret = PTR_ERR(di);
3922 goto err;
3923 }
3924 if (!di) {
3925 ret = -ENOENT;
3926 goto err;
3927 }
Chris Mason5f39d392007-10-15 16:14:19 -04003928 leaf = path->nodes[0];
3929 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003930 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003931 if (ret)
3932 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003933 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003934
Miao Xie67de1172013-12-26 13:07:06 +08003935 /*
3936 * If we don't have dir index, we have to get it by looking up
3937 * the inode ref, since we get the inode ref, remove it directly,
3938 * it is unnecessary to do delayed deletion.
3939 *
3940 * But if we have dir index, needn't search inode ref to get it.
3941 * Since the inode ref is close to the inode item, it is better
3942 * that we delay to delete it, and just do this deletion when
3943 * we update the inode item.
3944 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003945 if (inode->dir_index) {
3946 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08003947 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003948 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003949 goto skip_backref;
3950 }
3951 }
3952
Li Zefan33345d012011-04-20 10:31:50 +08003953 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3954 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003955 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003956 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003957 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003958 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003959 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003960 goto err;
3961 }
Miao Xie67de1172013-12-26 13:07:06 +08003962skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003963 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003964 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003965 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003966 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003967 }
Chris Mason39279cc2007-06-12 06:35:45 -04003968
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003969 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3970 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003971 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003972 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003973 goto err;
3974 }
Chris Masone02119d2008-09-05 16:13:11 -04003975
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003976 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3977 index);
Chris Mason6418c962010-10-30 07:34:24 -04003978 if (ret == -ENOENT)
3979 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003980 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003981 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003982err:
3983 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003984 if (ret)
3985 goto out;
3986
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003987 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003988 inode_inc_iversion(&inode->vfs_inode);
3989 inode_inc_iversion(&dir->vfs_inode);
3990 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3991 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3992 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003993out:
Chris Mason39279cc2007-06-12 06:35:45 -04003994 return ret;
3995}
3996
Al Viro92986792011-03-04 17:14:37 +00003997int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3998 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003999 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004000 const char *name, int name_len)
4001{
4002 int ret;
4003 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4004 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004005 drop_nlink(&inode->vfs_inode);
4006 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004007 }
4008 return ret;
4009}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004010
4011/*
4012 * helper to start transaction for unlink and rmdir.
4013 *
Josef Bacikd52be812013-05-29 14:54:47 -04004014 * unlink and rmdir are special in btrfs, they do not always free space, so
4015 * if we cannot make our reservations the normal way try and see if there is
4016 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4017 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004018 */
Josef Bacikd52be812013-05-29 14:54:47 -04004019static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004021 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004022
Josef Bacike70bea52011-10-11 14:18:24 -04004023 /*
4024 * 1 for the possible orphan item
4025 * 1 for the dir item
4026 * 1 for the dir index
4027 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004028 * 1 for the inode
4029 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004030 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004031}
4032
Chris Mason39279cc2007-06-12 06:35:45 -04004033static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4034{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004035 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004036 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004037 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004038 int ret;
4039
Josef Bacikd52be812013-05-29 14:54:47 -04004040 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004041 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004042 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004043
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004044 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4045 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004046
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004047 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4048 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4049 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004050 if (ret)
4051 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004052
Yan, Zhenga22285a2010-05-16 10:48:46 -04004053 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004054 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004055 if (ret)
4056 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004057 }
Josef Bacik7b128762008-07-24 12:17:14 -04004058
Tsutomu Itohb5324022011-07-19 07:27:20 +00004059out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004060 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004061 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004062 return ret;
4063}
4064
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004065int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4066 struct btrfs_root *root,
4067 struct inode *dir, u64 objectid,
4068 const char *name, int name_len)
4069{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004070 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004071 struct btrfs_path *path;
4072 struct extent_buffer *leaf;
4073 struct btrfs_dir_item *di;
4074 struct btrfs_key key;
4075 u64 index;
4076 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004077 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004078
4079 path = btrfs_alloc_path();
4080 if (!path)
4081 return -ENOMEM;
4082
Li Zefan33345d012011-04-20 10:31:50 +08004083 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004084 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004085 if (IS_ERR_OR_NULL(di)) {
4086 if (!di)
4087 ret = -ENOENT;
4088 else
4089 ret = PTR_ERR(di);
4090 goto out;
4091 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004092
4093 leaf = path->nodes[0];
4094 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4095 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4096 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004097 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004098 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004099 goto out;
4100 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004101 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004102
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004103 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4104 root->root_key.objectid, dir_ino,
4105 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004106 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004107 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004108 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004109 goto out;
4110 }
Li Zefan33345d012011-04-20 10:31:50 +08004111 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004113 if (IS_ERR_OR_NULL(di)) {
4114 if (!di)
4115 ret = -ENOENT;
4116 else
4117 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004118 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004119 goto out;
4120 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004121
4122 leaf = path->nodes[0];
4123 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004124 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004125 index = key.offset;
4126 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004127 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004128
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004129 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004130 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004131 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004132 goto out;
4133 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004134
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004135 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004136 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004137 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004138 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004139 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004140 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004141out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004142 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004143 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004144}
4145
Chris Mason39279cc2007-06-12 06:35:45 -04004146static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4147{
David Howells2b0143b2015-03-17 22:25:59 +00004148 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004149 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004150 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004151 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004152 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004153
David Sterbab3ae2442012-09-13 16:04:34 -06004154 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004155 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004156 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004157 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004158
Josef Bacikd52be812013-05-29 14:54:47 -04004159 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004160 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004161 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004162
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004163 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004164 err = btrfs_unlink_subvol(trans, root, dir,
4165 BTRFS_I(inode)->location.objectid,
4166 dentry->d_name.name,
4167 dentry->d_name.len);
4168 goto out;
4169 }
4170
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004171 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004172 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004173 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004174
Filipe Manana44f714d2016-06-06 16:11:13 +01004175 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4176
Chris Mason39279cc2007-06-12 06:35:45 -04004177 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004178 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4179 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4180 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004181 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004182 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004183 /*
4184 * Propagate the last_unlink_trans value of the deleted dir to
4185 * its parent directory. This is to prevent an unrecoverable
4186 * log tree in the case we do something like this:
4187 * 1) create dir foo
4188 * 2) create snapshot under dir foo
4189 * 3) delete the snapshot
4190 * 4) rmdir foo
4191 * 5) mkdir foo
4192 * 6) fsync foo or some file inside foo
4193 */
4194 if (last_unlink_trans >= trans->transid)
4195 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4196 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004197out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004198 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004199 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004200
Chris Mason39279cc2007-06-12 06:35:45 -04004201 return err;
4202}
4203
Chris Mason28f75a02015-02-04 06:59:29 -08004204static int truncate_space_check(struct btrfs_trans_handle *trans,
4205 struct btrfs_root *root,
4206 u64 bytes_deleted)
4207{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004208 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004209 int ret;
4210
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004211 /*
4212 * This is only used to apply pressure to the enospc system, we don't
4213 * intend to use this reservation at all.
4214 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004215 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004216 bytes_deleted *= fs_info->nodesize;
4217 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004218 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004219 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004220 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004221 trans->transid,
4222 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004223 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004224 }
Chris Mason28f75a02015-02-04 06:59:29 -08004225 return ret;
4226
4227}
4228
Filipe Manana0305cd52015-10-16 12:34:25 +01004229static int truncate_inline_extent(struct inode *inode,
4230 struct btrfs_path *path,
4231 struct btrfs_key *found_key,
4232 const u64 item_end,
4233 const u64 new_size)
4234{
4235 struct extent_buffer *leaf = path->nodes[0];
4236 int slot = path->slots[0];
4237 struct btrfs_file_extent_item *fi;
4238 u32 size = (u32)(new_size - found_key->offset);
4239 struct btrfs_root *root = BTRFS_I(inode)->root;
4240
4241 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4242
4243 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4244 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004245 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004246
4247 /*
4248 * Zero out the remaining of the last page of our inline extent,
4249 * instead of directly truncating our inline extent here - that
4250 * would be much more complex (decompressing all the data, then
4251 * compressing the truncated data, which might be bigger than
4252 * the size of the inline extent, resize the extent, etc).
4253 * We release the path because to get the page we might need to
4254 * read the extent item from disk (data not in the page cache).
4255 */
4256 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304257 return btrfs_truncate_block(inode, offset, page_end - offset,
4258 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004259 }
4260
4261 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4262 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004263 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004264
4265 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4266 inode_sub_bytes(inode, item_end + 1 - new_size);
4267
4268 return 0;
4269}
4270
Chris Mason323ac952008-10-01 19:05:46 -04004271/*
Chris Mason39279cc2007-06-12 06:35:45 -04004272 * this can truncate away extent items, csum items and directory items.
4273 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004274 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004275 *
4276 * csum items that cross the new i_size are truncated to the new size
4277 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004278 *
4279 * min_type is the minimum key type to truncate down to. If set to 0, this
4280 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004281 */
Yan, Zheng80825102009-11-12 09:35:36 +00004282int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4283 struct btrfs_root *root,
4284 struct inode *inode,
4285 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004286{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004287 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004288 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004289 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004290 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004291 struct btrfs_key key;
4292 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004293 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004294 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004295 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004296 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004297 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004298 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004299 int found_extent;
4300 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004301 int pending_del_nr = 0;
4302 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004303 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004304 int ret;
4305 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004306 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004307 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004308 bool be_nice = 0;
4309 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004310 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004311
4312 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004313
Chris Mason28ed1342014-12-17 09:41:04 -08004314 /*
4315 * for non-free space inodes and ref cows, we want to back off from
4316 * time to time
4317 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004318 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004319 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4320 be_nice = 1;
4321
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004322 path = btrfs_alloc_path();
4323 if (!path)
4324 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004325 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004326
Josef Bacik5dc562c2012-08-17 13:14:17 -04004327 /*
4328 * We want to drop from the next block forward in case this new size is
4329 * not block aligned since we will be keeping the last block of the
4330 * extent just the way it is.
4331 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004332 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004333 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004334 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004335 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004336 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004337
Miao Xie16cdcec2011-04-22 18:12:22 +08004338 /*
4339 * This function is also used to drop the items in the log tree before
4340 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4341 * it is used to drop the loged items. So we shouldn't kill the delayed
4342 * items.
4343 */
4344 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004345 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004346
Li Zefan33345d012011-04-20 10:31:50 +08004347 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004348 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004349 key.type = (u8)-1;
4350
Chris Mason85e21ba2008-01-29 15:11:36 -05004351search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004352 /*
4353 * with a 16K leaf size and 128MB extents, you can actually queue
4354 * up a huge file in a single leaf. Most of the time that
4355 * bytes_deleted is > 0, it will be huge by the time we get here
4356 */
Byongho Leeee221842015-12-15 01:42:10 +09004357 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004358 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004359 err = -EAGAIN;
4360 goto error;
4361 }
4362 }
4363
4364
Chris Masonb9473432009-03-13 11:00:37 -04004365 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004366 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004367 if (ret < 0) {
4368 err = ret;
4369 goto out;
4370 }
Chris Masond3977122009-01-05 21:25:51 -05004371
Chris Mason85e21ba2008-01-29 15:11:36 -05004372 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004373 /* there are no items in the tree for us to truncate, we're
4374 * done
4375 */
Yan, Zheng80825102009-11-12 09:35:36 +00004376 if (path->slots[0] == 0)
4377 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004378 path->slots[0]--;
4379 }
4380
Chris Masond3977122009-01-05 21:25:51 -05004381 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004382 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004383 leaf = path->nodes[0];
4384 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004385 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004386
Li Zefan33345d012011-04-20 10:31:50 +08004387 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004388 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004389
Chris Mason85e21ba2008-01-29 15:11:36 -05004390 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004391 break;
4392
Chris Mason5f39d392007-10-15 16:14:19 -04004393 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004394 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004395 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004396 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004397 extent_type = btrfs_file_extent_type(leaf, fi);
4398 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004399 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004400 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004401 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004402 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004403 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004404 }
Yan008630c2007-11-07 13:31:09 -05004405 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004406 }
Yan, Zheng80825102009-11-12 09:35:36 +00004407 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004408 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004409 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004410 if (item_end < new_size) {
4411 /*
4412 * With NO_HOLES mode, for the following mapping
4413 *
4414 * [0-4k][hole][8k-12k]
4415 *
4416 * if truncating isize down to 6k, it ends up
4417 * isize being 8k.
4418 */
4419 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4420 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004421 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004422 }
Yan, Zheng80825102009-11-12 09:35:36 +00004423 if (found_key.offset >= new_size)
4424 del_item = 1;
4425 else
4426 del_item = 0;
4427 }
Chris Mason39279cc2007-06-12 06:35:45 -04004428 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004430 if (found_type != BTRFS_EXTENT_DATA_KEY)
4431 goto delete;
4432
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004433 if (del_item)
4434 last_size = found_key.offset;
4435 else
4436 last_size = new_size;
4437
Chris Mason179e29e2007-11-01 11:28:41 -04004438 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004439 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004440 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004441 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004442 u64 orig_num_bytes =
4443 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004444 extent_num_bytes = ALIGN(new_size -
4445 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004446 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004447 btrfs_set_file_extent_num_bytes(leaf, fi,
4448 extent_num_bytes);
4449 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004450 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004451 if (test_bit(BTRFS_ROOT_REF_COWS,
4452 &root->state) &&
4453 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004454 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004455 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004456 } else {
Chris Masondb945352007-10-15 16:15:53 -04004457 extent_num_bytes =
4458 btrfs_file_extent_disk_num_bytes(leaf,
4459 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004460 extent_offset = found_key.offset -
4461 btrfs_file_extent_offset(leaf, fi);
4462
Chris Mason39279cc2007-06-12 06:35:45 -04004463 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004464 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004465 if (extent_start != 0) {
4466 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004467 if (test_bit(BTRFS_ROOT_REF_COWS,
4468 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004469 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004470 }
4471 }
Chris Mason90692182008-02-08 13:49:28 -05004472 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004473 /*
4474 * we can't truncate inline items that have had
4475 * special encodings
4476 */
4477 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004478 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4479 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004480
4481 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004482 * Need to release path in order to truncate a
4483 * compressed extent. So delete any accumulated
4484 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004485 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004486 if (btrfs_file_extent_compression(leaf, fi) !=
4487 BTRFS_COMPRESS_NONE && pending_del_nr) {
4488 err = btrfs_del_items(trans, root, path,
4489 pending_del_slot,
4490 pending_del_nr);
4491 if (err) {
4492 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004493 err);
4494 goto error;
4495 }
4496 pending_del_nr = 0;
4497 }
4498
4499 err = truncate_inline_extent(inode, path,
4500 &found_key,
4501 item_end,
4502 new_size);
4503 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004504 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004505 goto error;
4506 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004507 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4508 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004509 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004510 }
Chris Mason39279cc2007-06-12 06:35:45 -04004511 }
Chris Mason179e29e2007-11-01 11:28:41 -04004512delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004513 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004514 if (!pending_del_nr) {
4515 /* no pending yet, add ourselves */
4516 pending_del_slot = path->slots[0];
4517 pending_del_nr = 1;
4518 } else if (pending_del_nr &&
4519 path->slots[0] + 1 == pending_del_slot) {
4520 /* hop on the pending chunk */
4521 pending_del_nr++;
4522 pending_del_slot = path->slots[0];
4523 } else {
Chris Masond3977122009-01-05 21:25:51 -05004524 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004525 }
Chris Mason39279cc2007-06-12 06:35:45 -04004526 } else {
4527 break;
4528 }
Chris Mason28f75a02015-02-04 06:59:29 -08004529 should_throttle = 0;
4530
Miao Xie27cdeb72014-04-02 19:51:05 +08004531 if (found_extent &&
4532 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004533 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004534 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004535 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004536 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004537 extent_num_bytes, 0,
4538 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004539 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004540 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004541 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4542 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004543 trans->delayed_ref_updates * 2,
4544 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004545 if (be_nice) {
4546 if (truncate_space_check(trans, root,
4547 extent_num_bytes)) {
4548 should_end = 1;
4549 }
4550 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004551 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004552 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004553 }
Chris Mason39279cc2007-06-12 06:35:45 -04004554 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004555
Yan, Zheng80825102009-11-12 09:35:36 +00004556 if (found_type == BTRFS_INODE_ITEM_KEY)
4557 break;
4558
4559 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004560 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004561 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004562 if (pending_del_nr) {
4563 ret = btrfs_del_items(trans, root, path,
4564 pending_del_slot,
4565 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004566 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004567 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004568 goto error;
4569 }
Yan, Zheng80825102009-11-12 09:35:36 +00004570 pending_del_nr = 0;
4571 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004572 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004573 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004574 unsigned long updates = trans->delayed_ref_updates;
4575 if (updates) {
4576 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004577 ret = btrfs_run_delayed_refs(trans,
4578 fs_info,
4579 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004580 if (ret && !err)
4581 err = ret;
4582 }
4583 }
Chris Mason28f75a02015-02-04 06:59:29 -08004584 /*
4585 * if we failed to refill our space rsv, bail out
4586 * and let the transaction restart
4587 */
4588 if (should_end) {
4589 err = -EAGAIN;
4590 goto error;
4591 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004592 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004593 } else {
4594 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004595 }
Chris Mason39279cc2007-06-12 06:35:45 -04004596 }
Yan, Zheng80825102009-11-12 09:35:36 +00004597out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004598 if (pending_del_nr) {
4599 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4600 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004601 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004602 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004603 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004604error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004605 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004606 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004607
Chris Mason39279cc2007-06-12 06:35:45 -04004608 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004609
Liu Bo19fd2df2016-12-01 13:01:02 -08004610 if (err == 0) {
4611 /* only inline file may have last_size != new_size */
4612 if (new_size >= fs_info->sectorsize ||
4613 new_size > fs_info->max_inline)
4614 ASSERT(last_size == new_size);
4615 }
4616
Byongho Leeee221842015-12-15 01:42:10 +09004617 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004618 unsigned long updates = trans->delayed_ref_updates;
4619 if (updates) {
4620 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004621 ret = btrfs_run_delayed_refs(trans, fs_info,
4622 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004623 if (ret && !err)
4624 err = ret;
4625 }
4626 }
Yan, Zheng80825102009-11-12 09:35:36 +00004627 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004628}
4629
Chris Masona52d9a82007-08-27 16:49:44 -04004630/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304631 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004632 * @inode - inode that we're zeroing
4633 * @from - the offset to start zeroing
4634 * @len - the length to zero, 0 to zero the entire range respective to the
4635 * offset
4636 * @front - zero up to the offset instead of from the offset on
4637 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304638 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004639 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004640 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304641int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004642 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004643{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004644 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004645 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004646 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4647 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004648 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004649 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004650 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004651 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304652 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004653 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004654 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004655 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304656 u64 block_start;
4657 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004658
Josef Bacik2aaa6652012-08-29 14:27:18 -04004659 if ((offset & (blocksize - 1)) == 0 &&
4660 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004661 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304662
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004663 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304664 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004665 if (ret)
4666 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004667
Chris Mason211c17f2008-05-15 09:13:45 -04004668again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004669 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004670 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004671 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304672 round_down(from, blocksize),
4673 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004674 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004675 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004676 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004677
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304678 block_start = round_down(from, blocksize);
4679 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004680
Chris Masona52d9a82007-08-27 16:49:44 -04004681 if (!PageUptodate(page)) {
4682 ret = btrfs_readpage(NULL, page);
4683 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004684 if (page->mapping != mapping) {
4685 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004686 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004687 goto again;
4688 }
Chris Masona52d9a82007-08-27 16:49:44 -04004689 if (!PageUptodate(page)) {
4690 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004691 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004692 }
4693 }
Chris Mason211c17f2008-05-15 09:13:45 -04004694 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004695
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304696 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004697 set_page_extent_mapped(page);
4698
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304699 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004700 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304701 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004702 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004703 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004704 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004705 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004706 btrfs_put_ordered_extent(ordered);
4707 goto again;
4708 }
4709
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304710 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004711 EXTENT_DIRTY | EXTENT_DELALLOC |
4712 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004713 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004714
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304715 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004716 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004717 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304718 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004719 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004720 goto out_unlock;
4721 }
4722
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304723 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004724 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304725 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004726 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004727 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304728 memset(kaddr + (block_start - page_offset(page)),
4729 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004730 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304731 memset(kaddr + (block_start - page_offset(page)) + offset,
4732 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004733 flush_dcache_page(page);
4734 kunmap(page);
4735 }
Chris Mason247e7432008-07-17 12:53:51 -04004736 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004737 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304738 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004739 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004740
Chris Mason89642222008-07-24 09:41:53 -04004741out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004742 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304743 btrfs_delalloc_release_space(inode, block_start,
4744 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004745 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004746 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004747out:
4748 return ret;
4749}
4750
Josef Bacik16e75492013-10-22 12:18:51 -04004751static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4752 u64 offset, u64 len)
4753{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004754 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004755 struct btrfs_trans_handle *trans;
4756 int ret;
4757
4758 /*
4759 * Still need to make sure the inode looks like it's been updated so
4760 * that any holes get logged if we fsync.
4761 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004762 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4763 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004764 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4765 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4766 return 0;
4767 }
4768
4769 /*
4770 * 1 - for the one we're dropping
4771 * 1 - for the one we're adding
4772 * 1 - for updating the inode.
4773 */
4774 trans = btrfs_start_transaction(root, 3);
4775 if (IS_ERR(trans))
4776 return PTR_ERR(trans);
4777
4778 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4779 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004780 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004781 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004782 return ret;
4783 }
4784
David Sterbaf85b7372017-01-20 14:54:07 +01004785 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4786 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004787 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004788 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004789 else
4790 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004791 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004792 return ret;
4793}
4794
Josef Bacik695a0d02011-03-04 15:46:53 -05004795/*
4796 * This function puts in dummy file extents for the area we're creating a hole
4797 * for. So if we are truncating this file to a larger size we need to insert
4798 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4799 * the range between oldsize and size
4800 */
Josef Bacika41ad392011-01-31 15:30:16 -05004801int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004802{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004803 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004804 struct btrfs_root *root = BTRFS_I(inode)->root;
4805 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004806 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004807 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004808 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004809 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4810 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004811 u64 last_byte;
4812 u64 cur_offset;
4813 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004814 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004815
Josef Bacika71754f2013-06-17 17:14:39 -04004816 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304817 * If our size started in the middle of a block we need to zero out the
4818 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004819 * expose stale data.
4820 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304821 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004822 if (err)
4823 return err;
4824
Yan Zheng9036c102008-10-30 14:19:41 -04004825 if (size <= hole_start)
4826 return 0;
4827
Yan Zheng9036c102008-10-30 14:19:41 -04004828 while (1) {
4829 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004830
David Sterbaff13db42015-12-03 14:30:40 +01004831 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004832 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004833 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004834 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004835 if (!ordered)
4836 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004837 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4838 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004839 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004840 btrfs_put_ordered_extent(ordered);
4841 }
4842
Yan Zheng9036c102008-10-30 14:19:41 -04004843 cur_offset = hole_start;
4844 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02004845 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004846 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004847 if (IS_ERR(em)) {
4848 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004849 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004850 break;
4851 }
Yan Zheng9036c102008-10-30 14:19:41 -04004852 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004853 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004854 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004855 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004856 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004857
Josef Bacik16e75492013-10-22 12:18:51 -04004858 err = maybe_insert_hole(root, inode, cur_offset,
4859 hole_size);
4860 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004861 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004862 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004863 cur_offset + hole_size - 1, 0);
4864 hole_em = alloc_extent_map();
4865 if (!hole_em) {
4866 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4867 &BTRFS_I(inode)->runtime_flags);
4868 goto next;
4869 }
4870 hole_em->start = cur_offset;
4871 hole_em->len = hole_size;
4872 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004873
Josef Bacik5dc562c2012-08-17 13:14:17 -04004874 hole_em->block_start = EXTENT_MAP_HOLE;
4875 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004876 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004877 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004878 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004879 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004880 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004881
4882 while (1) {
4883 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004884 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004885 write_unlock(&em_tree->lock);
4886 if (err != -EEXIST)
4887 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004888 btrfs_drop_extent_cache(BTRFS_I(inode),
4889 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004890 cur_offset +
4891 hole_size - 1, 0);
4892 }
4893 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004894 }
Josef Bacik16e75492013-10-22 12:18:51 -04004895next:
Yan Zheng9036c102008-10-30 14:19:41 -04004896 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004897 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004898 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004899 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004900 break;
4901 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004902 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004903 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4904 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004905 return err;
4906}
4907
Eric Sandeen3972f262013-01-12 02:57:22 +00004908static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004909{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004910 struct btrfs_root *root = BTRFS_I(inode)->root;
4911 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004912 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004913 loff_t newsize = attr->ia_size;
4914 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004915 int ret;
4916
Eric Sandeen3972f262013-01-12 02:57:22 +00004917 /*
4918 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4919 * special case where we need to update the times despite not having
4920 * these flags set. For all other operations the VFS set these flags
4921 * explicitly if it wants a timestamp update.
4922 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004923 if (newsize != oldsize) {
4924 inode_inc_iversion(inode);
4925 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4926 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004927 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004928 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004929
Josef Bacika41ad392011-01-31 15:30:16 -05004930 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004931 /*
4932 * Don't do an expanding truncate while snapshoting is ongoing.
4933 * This is to ensure the snapshot captures a fully consistent
4934 * state of this file - if the snapshot captures this expanding
4935 * truncation, it must capture all writes that happened before
4936 * this truncation.
4937 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004938 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004939 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004940 if (ret) {
4941 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004942 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004943 }
Yan, Zheng80825102009-11-12 09:35:36 +00004944
Miao Xief4a2f4c2011-12-14 20:12:01 -05004945 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004946 if (IS_ERR(trans)) {
4947 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004948 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004949 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004950
4951 i_size_write(inode, newsize);
4952 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304953 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004954 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004955 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004956 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004957 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004958
Josef Bacika41ad392011-01-31 15:30:16 -05004959 /*
4960 * We're truncating a file that used to have good data down to
4961 * zero. Make sure it gets into the ordered flush list so that
4962 * any new writes get down to disk quickly.
4963 */
4964 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004965 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4966 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004967
Josef Bacikf3fe8202013-01-07 17:03:21 -05004968 /*
4969 * 1 for the orphan item we're going to add
4970 * 1 for the orphan item deletion.
4971 */
4972 trans = btrfs_start_transaction(root, 2);
4973 if (IS_ERR(trans))
4974 return PTR_ERR(trans);
4975
4976 /*
4977 * We need to do this in case we fail at _any_ point during the
4978 * actual truncate. Once we do the truncate_setsize we could
4979 * invalidate pages which forces any outstanding ordered io to
4980 * be instantly completed which will give us extents that need
4981 * to be truncated. If we fail to get an orphan inode down we
4982 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004983 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05004984 * will be consistent.
4985 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004986 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004987 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004988 if (ret)
4989 return ret;
4990
Josef Bacika41ad392011-01-31 15:30:16 -05004991 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4992 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004993
4994 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02004995 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004996 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02004997 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004998
Josef Bacika41ad392011-01-31 15:30:16 -05004999 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005000 if (ret && inode->i_nlink) {
5001 int err;
5002
Liu Bo19fd2df2016-12-01 13:01:02 -08005003 /* To get a stable disk_i_size */
5004 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5005 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005006 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005007 return err;
5008 }
5009
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005010 /*
5011 * failed to truncate, disk_i_size is only adjusted down
5012 * as we remove extents, so it should represent the true
5013 * size of the inode, so reset the in memory size and
5014 * delete our orphan entry.
5015 */
5016 trans = btrfs_join_transaction(root);
5017 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005018 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005019 return ret;
5020 }
5021 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005022 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005023 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005024 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005025 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005026 }
Yan, Zheng80825102009-11-12 09:35:36 +00005027 }
5028
Josef Bacika41ad392011-01-31 15:30:16 -05005029 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005030}
5031
Chris Mason39279cc2007-06-12 06:35:45 -04005032static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5033{
David Howells2b0143b2015-03-17 22:25:59 +00005034 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005035 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005036 int err;
5037
Li Zefanb83cc962010-12-20 16:04:08 +08005038 if (btrfs_root_readonly(root))
5039 return -EROFS;
5040
Jan Kara31051c82016-05-26 16:55:18 +02005041 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005042 if (err)
5043 return err;
5044
Chris Mason5a3f23d2009-03-31 13:27:11 -04005045 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005046 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005047 if (err)
5048 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005049 }
Yan Zheng9036c102008-10-30 14:19:41 -04005050
Christoph Hellwig10257742010-06-04 11:30:02 +02005051 if (attr->ia_valid) {
5052 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005053 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005054 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005055
Josef Bacik22c44fe2011-11-30 10:45:38 -05005056 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005057 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005058 }
5059
Chris Mason39279cc2007-06-12 06:35:45 -04005060 return err;
5061}
Chris Mason61295eb2008-01-14 16:24:38 -05005062
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005063/*
5064 * While truncating the inode pages during eviction, we get the VFS calling
5065 * btrfs_invalidatepage() against each page of the inode. This is slow because
5066 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5067 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5068 * extent_state structures over and over, wasting lots of time.
5069 *
5070 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5071 * those expensive operations on a per page basis and do only the ordered io
5072 * finishing, while we release here the extent_map and extent_state structures,
5073 * without the excessive merging and splitting.
5074 */
5075static void evict_inode_truncate_pages(struct inode *inode)
5076{
5077 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5078 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5079 struct rb_node *node;
5080
5081 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005082 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005083
5084 write_lock(&map_tree->lock);
5085 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5086 struct extent_map *em;
5087
5088 node = rb_first(&map_tree->map);
5089 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005090 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5091 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005092 remove_extent_mapping(map_tree, em);
5093 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005094 if (need_resched()) {
5095 write_unlock(&map_tree->lock);
5096 cond_resched();
5097 write_lock(&map_tree->lock);
5098 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005099 }
5100 write_unlock(&map_tree->lock);
5101
Filipe Manana6ca07092015-05-26 00:55:42 +01005102 /*
5103 * Keep looping until we have no more ranges in the io tree.
5104 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005105 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5106 * still in progress (unlocked the pages in the bio but did not yet
5107 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005108 * ranges can still be locked and eviction started because before
5109 * submitting those bios, which are executed by a separate task (work
5110 * queue kthread), inode references (inode->i_count) were not taken
5111 * (which would be dropped in the end io callback of each bio).
5112 * Therefore here we effectively end up waiting for those bios and
5113 * anyone else holding locked ranges without having bumped the inode's
5114 * reference count - if we don't do it, when they access the inode's
5115 * io_tree to unlock a range it may be too late, leading to an
5116 * use-after-free issue.
5117 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005118 spin_lock(&io_tree->lock);
5119 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5120 struct extent_state *state;
5121 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005122 u64 start;
5123 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005124
5125 node = rb_first(&io_tree->state);
5126 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005127 start = state->start;
5128 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005129 spin_unlock(&io_tree->lock);
5130
David Sterbaff13db42015-12-03 14:30:40 +01005131 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005132
5133 /*
5134 * If still has DELALLOC flag, the extent didn't reach disk,
5135 * and its reserved space won't be freed by delayed_ref.
5136 * So we need to free its reserved space here.
5137 * (Refer to comment in btrfs_invalidatepage, case 2)
5138 *
5139 * Note, end is the bytenr of last byte, so we need + 1 here.
5140 */
5141 if (state->state & EXTENT_DELALLOC)
5142 btrfs_qgroup_free_data(inode, start, end - start + 1);
5143
Filipe Manana6ca07092015-05-26 00:55:42 +01005144 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005145 EXTENT_LOCKED | EXTENT_DIRTY |
5146 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5147 EXTENT_DEFRAG, 1, 1,
5148 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005149
Filipe Manana7064dd52014-08-08 02:47:05 +01005150 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005151 spin_lock(&io_tree->lock);
5152 }
5153 spin_unlock(&io_tree->lock);
5154}
5155
Al Virobd555972010-06-07 11:35:40 -04005156void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005157{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005158 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005159 struct btrfs_trans_handle *trans;
5160 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005161 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005162 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005163 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005164 int ret;
5165
liubo1abe9b82011-03-24 11:18:59 +00005166 trace_btrfs_inode_evict(inode);
5167
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005168 if (!root) {
5169 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5170 return;
5171 }
5172
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005173 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005174
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005175 evict_inode_truncate_pages(inode);
5176
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005177 if (inode->i_nlink &&
5178 ((btrfs_root_refs(&root->root_item) != 0 &&
5179 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005180 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005181 goto no_delete;
5182
Chris Mason39279cc2007-06-12 06:35:45 -04005183 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005184 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005185 goto no_delete;
5186 }
Al Virobd555972010-06-07 11:35:40 -04005187 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005188 if (!special_file(inode->i_mode))
5189 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005190
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005191 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005192
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005193 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005194 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005195 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005196 goto no_delete;
5197 }
5198
Yan, Zheng76dda932009-09-21 16:00:26 -04005199 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005200 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5201 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005202 goto no_delete;
5203 }
5204
Nikolay Borisovaa790212017-01-10 20:35:40 +02005205 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005206 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005207 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005208 goto no_delete;
5209 }
5210
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005211 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005212 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005213 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005214 goto no_delete;
5215 }
Josef Bacik4a338542011-08-29 11:01:31 -04005216 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005217 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005218 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005219
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005220 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005221
Josef Bacik4289a662011-08-05 13:22:24 -04005222 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005223 * This is a bit simpler than btrfs_truncate since we've already
5224 * reserved our space for our orphan item in the unlink, so we just
5225 * need to reserve some slack space in case we add bytes and update
5226 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005227 */
Yan, Zheng80825102009-11-12 09:35:36 +00005228 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005229 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5230 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005231
Josef Bacik726c35f2011-09-26 15:46:06 -04005232 /*
5233 * Try and steal from the global reserve since we will
5234 * likely not use this space anyway, we want to try as
5235 * hard as possible to get this to work.
5236 */
5237 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005238 steal_from_global++;
5239 else
5240 steal_from_global = 0;
5241 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005242
Josef Bacik3bce8762015-02-24 12:35:51 -08005243 /*
5244 * steal_from_global == 0: we reserved stuff, hooray!
5245 * steal_from_global == 1: we didn't reserve stuff, boo!
5246 * steal_from_global == 2: we've committed, still not a lot of
5247 * room but maybe we'll have room in the global reserve this
5248 * time.
5249 * steal_from_global == 3: abandon all hope!
5250 */
5251 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005252 btrfs_warn(fs_info,
5253 "Could not get space for a delete, will truncate on mount %d",
5254 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005255 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005256 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005257 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005258 }
5259
Miao Xie0e8c36a2012-12-19 06:59:51 +00005260 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005261 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005262 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005263 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005264 goto no_delete;
5265 }
5266
Josef Bacik3bce8762015-02-24 12:35:51 -08005267 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005268 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005269 * sure there is room to do it, if not we need to commit and try
5270 * again.
5271 */
5272 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005273 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005274 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005275 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005276 else
5277 ret = -ENOSPC;
5278 }
5279
5280 /*
5281 * Couldn't steal from the global reserve, we have too much
5282 * pending stuff built up, commit the transaction and try it
5283 * again.
5284 */
5285 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005286 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005287 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005288 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005289 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005290 goto no_delete;
5291 }
5292 continue;
5293 } else {
5294 steal_from_global = 0;
5295 }
5296
Josef Bacik4289a662011-08-05 13:22:24 -04005297 trans->block_rsv = rsv;
5298
Yan, Zhengd68fc572010-05-16 10:49:58 -04005299 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005300 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005301 break;
5302
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005303 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005304 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005305 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005306 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005307 }
5308
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005309 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005310
Josef Bacik4ef31a42013-08-13 14:10:08 -04005311 /*
5312 * Errors here aren't a big deal, it just means we leave orphan items
5313 * in the tree. They will be cleaned up on the next mount.
5314 */
Yan, Zheng80825102009-11-12 09:35:36 +00005315 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005316 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005317 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005318 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005319 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005320 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005321
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005322 trans->block_rsv = &fs_info->trans_block_rsv;
5323 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005324 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005325 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005326
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005327 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005328 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005329no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005330 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005331 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005332}
5333
5334/*
5335 * this returns the key found in the dir entry in the location pointer.
5336 * If no dir entries were found, location->objectid is 0.
5337 */
5338static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5339 struct btrfs_key *location)
5340{
5341 const char *name = dentry->d_name.name;
5342 int namelen = dentry->d_name.len;
5343 struct btrfs_dir_item *di;
5344 struct btrfs_path *path;
5345 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005346 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005347
5348 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005349 if (!path)
5350 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005351
David Sterbaf85b7372017-01-20 14:54:07 +01005352 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5353 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005354 if (IS_ERR(di))
5355 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005356
David Sterbac7040052011-04-19 18:00:01 +02005357 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005358 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005359
Chris Mason5f39d392007-10-15 16:14:19 -04005360 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005361out:
Chris Mason39279cc2007-06-12 06:35:45 -04005362 btrfs_free_path(path);
5363 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005364out_err:
5365 location->objectid = 0;
5366 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005367}
5368
5369/*
5370 * when we hit a tree root in a directory, the btrfs part of the inode
5371 * needs to be changed to reflect the root directory of the tree root. This
5372 * is kind of like crossing a mount point.
5373 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005374static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005375 struct inode *dir,
5376 struct dentry *dentry,
5377 struct btrfs_key *location,
5378 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005379{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005380 struct btrfs_path *path;
5381 struct btrfs_root *new_root;
5382 struct btrfs_root_ref *ref;
5383 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005384 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005385 int ret;
5386 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005387
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005388 path = btrfs_alloc_path();
5389 if (!path) {
5390 err = -ENOMEM;
5391 goto out;
5392 }
Chris Mason39279cc2007-06-12 06:35:45 -04005393
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005394 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005395 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5396 key.type = BTRFS_ROOT_REF_KEY;
5397 key.offset = location->objectid;
5398
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005399 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005400 if (ret) {
5401 if (ret < 0)
5402 err = ret;
5403 goto out;
5404 }
Chris Mason39279cc2007-06-12 06:35:45 -04005405
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005406 leaf = path->nodes[0];
5407 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005408 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005409 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5410 goto out;
5411
5412 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5413 (unsigned long)(ref + 1),
5414 dentry->d_name.len);
5415 if (ret)
5416 goto out;
5417
David Sterbab3b4aa72011-04-21 01:20:15 +02005418 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005419
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005420 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005421 if (IS_ERR(new_root)) {
5422 err = PTR_ERR(new_root);
5423 goto out;
5424 }
5425
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005426 *sub_root = new_root;
5427 location->objectid = btrfs_root_dirid(&new_root->root_item);
5428 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005429 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005430 err = 0;
5431out:
5432 btrfs_free_path(path);
5433 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005434}
5435
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005436static void inode_tree_add(struct inode *inode)
5437{
5438 struct btrfs_root *root = BTRFS_I(inode)->root;
5439 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005440 struct rb_node **p;
5441 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005442 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005443 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005444
Al Viro1d3382cb2010-10-23 15:19:20 -04005445 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005446 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005447 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005448 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005449 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005450 while (*p) {
5451 parent = *p;
5452 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5453
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005454 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005455 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005456 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005457 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005458 else {
5459 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005460 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005461 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005462 RB_CLEAR_NODE(parent);
5463 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005464 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005465 }
5466 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005467 rb_link_node(new, parent, p);
5468 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005469 spin_unlock(&root->inode_lock);
5470}
5471
5472static void inode_tree_del(struct inode *inode)
5473{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005474 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005475 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005476 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005477
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005478 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005479 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005480 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005481 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005482 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005483 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005484 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005485
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005486 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005487 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005488 spin_lock(&root->inode_lock);
5489 empty = RB_EMPTY_ROOT(&root->inode_tree);
5490 spin_unlock(&root->inode_lock);
5491 if (empty)
5492 btrfs_add_dead_root(root);
5493 }
5494}
5495
Jeff Mahoney143bede2012-03-01 14:56:26 +01005496void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005497{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005498 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005499 struct rb_node *node;
5500 struct rb_node *prev;
5501 struct btrfs_inode *entry;
5502 struct inode *inode;
5503 u64 objectid = 0;
5504
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005505 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005506 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005507
5508 spin_lock(&root->inode_lock);
5509again:
5510 node = root->inode_tree.rb_node;
5511 prev = NULL;
5512 while (node) {
5513 prev = node;
5514 entry = rb_entry(node, struct btrfs_inode, rb_node);
5515
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005516 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005517 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005518 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005519 node = node->rb_right;
5520 else
5521 break;
5522 }
5523 if (!node) {
5524 while (prev) {
5525 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005526 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005527 node = prev;
5528 break;
5529 }
5530 prev = rb_next(prev);
5531 }
5532 }
5533 while (node) {
5534 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005535 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005536 inode = igrab(&entry->vfs_inode);
5537 if (inode) {
5538 spin_unlock(&root->inode_lock);
5539 if (atomic_read(&inode->i_count) > 1)
5540 d_prune_aliases(inode);
5541 /*
Al Viro45321ac2010-06-07 13:43:19 -04005542 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005543 * the inode cache when its usage count
5544 * hits zero.
5545 */
5546 iput(inode);
5547 cond_resched();
5548 spin_lock(&root->inode_lock);
5549 goto again;
5550 }
5551
5552 if (cond_resched_lock(&root->inode_lock))
5553 goto again;
5554
5555 node = rb_next(node);
5556 }
5557 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005558}
5559
Chris Masone02119d2008-09-05 16:13:11 -04005560static int btrfs_init_locked_inode(struct inode *inode, void *p)
5561{
5562 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005563 inode->i_ino = args->location->objectid;
5564 memcpy(&BTRFS_I(inode)->location, args->location,
5565 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005566 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005567 return 0;
5568}
5569
5570static int btrfs_find_actor(struct inode *inode, void *opaque)
5571{
5572 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005573 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005574 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005575}
5576
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005577static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005578 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005579 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005580{
5581 struct inode *inode;
5582 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005583 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005584
Chris Mason90d3e592014-01-09 17:28:00 -08005585 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005586 args.root = root;
5587
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005588 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005589 btrfs_init_locked_inode,
5590 (void *)&args);
5591 return inode;
5592}
5593
Balaji Rao1a54ef82008-07-21 02:01:04 +05305594/* Get an inode object given its location and corresponding root.
5595 * Returns in *is_new if the inode was read from disk
5596 */
5597struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005598 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305599{
5600 struct inode *inode;
5601
Chris Mason90d3e592014-01-09 17:28:00 -08005602 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305603 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005604 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305605
5606 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005607 int ret;
5608
5609 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005610 if (!is_bad_inode(inode)) {
5611 inode_tree_add(inode);
5612 unlock_new_inode(inode);
5613 if (new)
5614 *new = 1;
5615 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005616 unlock_new_inode(inode);
5617 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005618 ASSERT(ret < 0);
5619 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005620 }
5621 }
5622
Balaji Rao1a54ef82008-07-21 02:01:04 +05305623 return inode;
5624}
5625
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005626static struct inode *new_simple_dir(struct super_block *s,
5627 struct btrfs_key *key,
5628 struct btrfs_root *root)
5629{
5630 struct inode *inode = new_inode(s);
5631
5632 if (!inode)
5633 return ERR_PTR(-ENOMEM);
5634
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005635 BTRFS_I(inode)->root = root;
5636 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005637 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638
5639 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005640 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005641 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005642 inode->i_fop = &simple_dir_operations;
5643 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005644 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305645 inode->i_atime = inode->i_mtime;
5646 inode->i_ctime = inode->i_mtime;
5647 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005648
5649 return inode;
5650}
5651
Chris Mason3de45862008-11-17 21:02:50 -05005652struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005653{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005654 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005655 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005656 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005657 struct btrfs_root *sub_root = root;
5658 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005659 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005660 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005661
5662 if (dentry->d_name.len > BTRFS_NAME_LEN)
5663 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005664
Jeff Layton39e3c952012-11-28 11:30:53 -05005665 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005666 if (ret < 0)
5667 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005668
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005669 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005670 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005671
5672 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005673 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005674 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005675 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005676
5677 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5678
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005679 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005680 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005681 &location, &sub_root);
5682 if (ret < 0) {
5683 if (ret != -ENOENT)
5684 inode = ERR_PTR(ret);
5685 else
5686 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5687 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005688 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005689 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005690 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005691
Julia Lawall34d19ba2011-01-24 19:55:19 +00005692 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005693 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005694 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005695 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005696 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005697 if (ret) {
5698 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005699 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005700 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005701 }
5702
Chris Mason3de45862008-11-17 21:02:50 -05005703 return inode;
5704}
5705
Nick Pigginfe15ce42011-01-07 17:49:23 +11005706static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005707{
5708 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005709 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005710
Li Zefan848cce02012-02-21 17:04:28 +08005711 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005712 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005713
Li Zefan848cce02012-02-21 17:04:28 +08005714 if (inode) {
5715 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005716 if (btrfs_root_refs(&root->root_item) == 0)
5717 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005718
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005719 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005720 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005721 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005722 return 0;
5723}
5724
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005725static void btrfs_dentry_release(struct dentry *dentry)
5726{
Daeseok Youn944a4512014-04-14 15:37:02 +09005727 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005728}
5729
Chris Mason3de45862008-11-17 21:02:50 -05005730static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005731 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005732{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005733 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005734
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005735 inode = btrfs_lookup_dentry(dir, dentry);
5736 if (IS_ERR(inode)) {
5737 if (PTR_ERR(inode) == -ENOENT)
5738 inode = NULL;
5739 else
5740 return ERR_CAST(inode);
5741 }
5742
Al Viro41d28bc2014-10-12 22:24:21 -04005743 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005744}
5745
Miao Xie16cdcec2011-04-22 18:12:22 +08005746unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005747 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5748};
5749
Al Viro9cdda8d2013-05-22 16:48:09 -04005750static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005751{
Al Viro9cdda8d2013-05-22 16:48:09 -04005752 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005753 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005754 struct btrfs_root *root = BTRFS_I(inode)->root;
5755 struct btrfs_item *item;
5756 struct btrfs_dir_item *di;
5757 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005758 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005759 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005760 struct list_head ins_list;
5761 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005762 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005763 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005764 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005765 unsigned char d_type;
5766 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005767 char tmp_name[32];
5768 char *name_ptr;
5769 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005770 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005771 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005772
Al Viro9cdda8d2013-05-22 16:48:09 -04005773 if (!dir_emit_dots(file, ctx))
5774 return 0;
5775
David Woodhouse49593bf2008-08-17 17:08:36 +01005776 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005777 if (!path)
5778 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005779
David Sterbae4058b52015-11-27 16:31:35 +01005780 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005781
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005782 INIT_LIST_HEAD(&ins_list);
5783 INIT_LIST_HEAD(&del_list);
5784 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005785
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005786 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005787 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005788 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005789
Chris Mason39279cc2007-06-12 06:35:45 -04005790 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5791 if (ret < 0)
5792 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005793
5794 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005795 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005796 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005797 if (slot >= btrfs_header_nritems(leaf)) {
5798 ret = btrfs_next_leaf(root, path);
5799 if (ret < 0)
5800 goto err;
5801 else if (ret > 0)
5802 break;
5803 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005804 }
Chris Mason3de45862008-11-17 21:02:50 -05005805
Ross Kirkdd3cc162013-09-16 15:58:09 +01005806 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005807 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5808
5809 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005810 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005811 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005812 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005813 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005814 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005815 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005816 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005817
Al Viro9cdda8d2013-05-22 16:48:09 -04005818 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005819
Chris Mason39279cc2007-06-12 06:35:45 -04005820 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005821 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005822 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005823
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005824 name_len = btrfs_dir_name_len(leaf, di);
5825 if (name_len <= sizeof(tmp_name)) {
5826 name_ptr = tmp_name;
5827 } else {
5828 name_ptr = kmalloc(name_len, GFP_KERNEL);
5829 if (!name_ptr) {
5830 ret = -ENOMEM;
5831 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005832 }
Chris Mason39279cc2007-06-12 06:35:45 -04005833 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005834 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5835 name_len);
5836
5837 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5838 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5839
5840 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5841 d_type);
5842
5843 if (name_ptr != tmp_name)
5844 kfree(name_ptr);
5845
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005846 if (over)
5847 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005848 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005849next:
5850 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005851 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005852
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005853 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005854 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005855 goto nopos;
5856
Zach Browndb62efb2013-07-11 16:19:42 -07005857 /*
5858 * Stop new entries from being returned after we return the last
5859 * entry.
5860 *
5861 * New directory entries are assigned a strictly increasing
5862 * offset. This means that new entries created during readdir
5863 * are *guaranteed* to be seen in the future by that readdir.
5864 * This has broken buggy programs which operate on names as
5865 * they're returned by readdir. Until we re-use freed offsets
5866 * we have this hack to stop new entries from being returned
5867 * under the assumption that they'll never reach this huge
5868 * offset.
5869 *
5870 * This is being careful not to overflow 32bit loff_t unless the
5871 * last entry requires it because doing so has broken 32bit apps
5872 * in the past.
5873 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005874 if (ctx->pos >= INT_MAX)
5875 ctx->pos = LLONG_MAX;
5876 else
5877 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005878nopos:
5879 ret = 0;
5880err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005881 if (put)
5882 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005883 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005884 return ret;
5885}
5886
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005887int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005888{
5889 struct btrfs_root *root = BTRFS_I(inode)->root;
5890 struct btrfs_trans_handle *trans;
5891 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005892 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005893
Josef Bacik72ac3c02012-05-23 14:13:11 -04005894 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005895 return 0;
5896
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005897 if (btrfs_fs_closing(root->fs_info) &&
5898 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08005899 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005900
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005901 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005902 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005903 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005904 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005905 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005906 if (IS_ERR(trans))
5907 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005908 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005909 }
5910 return ret;
5911}
5912
5913/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005914 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005915 * inode changes. But, it is most likely to find the inode in cache.
5916 * FIXME, needs more benchmarking...there are no reasons other than performance
5917 * to keep or drop this code.
5918 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005919static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005920{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005922 struct btrfs_root *root = BTRFS_I(inode)->root;
5923 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005924 int ret;
5925
Josef Bacik72ac3c02012-05-23 14:13:11 -04005926 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005927 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005928
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005929 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005930 if (IS_ERR(trans))
5931 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005932
5933 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005934 if (ret && ret == -ENOSPC) {
5935 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005936 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005937 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005938 if (IS_ERR(trans))
5939 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005940
Chris Mason94b60442010-05-26 11:02:00 -04005941 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005942 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005943 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005944 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005945 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005946
5947 return ret;
5948}
5949
5950/*
5951 * This is a copy of file_update_time. We need this so we can return error on
5952 * ENOSPC for updating the inode in the case of file write and mmap writes.
5953 */
Josef Bacike41f9412012-03-26 09:46:47 -04005954static int btrfs_update_time(struct inode *inode, struct timespec *now,
5955 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005956{
Alexander Block2bc5565282012-06-15 09:49:33 +02005957 struct btrfs_root *root = BTRFS_I(inode)->root;
5958
5959 if (btrfs_root_readonly(root))
5960 return -EROFS;
5961
Josef Bacike41f9412012-03-26 09:46:47 -04005962 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005963 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005964 if (flags & S_CTIME)
5965 inode->i_ctime = *now;
5966 if (flags & S_MTIME)
5967 inode->i_mtime = *now;
5968 if (flags & S_ATIME)
5969 inode->i_atime = *now;
5970 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005971}
5972
Chris Masond352ac62008-09-29 15:18:18 -04005973/*
5974 * find the highest existing sequence number in a directory
5975 * and then set the in-memory index_cnt variable to reflect
5976 * free sequence numbers
5977 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005978static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005979{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005980 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005981 struct btrfs_key key, found_key;
5982 struct btrfs_path *path;
5983 struct extent_buffer *leaf;
5984 int ret;
5985
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005986 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005987 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005988 key.offset = (u64)-1;
5989
5990 path = btrfs_alloc_path();
5991 if (!path)
5992 return -ENOMEM;
5993
5994 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5995 if (ret < 0)
5996 goto out;
5997 /* FIXME: we should be able to handle this */
5998 if (ret == 0)
5999 goto out;
6000 ret = 0;
6001
6002 /*
6003 * MAGIC NUMBER EXPLANATION:
6004 * since we search a directory based on f_pos we have to start at 2
6005 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6006 * else has to start at 2
6007 */
6008 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006009 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006010 goto out;
6011 }
6012
6013 path->slots[0]--;
6014
6015 leaf = path->nodes[0];
6016 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6017
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006018 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006019 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006020 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006021 goto out;
6022 }
6023
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006024 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006025out:
6026 btrfs_free_path(path);
6027 return ret;
6028}
6029
Chris Masond352ac62008-09-29 15:18:18 -04006030/*
6031 * helper to find a free sequence number in a given directory. This current
6032 * code is very simple, later versions will do smarter things in the btree
6033 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006034int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006035{
6036 int ret = 0;
6037
Nikolay Borisov877574e2017-02-20 13:50:33 +02006038 if (dir->index_cnt == (u64)-1) {
6039 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006040 if (ret) {
Nikolay Borisov877574e2017-02-20 13:50:33 +02006041 ret = btrfs_set_inode_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006042 if (ret)
6043 return ret;
6044 }
Josef Bacikaec74772008-07-24 12:12:38 -04006045 }
6046
Nikolay Borisov877574e2017-02-20 13:50:33 +02006047 *index = dir->index_cnt;
6048 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006049
6050 return ret;
6051}
6052
Chris Masonb0d5d102014-09-08 13:08:51 -07006053static int btrfs_insert_inode_locked(struct inode *inode)
6054{
6055 struct btrfs_iget_args args;
6056 args.location = &BTRFS_I(inode)->location;
6057 args.root = BTRFS_I(inode)->root;
6058
6059 return insert_inode_locked4(inode,
6060 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6061 btrfs_find_actor, &args);
6062}
6063
Chris Mason39279cc2007-06-12 06:35:45 -04006064static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6065 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006066 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006067 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006068 u64 ref_objectid, u64 objectid,
6069 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006070{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006071 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006072 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006073 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006074 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006075 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006076 struct btrfs_inode_ref *ref;
6077 struct btrfs_key key[2];
6078 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006079 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006080 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006081 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006082
Chris Mason5f39d392007-10-15 16:14:19 -04006083 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006084 if (!path)
6085 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006086
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006087 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006088 if (!inode) {
6089 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006090 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006091 }
Chris Mason39279cc2007-06-12 06:35:45 -04006092
Li Zefan581bb052011-04-20 10:06:11 +08006093 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006094 * O_TMPFILE, set link count to 0, so that after this point,
6095 * we fill in an inode item with the correct link count.
6096 */
6097 if (!name)
6098 set_nlink(inode, 0);
6099
6100 /*
Li Zefan581bb052011-04-20 10:06:11 +08006101 * we have to initialize this early, so we can reclaim the inode
6102 * number if we fail afterwards in this function.
6103 */
6104 inode->i_ino = objectid;
6105
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006106 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006107 trace_btrfs_inode_request(dir);
6108
Nikolay Borisov877574e2017-02-20 13:50:33 +02006109 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006110 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006111 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006112 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006113 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006114 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006115 } else if (dir) {
6116 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006117 }
6118 /*
6119 * index_cnt is ignored for everything but a dir,
6120 * btrfs_get_inode_index_count has an explanation for the magic
6121 * number
6122 */
6123 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006124 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006125 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006126 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006127 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006128
Josef Bacik5dc562c2012-08-17 13:14:17 -04006129 /*
6130 * We could have gotten an inode number from somebody who was fsynced
6131 * and then removed in this same transaction, so let's just set full
6132 * sync since it will be a full sync anyway and this will blow away the
6133 * old info in the log.
6134 */
6135 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6136
Chris Mason9c583092008-01-29 15:15:18 -05006137 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006138 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006139 key[0].offset = 0;
6140
Chris Mason9c583092008-01-29 15:15:18 -05006141 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006142
6143 if (name) {
6144 /*
6145 * Start new inodes with an inode_ref. This is slightly more
6146 * efficient for small numbers of hard links since they will
6147 * be packed into one item. Extended refs will kick in if we
6148 * add more hard links than can fit in the ref item.
6149 */
6150 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006151 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006152 key[1].offset = ref_objectid;
6153
6154 sizes[1] = name_len + sizeof(*ref);
6155 }
Chris Mason9c583092008-01-29 15:15:18 -05006156
Chris Masonb0d5d102014-09-08 13:08:51 -07006157 location = &BTRFS_I(inode)->location;
6158 location->objectid = objectid;
6159 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006160 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006161
6162 ret = btrfs_insert_inode_locked(inode);
6163 if (ret < 0)
6164 goto fail;
6165
Chris Masonb9473432009-03-13 11:00:37 -04006166 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006167 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006168 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006169 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006170
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006171 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006172 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306173
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006174 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306175 inode->i_atime = inode->i_mtime;
6176 inode->i_ctime = inode->i_mtime;
6177 BTRFS_I(inode)->i_otime = inode->i_mtime;
6178
Chris Mason5f39d392007-10-15 16:14:19 -04006179 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6180 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006181 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006182 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006183 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006184
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006185 if (name) {
6186 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6187 struct btrfs_inode_ref);
6188 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6189 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6190 ptr = (unsigned long)(ref + 1);
6191 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6192 }
Chris Mason9c583092008-01-29 15:15:18 -05006193
Chris Mason5f39d392007-10-15 16:14:19 -04006194 btrfs_mark_buffer_dirty(path->nodes[0]);
6195 btrfs_free_path(path);
6196
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006197 btrfs_inherit_iflags(inode, dir);
6198
Al Viro569254b2011-07-24 17:08:40 -04006199 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006200 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006201 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006202 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006203 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6204 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006205 }
6206
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006207 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006208
6209 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006210 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006211
Alexander Block8ea05e32012-07-25 17:35:53 +02006212 btrfs_update_root_times(trans, root);
6213
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006214 ret = btrfs_inode_inherit_props(trans, inode, dir);
6215 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006216 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006217 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006218 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006219
Chris Mason39279cc2007-06-12 06:35:45 -04006220 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006221
6222fail_unlock:
6223 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006224fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006225 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006226 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006227 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006228 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006229 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006230}
6231
6232static inline u8 btrfs_inode_type(struct inode *inode)
6233{
6234 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6235}
6236
Chris Masond352ac62008-09-29 15:18:18 -04006237/*
6238 * utility function to add 'inode' into 'parent_inode' with
6239 * a give name and a given sequence number.
6240 * if 'add_backref' is true, also insert a backref from the
6241 * inode to the parent directory.
6242 */
Chris Masone02119d2008-09-05 16:13:11 -04006243int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006244 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006245 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006246{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006247 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006248 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006249 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006250 struct btrfs_root *root = parent_inode->root;
6251 u64 ino = btrfs_ino(inode);
6252 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006253
Li Zefan33345d012011-04-20 10:31:50 +08006254 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006255 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006256 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006257 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006258 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006259 key.offset = 0;
6260 }
Chris Mason39279cc2007-06-12 06:35:45 -04006261
Li Zefan33345d012011-04-20 10:31:50 +08006262 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006263 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6264 root->root_key.objectid, parent_ino,
6265 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006266 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006267 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6268 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006269 }
6270
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006271 /* Nothing to clean up yet */
6272 if (ret)
6273 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006274
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006275 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006276 parent_inode, &key,
6277 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006278 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006279 goto fail_dir_item;
6280 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006281 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006282 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006283 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006284
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006285 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006286 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006287 inode_inc_iversion(&parent_inode->vfs_inode);
6288 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6289 current_time(&parent_inode->vfs_inode);
6290 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006291 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006292 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006293 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006294
6295fail_dir_item:
6296 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6297 u64 local_index;
6298 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006299 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6300 root->root_key.objectid, parent_ino,
6301 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006302
6303 } else if (add_backref) {
6304 u64 local_index;
6305 int err;
6306
6307 err = btrfs_del_inode_ref(trans, root, name, name_len,
6308 ino, parent_ino, &local_index);
6309 }
6310 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006311}
6312
6313static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006314 struct btrfs_inode *dir, struct dentry *dentry,
6315 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006316{
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006317 int err = btrfs_add_link(trans, dir, inode,
Josef Bacika1b075d2010-11-19 20:36:11 +00006318 dentry->d_name.name, dentry->d_name.len,
6319 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006320 if (err > 0)
6321 err = -EEXIST;
6322 return err;
6323}
6324
Josef Bacik618e21d2007-07-11 10:18:17 -04006325static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006326 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006327{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006328 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006329 struct btrfs_trans_handle *trans;
6330 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006331 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006332 int err;
6333 int drop_inode = 0;
6334 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006335 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006336
Josef Bacik9ed74f22009-09-11 16:12:44 -04006337 /*
6338 * 2 for inode item and ref
6339 * 2 for dir items
6340 * 1 for xattr if selinux is on
6341 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006342 trans = btrfs_start_transaction(root, 5);
6343 if (IS_ERR(trans))
6344 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006345
Li Zefan581bb052011-04-20 10:06:11 +08006346 err = btrfs_find_free_ino(root, &objectid);
6347 if (err)
6348 goto out_unlock;
6349
Josef Bacikaec74772008-07-24 12:12:38 -04006350 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006351 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6352 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006353 if (IS_ERR(inode)) {
6354 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006355 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006356 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006357
Casey Schauflerad19db72011-12-15 10:09:07 -05006358 /*
6359 * If the active LSM wants to access the inode during
6360 * d_instantiate it needs these. Smack checks to see
6361 * if the filesystem supports xattrs by looking at the
6362 * ops vector.
6363 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006364 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006365 init_special_inode(inode, inode->i_mode, rdev);
6366
6367 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006368 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006369 goto out_unlock_inode;
6370
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006371 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6372 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006373 if (err) {
6374 goto out_unlock_inode;
6375 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006376 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006377 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006378 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006379 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006380
Josef Bacik618e21d2007-07-11 10:18:17 -04006381out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006382 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006383 btrfs_balance_delayed_items(fs_info);
6384 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006385 if (drop_inode) {
6386 inode_dec_link_count(inode);
6387 iput(inode);
6388 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006389 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006390
6391out_unlock_inode:
6392 drop_inode = 1;
6393 unlock_new_inode(inode);
6394 goto out_unlock;
6395
Josef Bacik618e21d2007-07-11 10:18:17 -04006396}
6397
Chris Mason39279cc2007-06-12 06:35:45 -04006398static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006399 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006400{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006401 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006402 struct btrfs_trans_handle *trans;
6403 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006404 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006405 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006406 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006407 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006408 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006409
Josef Bacik9ed74f22009-09-11 16:12:44 -04006410 /*
6411 * 2 for inode item and ref
6412 * 2 for dir items
6413 * 1 for xattr if selinux is on
6414 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006415 trans = btrfs_start_transaction(root, 5);
6416 if (IS_ERR(trans))
6417 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006418
Li Zefan581bb052011-04-20 10:06:11 +08006419 err = btrfs_find_free_ino(root, &objectid);
6420 if (err)
6421 goto out_unlock;
6422
Josef Bacikaec74772008-07-24 12:12:38 -04006423 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006424 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6425 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006426 if (IS_ERR(inode)) {
6427 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006428 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006429 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006430 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006431 /*
6432 * If the active LSM wants to access the inode during
6433 * d_instantiate it needs these. Smack checks to see
6434 * if the filesystem supports xattrs by looking at the
6435 * ops vector.
6436 */
6437 inode->i_fop = &btrfs_file_operations;
6438 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006439 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006440
6441 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6442 if (err)
6443 goto out_unlock_inode;
6444
6445 err = btrfs_update_inode(trans, root, inode);
6446 if (err)
6447 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006448
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006449 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6450 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006451 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006452 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006453
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006454 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006455 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006456 d_instantiate(dentry, inode);
6457
Chris Mason39279cc2007-06-12 06:35:45 -04006458out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006459 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006460 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006461 inode_dec_link_count(inode);
6462 iput(inode);
6463 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006464 btrfs_balance_delayed_items(fs_info);
6465 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006466 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006467
6468out_unlock_inode:
6469 unlock_new_inode(inode);
6470 goto out_unlock;
6471
Chris Mason39279cc2007-06-12 06:35:45 -04006472}
6473
6474static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6475 struct dentry *dentry)
6476{
Filipe Manana271dba42016-01-05 16:24:05 +00006477 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006478 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006479 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006480 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006481 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006482 int err;
6483 int drop_inode = 0;
6484
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006485 /* do not allow sys_link's with other subvols of the same device */
6486 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006487 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006488
Mark Fashehf1863732012-08-08 11:32:27 -07006489 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006490 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006491
Nikolay Borisov877574e2017-02-20 13:50:33 +02006492 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006493 if (err)
6494 goto fail;
6495
Yan, Zhenga22285a2010-05-16 10:48:46 -04006496 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006497 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006498 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006499 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006500 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006501 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006502 if (IS_ERR(trans)) {
6503 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006504 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006505 goto fail;
6506 }
Chris Mason5f39d392007-10-15 16:14:19 -04006507
Miao Xie67de1172013-12-26 13:07:06 +08006508 /* There are several dir indexes for this inode, clear the cache. */
6509 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006510 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006511 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006512 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006513 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006514 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006515
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006516 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6517 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006518
Yan, Zhenga5719522009-09-24 09:17:31 -04006519 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006520 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006521 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006522 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006523 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006524 if (err)
6525 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006526 if (inode->i_nlink == 1) {
6527 /*
6528 * If new hard link count is 1, it's a file created
6529 * with open(2) O_TMPFILE flag.
6530 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006531 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006532 if (err)
6533 goto fail;
6534 }
Al Viro08c422c2011-12-23 07:58:13 -05006535 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006536 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006537 }
Chris Mason39279cc2007-06-12 06:35:45 -04006538
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006539 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006540fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006541 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006542 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006543 if (drop_inode) {
6544 inode_dec_link_count(inode);
6545 iput(inode);
6546 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006547 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006548 return err;
6549}
6550
Al Viro18bb1db2011-07-26 01:41:39 -04006551static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006552{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006553 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006554 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006555 struct btrfs_trans_handle *trans;
6556 struct btrfs_root *root = BTRFS_I(dir)->root;
6557 int err = 0;
6558 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006559 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006560 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006561
Josef Bacik9ed74f22009-09-11 16:12:44 -04006562 /*
6563 * 2 items for inode and ref
6564 * 2 items for dir items
6565 * 1 for xattr if selinux is on
6566 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006567 trans = btrfs_start_transaction(root, 5);
6568 if (IS_ERR(trans))
6569 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006570
Li Zefan581bb052011-04-20 10:06:11 +08006571 err = btrfs_find_free_ino(root, &objectid);
6572 if (err)
6573 goto out_fail;
6574
Josef Bacikaec74772008-07-24 12:12:38 -04006575 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006576 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6577 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006578 if (IS_ERR(inode)) {
6579 err = PTR_ERR(inode);
6580 goto out_fail;
6581 }
Chris Mason5f39d392007-10-15 16:14:19 -04006582
Chris Mason39279cc2007-06-12 06:35:45 -04006583 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006584 /* these must be set before we unlock the inode */
6585 inode->i_op = &btrfs_dir_inode_operations;
6586 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006587
Eric Paris2a7dba32011-02-01 11:05:39 -05006588 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006589 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006590 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006591
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006592 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006593 err = btrfs_update_inode(trans, root, inode);
6594 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006595 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006596
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006597 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6598 dentry->d_name.name,
6599 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006600 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006601 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006602
Chris Mason39279cc2007-06-12 06:35:45 -04006603 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006604 /*
6605 * mkdir is special. We're unlocking after we call d_instantiate
6606 * to avoid a race with nfsd calling d_instantiate.
6607 */
6608 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006609 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006610
6611out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006612 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006613 if (drop_on_err) {
6614 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006615 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006616 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006617 btrfs_balance_delayed_items(fs_info);
6618 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006619 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006620
6621out_fail_inode:
6622 unlock_new_inode(inode);
6623 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006624}
6625
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006626/* Find next extent map of a given extent map, caller needs to ensure locks */
6627static struct extent_map *next_extent_map(struct extent_map *em)
6628{
6629 struct rb_node *next;
6630
6631 next = rb_next(&em->rb_node);
6632 if (!next)
6633 return NULL;
6634 return container_of(next, struct extent_map, rb_node);
6635}
6636
6637static struct extent_map *prev_extent_map(struct extent_map *em)
6638{
6639 struct rb_node *prev;
6640
6641 prev = rb_prev(&em->rb_node);
6642 if (!prev)
6643 return NULL;
6644 return container_of(prev, struct extent_map, rb_node);
6645}
6646
Chris Masond352ac62008-09-29 15:18:18 -04006647/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006648 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006649 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006650 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006651 */
Chris Mason3b951512008-04-17 11:29:12 -04006652static int merge_extent_mapping(struct extent_map_tree *em_tree,
6653 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006654 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006655 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006656{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006657 struct extent_map *prev;
6658 struct extent_map *next;
6659 u64 start;
6660 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006661 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006662
Chris Masone6dcd2d2008-07-17 12:53:50 -04006663 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006664
6665 if (existing->start > map_start) {
6666 next = existing;
6667 prev = prev_extent_map(next);
6668 } else {
6669 prev = existing;
6670 next = next_extent_map(prev);
6671 }
6672
6673 start = prev ? extent_map_end(prev) : em->start;
6674 start = max_t(u64, start, em->start);
6675 end = next ? next->start : extent_map_end(em);
6676 end = min_t(u64, end, extent_map_end(em));
6677 start_diff = start - em->start;
6678 em->start = start;
6679 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006680 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6681 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006682 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006683 em->block_len -= start_diff;
6684 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006685 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006686}
6687
Chris Masonc8b97812008-10-29 14:49:59 -04006688static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006689 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006690 size_t pg_offset, u64 extent_offset,
6691 struct btrfs_file_extent_item *item)
6692{
6693 int ret;
6694 struct extent_buffer *leaf = path->nodes[0];
6695 char *tmp;
6696 size_t max_size;
6697 unsigned long inline_size;
6698 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006699 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006700
6701 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006702 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006703 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6704 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006705 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006706 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006707 if (!tmp)
6708 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006709 ptr = btrfs_file_extent_inline_start(item);
6710
6711 read_extent_buffer(leaf, tmp, ptr, inline_size);
6712
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006713 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006714 ret = btrfs_decompress(compress_type, tmp, page,
6715 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006716 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006717 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006718}
6719
Chris Masond352ac62008-09-29 15:18:18 -04006720/*
6721 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006722 * the ugly parts come from merging extents from the disk with the in-ram
6723 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006724 * where the in-ram extents might be locked pending data=ordered completion.
6725 *
6726 * This also copies inline extents directly into the page.
6727 */
Chris Masond3977122009-01-05 21:25:51 -05006728
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006729struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6730 struct page *page,
6731 size_t pg_offset, u64 start, u64 len,
6732 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006733{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006734 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006735 int ret;
6736 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006737 u64 extent_start = 0;
6738 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006739 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006740 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006741 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006742 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006743 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006744 struct extent_buffer *leaf;
6745 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006746 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006747 struct extent_map_tree *em_tree = &inode->extent_tree;
6748 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006749 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006750 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006751
Chris Masona52d9a82007-08-27 16:49:44 -04006752again:
Chris Mason890871b2009-09-02 16:24:52 -04006753 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006754 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006755 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006756 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006757 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006758
Chris Masona52d9a82007-08-27 16:49:44 -04006759 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006760 if (em->start > start || em->start + em->len <= start)
6761 free_extent_map(em);
6762 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006763 free_extent_map(em);
6764 else
6765 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006766 }
David Sterba172ddd62011-04-21 00:48:27 +02006767 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006768 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006769 err = -ENOMEM;
6770 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006771 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006772 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006773 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006774 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006775 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006776 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006777
6778 if (!path) {
6779 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006780 if (!path) {
6781 err = -ENOMEM;
6782 goto out;
6783 }
6784 /*
6785 * Chances are we'll be called again, so go ahead and do
6786 * readahead
6787 */
David Sterbae4058b52015-11-27 16:31:35 +01006788 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006789 }
6790
Chris Mason179e29e2007-11-01 11:28:41 -04006791 ret = btrfs_lookup_file_extent(trans, root, path,
6792 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006793 if (ret < 0) {
6794 err = ret;
6795 goto out;
6796 }
6797
6798 if (ret != 0) {
6799 if (path->slots[0] == 0)
6800 goto not_found;
6801 path->slots[0]--;
6802 }
6803
Chris Mason5f39d392007-10-15 16:14:19 -04006804 leaf = path->nodes[0];
6805 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006806 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006807 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006808 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006809 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006810 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006811 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006812 /*
6813 * If we backup past the first extent we want to move forward
6814 * and see if there is an extent in front of us, otherwise we'll
6815 * say there is a hole for our whole search range which can
6816 * cause problems.
6817 */
6818 extent_end = start;
6819 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006820 }
6821
Chris Mason5f39d392007-10-15 16:14:19 -04006822 found_type = btrfs_file_extent_type(leaf, item);
6823 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006824 if (found_type == BTRFS_FILE_EXTENT_REG ||
6825 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006826 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006827 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006828 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6829 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006830 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006831 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006832 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006833 }
Josef Bacik25a50342013-10-14 12:08:38 -04006834next:
Yan Zheng9036c102008-10-30 14:19:41 -04006835 if (start >= extent_end) {
6836 path->slots[0]++;
6837 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6838 ret = btrfs_next_leaf(root, path);
6839 if (ret < 0) {
6840 err = ret;
6841 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006842 }
Yan Zheng9036c102008-10-30 14:19:41 -04006843 if (ret > 0)
6844 goto not_found;
6845 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006846 }
Yan Zheng9036c102008-10-30 14:19:41 -04006847 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6848 if (found_key.objectid != objectid ||
6849 found_key.type != BTRFS_EXTENT_DATA_KEY)
6850 goto not_found;
6851 if (start + len <= found_key.offset)
6852 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006853 if (start > found_key.offset)
6854 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006855 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006856 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006857 em->len = found_key.offset - start;
6858 goto not_found_em;
6859 }
6860
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006861 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006862 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006863
Yan Zhengd899e052008-10-30 14:25:28 -04006864 if (found_type == BTRFS_FILE_EXTENT_REG ||
6865 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006866 goto insert;
6867 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006868 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006869 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006870 size_t size;
6871 size_t extent_offset;
6872 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006873
Filipe Manana7ffbb592014-06-09 03:48:05 +01006874 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006875 goto out;
Yan689f9342007-10-29 11:41:07 -04006876
Chris Mason514ac8a2014-01-03 21:07:00 -08006877 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006878 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006879 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6880 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006881 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006882 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006883 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006884 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006885 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006886 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006887 if (btrfs_file_extent_compression(leaf, item) !=
6888 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006889 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006890 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006891 if (ret) {
6892 err = ret;
6893 goto out;
6894 }
Chris Masonc8b97812008-10-29 14:49:59 -04006895 } else {
6896 map = kmap(page);
6897 read_extent_buffer(leaf, map + pg_offset, ptr,
6898 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006899 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006900 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006901 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006902 copy_size);
6903 }
Chris Masonc8b97812008-10-29 14:49:59 -04006904 kunmap(page);
6905 }
Chris Mason179e29e2007-11-01 11:28:41 -04006906 flush_dcache_page(page);
6907 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006908 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006909 if (!trans) {
6910 kunmap(page);
6911 free_extent_map(em);
6912 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006913
David Sterbab3b4aa72011-04-21 01:20:15 +02006914 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006915 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006916
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006917 if (IS_ERR(trans))
6918 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006919 goto again;
6920 }
Chris Masonc8b97812008-10-29 14:49:59 -04006921 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006922 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006923 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006924 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006925 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006926 }
Chris Masond1310b22008-01-24 16:13:08 -05006927 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006928 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006929 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006930 }
6931not_found:
6932 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006933 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006934 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006935not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006936 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006937 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006938insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006939 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006940 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006941 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006942 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6943 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006944 err = -EIO;
6945 goto out;
6946 }
Chris Masond1310b22008-01-24 16:13:08 -05006947
6948 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006949 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006950 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006951 /* it is possible that someone inserted the extent into the tree
6952 * while we had the lock dropped. It is also possible that
6953 * an overlapping map exists in the tree
6954 */
Chris Masona52d9a82007-08-27 16:49:44 -04006955 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006956 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006957
6958 ret = 0;
6959
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006960 existing = search_extent_mapping(em_tree, start, len);
6961 /*
6962 * existing will always be non-NULL, since there must be
6963 * extent causing the -EEXIST.
6964 */
Chris Mason8dff9c82015-09-19 11:28:25 -07006965 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006966 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07006967 em->block_start == existing->block_start) {
6968 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006969 * The existing extent map already encompasses the
6970 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07006971 */
6972 free_extent_map(em);
6973 em = existing;
6974 err = 0;
6975
6976 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006977 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006978 /*
6979 * The existing extent map is the one nearest to
6980 * the [start, start + len) range which overlaps
6981 */
6982 err = merge_extent_mapping(em_tree, existing,
6983 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006984 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006985 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006986 free_extent_map(em);
6987 em = NULL;
6988 }
6989 } else {
6990 free_extent_map(em);
6991 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006992 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006993 }
Chris Masona52d9a82007-08-27 16:49:44 -04006994 }
Chris Mason890871b2009-09-02 16:24:52 -04006995 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006996out:
liubo1abe9b82011-03-24 11:18:59 +00006997
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006998 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006999
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007000 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007001 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007002 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007003 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007004 err = ret;
7005 }
Chris Masona52d9a82007-08-27 16:49:44 -04007006 if (err) {
7007 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007008 return ERR_PTR(err);
7009 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007010 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007011 return em;
7012}
7013
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007014struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7015 struct page *page,
7016 size_t pg_offset, u64 start, u64 len,
7017 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007018{
7019 struct extent_map *em;
7020 struct extent_map *hole_em = NULL;
7021 u64 range_start = start;
7022 u64 end;
7023 u64 found;
7024 u64 found_end;
7025 int err = 0;
7026
7027 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7028 if (IS_ERR(em))
7029 return em;
7030 if (em) {
7031 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007032 * if our em maps to
7033 * - a hole or
7034 * - a pre-alloc extent,
7035 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007036 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007037 if (em->block_start != EXTENT_MAP_HOLE &&
7038 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007039 return em;
7040 else
7041 hole_em = em;
7042 }
7043
7044 /* check to see if we've wrapped (len == -1 or similar) */
7045 end = start + len;
7046 if (end < start)
7047 end = (u64)-1;
7048 else
7049 end -= 1;
7050
7051 em = NULL;
7052
7053 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007054 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007055 end, len, EXTENT_DELALLOC, 1);
7056 found_end = range_start + found;
7057 if (found_end < range_start)
7058 found_end = (u64)-1;
7059
7060 /*
7061 * we didn't find anything useful, return
7062 * the original results from get_extent()
7063 */
7064 if (range_start > end || found_end <= start) {
7065 em = hole_em;
7066 hole_em = NULL;
7067 goto out;
7068 }
7069
7070 /* adjust the range_start to make sure it doesn't
7071 * go backwards from the start they passed in
7072 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307073 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007074 found = found_end - range_start;
7075
7076 if (found > 0) {
7077 u64 hole_start = start;
7078 u64 hole_len = len;
7079
David Sterba172ddd62011-04-21 00:48:27 +02007080 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007081 if (!em) {
7082 err = -ENOMEM;
7083 goto out;
7084 }
7085 /*
7086 * when btrfs_get_extent can't find anything it
7087 * returns one huge hole
7088 *
7089 * make sure what it found really fits our range, and
7090 * adjust to make sure it is based on the start from
7091 * the caller
7092 */
7093 if (hole_em) {
7094 u64 calc_end = extent_map_end(hole_em);
7095
7096 if (calc_end <= start || (hole_em->start > end)) {
7097 free_extent_map(hole_em);
7098 hole_em = NULL;
7099 } else {
7100 hole_start = max(hole_em->start, start);
7101 hole_len = calc_end - hole_start;
7102 }
7103 }
7104 em->bdev = NULL;
7105 if (hole_em && range_start > hole_start) {
7106 /* our hole starts before our delalloc, so we
7107 * have to return just the parts of the hole
7108 * that go until the delalloc starts
7109 */
7110 em->len = min(hole_len,
7111 range_start - hole_start);
7112 em->start = hole_start;
7113 em->orig_start = hole_start;
7114 /*
7115 * don't adjust block start at all,
7116 * it is fixed at EXTENT_MAP_HOLE
7117 */
7118 em->block_start = hole_em->block_start;
7119 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007120 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7121 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007122 } else {
7123 em->start = range_start;
7124 em->len = found;
7125 em->orig_start = range_start;
7126 em->block_start = EXTENT_MAP_DELALLOC;
7127 em->block_len = found;
7128 }
7129 } else if (hole_em) {
7130 return hole_em;
7131 }
7132out:
7133
7134 free_extent_map(hole_em);
7135 if (err) {
7136 free_extent_map(em);
7137 return ERR_PTR(err);
7138 }
7139 return em;
7140}
7141
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007142static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7143 const u64 start,
7144 const u64 len,
7145 const u64 orig_start,
7146 const u64 block_start,
7147 const u64 block_len,
7148 const u64 orig_block_len,
7149 const u64 ram_bytes,
7150 const int type)
7151{
7152 struct extent_map *em = NULL;
7153 int ret;
7154
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007155 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007156 em = create_io_em(inode, start, len, orig_start,
7157 block_start, block_len, orig_block_len,
7158 ram_bytes,
7159 BTRFS_COMPRESS_NONE, /* compress_type */
7160 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007161 if (IS_ERR(em))
7162 goto out;
7163 }
7164 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7165 len, block_len, type);
7166 if (ret) {
7167 if (em) {
7168 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007169 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007170 start + len - 1, 0);
7171 }
7172 em = ERR_PTR(ret);
7173 }
7174 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007175
7176 return em;
7177}
7178
Josef Bacik4b46fce2010-05-23 11:00:55 -04007179static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7180 u64 start, u64 len)
7181{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007182 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007183 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007184 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007185 struct btrfs_key ins;
7186 u64 alloc_hint;
7187 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007188
Josef Bacik4b46fce2010-05-23 11:00:55 -04007189 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007190 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007191 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007192 if (ret)
7193 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007194
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007195 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7196 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007197 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007198 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007199 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007200 btrfs_free_reserved_extent(fs_info, ins.objectid,
7201 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007202
Josef Bacik4b46fce2010-05-23 11:00:55 -04007203 return em;
7204}
7205
Chris Mason46bfbb52010-05-26 11:04:10 -04007206/*
7207 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7208 * block must be cow'd
7209 */
Josef Bacik00361582013-08-14 14:02:47 -04007210noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007211 u64 *orig_start, u64 *orig_block_len,
7212 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007213{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007214 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007215 struct btrfs_path *path;
7216 int ret;
7217 struct extent_buffer *leaf;
7218 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007219 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007220 struct btrfs_file_extent_item *fi;
7221 struct btrfs_key key;
7222 u64 disk_bytenr;
7223 u64 backref_offset;
7224 u64 extent_end;
7225 u64 num_bytes;
7226 int slot;
7227 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007228 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007229
Chris Mason46bfbb52010-05-26 11:04:10 -04007230 path = btrfs_alloc_path();
7231 if (!path)
7232 return -ENOMEM;
7233
David Sterbaf85b7372017-01-20 14:54:07 +01007234 ret = btrfs_lookup_file_extent(NULL, root, path,
7235 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007236 if (ret < 0)
7237 goto out;
7238
7239 slot = path->slots[0];
7240 if (ret == 1) {
7241 if (slot == 0) {
7242 /* can't find the item, must cow */
7243 ret = 0;
7244 goto out;
7245 }
7246 slot--;
7247 }
7248 ret = 0;
7249 leaf = path->nodes[0];
7250 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007251 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007252 key.type != BTRFS_EXTENT_DATA_KEY) {
7253 /* not our file or wrong item type, must cow */
7254 goto out;
7255 }
7256
7257 if (key.offset > offset) {
7258 /* Wrong offset, must cow */
7259 goto out;
7260 }
7261
7262 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7263 found_type = btrfs_file_extent_type(leaf, fi);
7264 if (found_type != BTRFS_FILE_EXTENT_REG &&
7265 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7266 /* not a regular extent, must cow */
7267 goto out;
7268 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007269
7270 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7271 goto out;
7272
Miao Xiee77751a2013-12-27 21:11:50 +08007273 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7274 if (extent_end <= offset)
7275 goto out;
7276
Chris Mason46bfbb52010-05-26 11:04:10 -04007277 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007278 if (disk_bytenr == 0)
7279 goto out;
7280
7281 if (btrfs_file_extent_compression(leaf, fi) ||
7282 btrfs_file_extent_encryption(leaf, fi) ||
7283 btrfs_file_extent_other_encoding(leaf, fi))
7284 goto out;
7285
Chris Mason46bfbb52010-05-26 11:04:10 -04007286 backref_offset = btrfs_file_extent_offset(leaf, fi);
7287
Josef Bacik7ee9e442013-06-21 16:37:03 -04007288 if (orig_start) {
7289 *orig_start = key.offset - backref_offset;
7290 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7291 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7292 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007293
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007294 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007295 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007296
7297 num_bytes = min(offset + *len, extent_end) - offset;
7298 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7299 u64 range_end;
7300
Jeff Mahoneyda170662016-06-15 09:22:56 -04007301 range_end = round_up(offset + num_bytes,
7302 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007303 ret = test_range_bit(io_tree, offset, range_end,
7304 EXTENT_DELALLOC, 0, NULL);
7305 if (ret) {
7306 ret = -EAGAIN;
7307 goto out;
7308 }
7309 }
7310
Josef Bacik1bda19e2013-10-18 12:10:36 -04007311 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007312
7313 /*
7314 * look for other files referencing this extent, if we
7315 * find any we must cow
7316 */
Josef Bacik00361582013-08-14 14:02:47 -04007317
Liu Boe4c3b2d2017-01-30 12:25:28 -08007318 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007319 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007320 if (ret) {
7321 ret = 0;
7322 goto out;
7323 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007324
7325 /*
7326 * adjust disk_bytenr and num_bytes to cover just the bytes
7327 * in this extent we are about to write. If there
7328 * are any csums in that range we have to cow in order
7329 * to keep the csums correct
7330 */
7331 disk_bytenr += backref_offset;
7332 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007333 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7334 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007335 /*
7336 * all of the above have passed, it is safe to overwrite this extent
7337 * without cow
7338 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007339 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007340 ret = 1;
7341out:
7342 btrfs_free_path(path);
7343 return ret;
7344}
7345
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007346bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7347{
7348 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7349 int found = false;
7350 void **pagep = NULL;
7351 struct page *page = NULL;
7352 int start_idx;
7353 int end_idx;
7354
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007355 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007356
7357 /*
7358 * end is the last byte in the last page. end == start is legal
7359 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007360 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007361
7362 rcu_read_lock();
7363
7364 /* Most of the code in this while loop is lifted from
7365 * find_get_page. It's been modified to begin searching from a
7366 * page and return just the first page found in that range. If the
7367 * found idx is less than or equal to the end idx then we know that
7368 * a page exists. If no pages are found or if those pages are
7369 * outside of the range then we're fine (yay!) */
7370 while (page == NULL &&
7371 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7372 page = radix_tree_deref_slot(pagep);
7373 if (unlikely(!page))
7374 break;
7375
7376 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007377 if (radix_tree_deref_retry(page)) {
7378 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007379 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007380 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007381 /*
7382 * Otherwise, shmem/tmpfs must be storing a swap entry
7383 * here as an exceptional entry: so return it without
7384 * attempting to raise page count.
7385 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007386 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007387 break; /* TODO: Is this relevant for this use case? */
7388 }
7389
Filipe Manana91405152014-06-05 13:22:24 +01007390 if (!page_cache_get_speculative(page)) {
7391 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007392 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007393 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007394
7395 /*
7396 * Has the page moved?
7397 * This is part of the lockless pagecache protocol. See
7398 * include/linux/pagemap.h for details.
7399 */
7400 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007401 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007402 page = NULL;
7403 }
7404 }
7405
7406 if (page) {
7407 if (page->index <= end_idx)
7408 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007409 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007410 }
7411
7412 rcu_read_unlock();
7413 return found;
7414}
7415
Josef Bacikeb838e72012-07-31 16:28:48 -04007416static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7417 struct extent_state **cached_state, int writing)
7418{
7419 struct btrfs_ordered_extent *ordered;
7420 int ret = 0;
7421
7422 while (1) {
7423 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007424 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007425 /*
7426 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007427 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007428 * extents in this range.
7429 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007430 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007431 lockend - lockstart + 1);
7432
7433 /*
7434 * We need to make sure there are no buffered pages in this
7435 * range either, we could have raced between the invalidate in
7436 * generic_file_direct_write and locking the extent. The
7437 * invalidate needs to happen so that reads after a write do not
7438 * get stale data.
7439 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007440 if (!ordered &&
7441 (!writing ||
7442 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007443 break;
7444
7445 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7446 cached_state, GFP_NOFS);
7447
7448 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007449 /*
7450 * If we are doing a DIO read and the ordered extent we
7451 * found is for a buffered write, we can not wait for it
7452 * to complete and retry, because if we do so we can
7453 * deadlock with concurrent buffered writes on page
7454 * locks. This happens only if our DIO read covers more
7455 * than one extent map, if at this point has already
7456 * created an ordered extent for a previous extent map
7457 * and locked its range in the inode's io tree, and a
7458 * concurrent write against that previous extent map's
7459 * range and this range started (we unlock the ranges
7460 * in the io tree only when the bios complete and
7461 * buffered writes always lock pages before attempting
7462 * to lock range in the io tree).
7463 */
7464 if (writing ||
7465 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7466 btrfs_start_ordered_extent(inode, ordered, 1);
7467 else
7468 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007469 btrfs_put_ordered_extent(ordered);
7470 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007471 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007472 * We could trigger writeback for this range (and wait
7473 * for it to complete) and then invalidate the pages for
7474 * this range (through invalidate_inode_pages2_range()),
7475 * but that can lead us to a deadlock with a concurrent
7476 * call to readpages() (a buffered read or a defrag call
7477 * triggered a readahead) on a page lock due to an
7478 * ordered dio extent we created before but did not have
7479 * yet a corresponding bio submitted (whence it can not
7480 * complete), which makes readpages() wait for that
7481 * ordered extent to complete while holding a lock on
7482 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007483 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007484 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007485 }
7486
Filipe Mananaade77022016-02-18 14:28:55 +00007487 if (ret)
7488 break;
7489
Josef Bacikeb838e72012-07-31 16:28:48 -04007490 cond_resched();
7491 }
7492
7493 return ret;
7494}
7495
Liu Bo6f9994d2017-01-31 07:50:22 -08007496/* The callers of this must take lock_extent() */
7497static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7498 u64 orig_start, u64 block_start,
7499 u64 block_len, u64 orig_block_len,
7500 u64 ram_bytes, int compress_type,
7501 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007502{
7503 struct extent_map_tree *em_tree;
7504 struct extent_map *em;
7505 struct btrfs_root *root = BTRFS_I(inode)->root;
7506 int ret;
7507
Liu Bo6f9994d2017-01-31 07:50:22 -08007508 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7509 type == BTRFS_ORDERED_COMPRESSED ||
7510 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007511 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007512
Josef Bacik69ffb542012-09-11 15:40:07 -04007513 em_tree = &BTRFS_I(inode)->extent_tree;
7514 em = alloc_extent_map();
7515 if (!em)
7516 return ERR_PTR(-ENOMEM);
7517
7518 em->start = start;
7519 em->orig_start = orig_start;
7520 em->len = len;
7521 em->block_len = block_len;
7522 em->block_start = block_start;
7523 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007524 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007525 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007526 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007527 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007528 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007529 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007530 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007531 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7532 em->compress_type = compress_type;
7533 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007534
7535 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007536 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007537 em->start + em->len - 1, 0);
7538 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007539 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007540 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007541 /*
7542 * The caller has taken lock_extent(), who could race with us
7543 * to add em?
7544 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007545 } while (ret == -EEXIST);
7546
7547 if (ret) {
7548 free_extent_map(em);
7549 return ERR_PTR(ret);
7550 }
7551
Liu Bo6f9994d2017-01-31 07:50:22 -08007552 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007553 return em;
7554}
7555
Filipe Manana9c9464c2015-11-04 09:52:04 +00007556static void adjust_dio_outstanding_extents(struct inode *inode,
7557 struct btrfs_dio_data *dio_data,
7558 const u64 len)
7559{
David Sterba823bb202017-01-04 11:09:51 +01007560 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007561
Filipe Manana9c9464c2015-11-04 09:52:04 +00007562 /*
7563 * If we have an outstanding_extents count still set then we're
7564 * within our reservation, otherwise we need to adjust our inode
7565 * counter appropriately.
7566 */
Liu Boc2931662016-12-22 17:13:54 -08007567 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007568 dio_data->outstanding_extents -= num_extents;
7569 } else {
Liu Boc2931662016-12-22 17:13:54 -08007570 /*
7571 * If dio write length has been split due to no large enough
7572 * contiguous space, we need to compensate our inode counter
7573 * appropriately.
7574 */
7575 u64 num_needed = num_extents - dio_data->outstanding_extents;
7576
Filipe Manana9c9464c2015-11-04 09:52:04 +00007577 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007578 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007579 spin_unlock(&BTRFS_I(inode)->lock);
7580 }
7581}
7582
Josef Bacik4b46fce2010-05-23 11:00:55 -04007583static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7584 struct buffer_head *bh_result, int create)
7585{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007586 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007587 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007588 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307589 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007590 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007591 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007592 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007593 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007594 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007595
Miao Xie172a5042013-02-21 02:48:22 -07007596 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007597 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007598 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007599 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007600
Josef Bacikc3298612012-08-03 16:49:19 -04007601 lockstart = start;
7602 lockend = start + len - 1;
7603
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007604 if (current->journal_info) {
7605 /*
7606 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007607 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007608 * confused.
7609 */
chandan50745b02015-08-28 21:10:13 +05307610 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007611 current->journal_info = NULL;
7612 }
7613
Josef Bacikeb838e72012-07-31 16:28:48 -04007614 /*
7615 * If this errors out it's because we couldn't invalidate pagecache for
7616 * this range and we need to fallback to buffered.
7617 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007618 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7619 create)) {
7620 ret = -ENOTBLK;
7621 goto err;
7622 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007623
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007624 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007625 if (IS_ERR(em)) {
7626 ret = PTR_ERR(em);
7627 goto unlock_err;
7628 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007629
7630 /*
7631 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7632 * io. INLINE is special, and we could probably kludge it in here, but
7633 * it's still buffered so for safety lets just fall back to the generic
7634 * buffered path.
7635 *
7636 * For COMPRESSED we _have_ to read the entire extent in so we can
7637 * decompress it, so there will be buffering required no matter what we
7638 * do, so go ahead and fallback to buffered.
7639 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007640 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007641 * to buffered IO. Don't blame me, this is the price we pay for using
7642 * the generic code.
7643 */
7644 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7645 em->block_start == EXTENT_MAP_INLINE) {
7646 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007647 ret = -ENOTBLK;
7648 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007649 }
7650
7651 /* Just a good old fashioned hole, return */
7652 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7653 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7654 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007655 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007656 }
7657
7658 /*
7659 * We don't allocate a new extent in the following cases
7660 *
7661 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7662 * existing extent.
7663 * 2) The extent is marked as PREALLOC. We're good to go here and can
7664 * just use the extent.
7665 *
7666 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007667 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007668 len = min(len, em->len - (start - em->start));
7669 lockstart = start + len;
7670 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007671 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007672
7673 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7674 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7675 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007676 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007677 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007678
7679 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7680 type = BTRFS_ORDERED_PREALLOC;
7681 else
7682 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007683 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007684 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007685
Josef Bacik00361582013-08-14 14:02:47 -04007686 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007687 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007688 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007689 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007690
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007691 em2 = btrfs_create_dio_extent(inode, start, len,
7692 orig_start, block_start,
7693 len, orig_block_len,
7694 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007695 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007696 if (type == BTRFS_ORDERED_PREALLOC) {
7697 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007698 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007699 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007700 if (em2 && IS_ERR(em2)) {
7701 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007702 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007703 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007704 /*
7705 * For inode marked NODATACOW or extent marked PREALLOC,
7706 * use the existing or preallocated extent, so does not
7707 * need to adjust btrfs_space_info's bytes_may_use.
7708 */
7709 btrfs_free_reserved_data_space_noquota(inode,
7710 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007711 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007712 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007713 }
Josef Bacik00361582013-08-14 14:02:47 -04007714
Chris Mason46bfbb52010-05-26 11:04:10 -04007715 /*
7716 * this will cow the extent, reset the len in case we changed
7717 * it above
7718 */
7719 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007720 free_extent_map(em);
7721 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007722 if (IS_ERR(em)) {
7723 ret = PTR_ERR(em);
7724 goto unlock_err;
7725 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007726 len = min(len, em->len - (start - em->start));
7727unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007728 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7729 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007730 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007731 bh_result->b_bdev = em->bdev;
7732 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007733 if (create) {
7734 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7735 set_buffer_new(bh_result);
7736
7737 /*
7738 * Need to update the i_size under the extent lock so buffered
7739 * readers will get the updated i_size when we unlock.
7740 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007741 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007742 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007743
Filipe Manana9c9464c2015-11-04 09:52:04 +00007744 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307745 WARN_ON(dio_data->reserve < len);
7746 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007747 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307748 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007749 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007750
Josef Bacikeb838e72012-07-31 16:28:48 -04007751 /*
7752 * In the case of write we need to clear and unlock the entire range,
7753 * in the case of read we need to unlock only the end area that we
7754 * aren't using if there is any left over space.
7755 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007756 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007757 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7758 lockend, unlock_bits, 1, 0,
7759 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007760 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007761 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007762 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007763
Josef Bacik4b46fce2010-05-23 11:00:55 -04007764 free_extent_map(em);
7765
7766 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007767
7768unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007769 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7770 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007771err:
chandan50745b02015-08-28 21:10:13 +05307772 if (dio_data)
7773 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007774 /*
7775 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7776 * write less data then expected, so that we don't underflow our inode's
7777 * outstanding extents counter.
7778 */
7779 if (create && dio_data)
7780 adjust_dio_outstanding_extents(inode, dio_data, len);
7781
Josef Bacikeb838e72012-07-31 16:28:48 -04007782 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007783}
7784
Miao Xie8b110e32014-09-12 18:44:03 +08007785static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007786 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007787{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007788 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007789 int ret;
7790
Mike Christie37226b22016-06-05 14:31:52 -05007791 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007792
7793 bio_get(bio);
7794
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007795 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007796 if (ret)
7797 goto err;
7798
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007799 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007800err:
7801 bio_put(bio);
7802 return ret;
7803}
7804
7805static int btrfs_check_dio_repairable(struct inode *inode,
7806 struct bio *failed_bio,
7807 struct io_failure_record *failrec,
7808 int failed_mirror)
7809{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007810 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007811 int num_copies;
7812
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007813 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007814 if (num_copies == 1) {
7815 /*
7816 * we only have a single copy of the data, so don't bother with
7817 * all the retry and error correction code that follows. no
7818 * matter what the error is, it is very likely to persist.
7819 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007820 btrfs_debug(fs_info,
7821 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7822 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007823 return 0;
7824 }
7825
7826 failrec->failed_mirror = failed_mirror;
7827 failrec->this_mirror++;
7828 if (failrec->this_mirror == failed_mirror)
7829 failrec->this_mirror++;
7830
7831 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007832 btrfs_debug(fs_info,
7833 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7834 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007835 return 0;
7836 }
7837
7838 return 1;
7839}
7840
7841static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307842 struct page *page, unsigned int pgoff,
7843 u64 start, u64 end, int failed_mirror,
7844 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007845{
7846 struct io_failure_record *failrec;
7847 struct bio *bio;
7848 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007849 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007850 int ret;
7851
Mike Christie37226b22016-06-05 14:31:52 -05007852 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007853
7854 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7855 if (ret)
7856 return ret;
7857
7858 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7859 failed_mirror);
7860 if (!ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007861 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007862 return -EIO;
7863 }
7864
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307865 if ((failed_bio->bi_vcnt > 1)
7866 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007867 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007868 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007869
7870 isector = start - btrfs_io_bio(failed_bio)->logical;
7871 isector >>= inode->i_sb->s_blocksize_bits;
7872 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307873 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007874 if (!bio) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007875 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007876 return -EIO;
7877 }
Mike Christie37226b22016-06-05 14:31:52 -05007878 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007879
7880 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7881 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7882 read_mode, failrec->this_mirror, failrec->in_validation);
7883
Mike Christie81a75f672016-06-05 14:31:54 -05007884 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007885 if (ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007886 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007887 bio_put(bio);
7888 }
7889
7890 return ret;
7891}
7892
7893struct btrfs_retry_complete {
7894 struct completion done;
7895 struct inode *inode;
7896 u64 start;
7897 int uptodate;
7898};
7899
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007900static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007901{
7902 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307903 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007904 struct bio_vec *bvec;
7905 int i;
7906
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007907 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007908 goto end;
7909
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307910 ASSERT(bio->bi_vcnt == 1);
7911 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007912 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307913
Miao Xie8b110e32014-09-12 18:44:03 +08007914 done->uptodate = 1;
7915 bio_for_each_segment_all(bvec, bio, i)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02007916 clean_io_failure(BTRFS_I(done->inode), done->start, bvec->bv_page, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007917end:
7918 complete(&done->done);
7919 bio_put(bio);
7920}
7921
7922static int __btrfs_correct_data_nocsum(struct inode *inode,
7923 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007924{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307925 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007926 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007927 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007928 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307929 unsigned int pgoff;
7930 u32 sectorsize;
7931 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007932 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007933 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007934
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307935 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007936 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307937
Miao Xiec1dc0892014-09-12 18:43:56 +08007938 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007939 done.inode = inode;
7940
7941 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307942 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7943 pgoff = bvec->bv_offset;
7944
7945next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007946 done.uptodate = 0;
7947 done.start = start;
7948 init_completion(&done.done);
7949
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307950 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7951 pgoff, start, start + sectorsize - 1,
7952 io_bio->mirror_num,
7953 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007954 if (ret)
7955 return ret;
7956
7957 wait_for_completion(&done.done);
7958
7959 if (!done.uptodate) {
7960 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307961 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007962 }
7963
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307964 start += sectorsize;
7965
7966 if (nr_sectors--) {
7967 pgoff += sectorsize;
7968 goto next_block_or_try_again;
7969 }
Miao Xie8b110e32014-09-12 18:44:03 +08007970 }
7971
7972 return 0;
7973}
7974
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007975static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007976{
7977 struct btrfs_retry_complete *done = bio->bi_private;
7978 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307979 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007980 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307981 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08007982 int uptodate;
7983 int ret;
7984 int i;
7985
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007986 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007987 goto end;
7988
7989 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307990
7991 start = done->start;
7992
7993 ASSERT(bio->bi_vcnt == 1);
7994 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007995 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307996
Miao Xie8b110e32014-09-12 18:44:03 +08007997 bio_for_each_segment_all(bvec, bio, i) {
7998 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307999 bvec->bv_page, bvec->bv_offset,
8000 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008001 if (!ret)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02008002 clean_io_failure(BTRFS_I(done->inode), done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308003 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008004 else
8005 uptodate = 0;
8006 }
8007
8008 done->uptodate = uptodate;
8009end:
8010 complete(&done->done);
8011 bio_put(bio);
8012}
8013
8014static int __btrfs_subio_endio_read(struct inode *inode,
8015 struct btrfs_io_bio *io_bio, int err)
8016{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308017 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008018 struct bio_vec *bvec;
8019 struct btrfs_retry_complete done;
8020 u64 start;
8021 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308022 u32 sectorsize;
8023 int nr_sectors;
8024 unsigned int pgoff;
8025 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008026 int i;
8027 int ret;
8028
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308029 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008030 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308031
Miao Xie8b110e32014-09-12 18:44:03 +08008032 err = 0;
8033 start = io_bio->logical;
8034 done.inode = inode;
8035
Miao Xiec1dc0892014-09-12 18:43:56 +08008036 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308037 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8038
8039 pgoff = bvec->bv_offset;
8040next_block:
8041 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8042 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8043 bvec->bv_page, pgoff, start,
8044 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008045 if (likely(!ret))
8046 goto next;
8047try_again:
8048 done.uptodate = 0;
8049 done.start = start;
8050 init_completion(&done.done);
8051
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308052 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8053 pgoff, start, start + sectorsize - 1,
8054 io_bio->mirror_num,
8055 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008056 if (ret) {
8057 err = ret;
8058 goto next;
8059 }
8060
8061 wait_for_completion(&done.done);
8062
8063 if (!done.uptodate) {
8064 /* We might have another mirror, so try again */
8065 goto try_again;
8066 }
8067next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308068 offset += sectorsize;
8069 start += sectorsize;
8070
8071 ASSERT(nr_sectors);
8072
8073 if (--nr_sectors) {
8074 pgoff += sectorsize;
8075 goto next_block;
8076 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008077 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008078
8079 return err;
8080}
8081
Miao Xie8b110e32014-09-12 18:44:03 +08008082static int btrfs_subio_endio_read(struct inode *inode,
8083 struct btrfs_io_bio *io_bio, int err)
8084{
8085 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8086
8087 if (skip_csum) {
8088 if (unlikely(err))
8089 return __btrfs_correct_data_nocsum(inode, io_bio);
8090 else
8091 return 0;
8092 } else {
8093 return __btrfs_subio_endio_read(inode, io_bio, err);
8094 }
8095}
8096
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008097static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008098{
8099 struct btrfs_dio_private *dip = bio->bi_private;
8100 struct inode *inode = dip->inode;
8101 struct bio *dio_bio;
8102 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008103 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008104
Miao Xie8b110e32014-09-12 18:44:03 +08008105 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8106 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008107
Josef Bacik4b46fce2010-05-23 11:00:55 -04008108 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008109 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008110 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008111
Josef Bacik4b46fce2010-05-23 11:00:55 -04008112 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008113
Filipe Manana1636d1d2016-02-15 16:20:26 +00008114 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008115 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008116
8117 if (io_bio->end_io)
8118 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008119 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008120}
8121
Filipe Manana14543772015-11-24 16:23:54 +00008122static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8123 const u64 offset,
8124 const u64 bytes,
8125 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008126{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008127 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008128 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008129 u64 ordered_offset = offset;
8130 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008131 int ret;
8132
Chris Mason163cf092010-11-28 19:56:33 -05008133again:
8134 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8135 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008136 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008137 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008138 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008139 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008140
Liu Bo9e0af232014-08-15 23:36:53 +08008141 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8142 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008143 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008144out_test:
8145 /*
8146 * our bio might span multiple ordered extents. If we haven't
8147 * completed the accounting for the whole dio, go back and try again
8148 */
Filipe Manana14543772015-11-24 16:23:54 +00008149 if (ordered_offset < offset + bytes) {
8150 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008151 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008152 goto again;
8153 }
Filipe Manana14543772015-11-24 16:23:54 +00008154}
8155
8156static void btrfs_endio_direct_write(struct bio *bio)
8157{
8158 struct btrfs_dio_private *dip = bio->bi_private;
8159 struct bio *dio_bio = dip->dio_bio;
8160
8161 btrfs_endio_direct_write_update_ordered(dip->inode,
8162 dip->logical_offset,
8163 dip->bytes,
8164 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008165
Josef Bacik4b46fce2010-05-23 11:00:55 -04008166 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008167
Filipe Manana1636d1d2016-02-15 16:20:26 +00008168 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008169 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008170 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008171}
8172
Mike Christie81a75f672016-06-05 14:31:54 -05008173static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008174 struct bio *bio, int mirror_num,
8175 unsigned long bio_flags, u64 offset)
8176{
8177 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008178 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008179 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008180 return 0;
8181}
8182
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008183static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008184{
8185 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008186 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008187
Miao Xie8b110e32014-09-12 18:44:03 +08008188 if (err)
8189 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008190 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008191 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8192 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008193 (unsigned long long)bio->bi_iter.bi_sector,
8194 bio->bi_iter.bi_size, err);
8195
8196 if (dip->subio_endio)
8197 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008198
8199 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008200 dip->errors = 1;
8201
8202 /*
8203 * before atomic variable goto zero, we must make sure
8204 * dip->errors is perceived to be set.
8205 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008206 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008207 }
8208
8209 /* if there are more bios still pending for this dio, just exit */
8210 if (!atomic_dec_and_test(&dip->pending_bios))
8211 goto out;
8212
Chris Mason9be33952013-05-17 18:30:14 -04008213 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008214 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008215 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008216 dip->dio_bio->bi_error = 0;
8217 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008218 }
8219out:
8220 bio_put(bio);
8221}
8222
8223static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8224 u64 first_sector, gfp_t gfp_flags)
8225{
Chris Masonda2f0f72015-07-02 13:57:22 -07008226 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008227 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008228 if (bio)
8229 bio_associate_current(bio);
8230 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008231}
8232
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008233static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008234 struct btrfs_dio_private *dip,
8235 struct bio *bio,
8236 u64 file_offset)
8237{
8238 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8239 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8240 int ret;
8241
8242 /*
8243 * We load all the csum data we need when we submit
8244 * the first bio to reduce the csum tree search and
8245 * contention.
8246 */
8247 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008248 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008249 file_offset);
8250 if (ret)
8251 return ret;
8252 }
8253
8254 if (bio == dip->orig_bio)
8255 return 0;
8256
8257 file_offset -= dip->logical_offset;
8258 file_offset >>= inode->i_sb->s_blocksize_bits;
8259 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8260
8261 return 0;
8262}
8263
Miao Xiee65e1532010-11-22 03:04:43 +00008264static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008265 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008266 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008267{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008268 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008269 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008270 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008271 int ret;
8272
Josef Bacikb812ce22012-11-16 13:56:32 -05008273 if (async_submit)
8274 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8275
Miao Xiee65e1532010-11-22 03:04:43 +00008276 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008277
8278 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008279 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008280 if (ret)
8281 goto err;
8282 }
Miao Xiee65e1532010-11-22 03:04:43 +00008283
Josef Bacik1ae39932011-04-06 14:41:34 -04008284 if (skip_sum)
8285 goto map;
8286
8287 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008288 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8289 file_offset,
8290 __btrfs_submit_bio_start_direct_io,
8291 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008292 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008293 } else if (write) {
8294 /*
8295 * If we aren't doing async submit, calculate the csum of the
8296 * bio now.
8297 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008298 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008299 if (ret)
8300 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008301 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008302 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008303 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008304 if (ret)
8305 goto err;
8306 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008307map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008308 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008309err:
8310 bio_put(bio);
8311 return ret;
8312}
8313
Mike Christie81a75f672016-06-05 14:31:54 -05008314static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008315 int skip_sum)
8316{
8317 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008318 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008319 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008320 struct bio *bio;
8321 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008322 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008323 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008324 u64 file_offset = dip->logical_offset;
8325 u64 submit_len = 0;
8326 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008327 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008328 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308329 int nr_sectors;
8330 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008331 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008332
Kent Overstreet4f024f32013-10-11 15:44:27 -07008333 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008334 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8335 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008336 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008337 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008338
Kent Overstreet4f024f32013-10-11 15:44:27 -07008339 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008340 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008341 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008342 goto submit;
8343 }
8344
David Woodhouse53b381b2013-01-29 18:40:14 -05008345 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008346 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008347 async_submit = 0;
8348 else
8349 async_submit = 1;
8350
Josef Bacik02f57c72011-04-06 14:25:44 -04008351 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8352 if (!bio)
8353 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008354
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008355 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008356 bio->bi_private = dip;
8357 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008358 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008359 atomic_inc(&dip->pending_bios);
8360
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008361 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008362 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308363 i = 0;
8364next_block:
8365 if (unlikely(map_length < submit_len + blocksize ||
8366 bio_add_page(bio, bvec->bv_page, blocksize,
8367 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008368 /*
8369 * inc the count before we submit the bio so
8370 * we know the end IO handler won't happen before
8371 * we inc the count. Otherwise, the dip might get freed
8372 * before we're done setting it up
8373 */
8374 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008375 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008376 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008377 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008378 if (ret) {
8379 bio_put(bio);
8380 atomic_dec(&dip->pending_bios);
8381 goto out_err;
8382 }
8383
Miao Xiee65e1532010-11-22 03:04:43 +00008384 start_sector += submit_len >> 9;
8385 file_offset += submit_len;
8386
8387 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008388
8389 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8390 start_sector, GFP_NOFS);
8391 if (!bio)
8392 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008393 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008394 bio->bi_private = dip;
8395 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008396 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008397
Kent Overstreet4f024f32013-10-11 15:44:27 -07008398 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008399 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008400 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008401 &map_length, NULL, 0);
8402 if (ret) {
8403 bio_put(bio);
8404 goto out_err;
8405 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308406
8407 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008408 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308409 submit_len += blocksize;
8410 if (--nr_sectors) {
8411 i++;
8412 goto next_block;
8413 }
Miao Xiee65e1532010-11-22 03:04:43 +00008414 }
8415 }
8416
Josef Bacik02f57c72011-04-06 14:25:44 -04008417submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008418 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008419 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008420 if (!ret)
8421 return 0;
8422
8423 bio_put(bio);
8424out_err:
8425 dip->errors = 1;
8426 /*
8427 * before atomic variable goto zero, we must
8428 * make sure dip->errors is perceived to be set.
8429 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008430 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008431 if (atomic_dec_and_test(&dip->pending_bios))
8432 bio_io_error(dip->orig_bio);
8433
8434 /* bio_end_io() will handle error, so we needn't return it */
8435 return 0;
8436}
8437
Mike Christie8a4c1e42016-06-05 14:31:50 -05008438static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8439 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008440{
Filipe Manana61de7182015-07-01 12:13:10 +01008441 struct btrfs_dio_private *dip = NULL;
8442 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008443 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008444 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008445 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008446 int ret = 0;
8447
8448 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8449
Chris Mason9be33952013-05-17 18:30:14 -04008450 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008451 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008452 ret = -ENOMEM;
8453 goto free_ordered;
8454 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008455
Miao Xiec1dc0892014-09-12 18:43:56 +08008456 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008457 if (!dip) {
8458 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008459 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008460 }
8461
8462 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008463 dip->inode = inode;
8464 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008465 dip->bytes = dio_bio->bi_iter.bi_size;
8466 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008467 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008468 dip->orig_bio = io_bio;
8469 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008470 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008471 btrfs_bio = btrfs_io_bio(io_bio);
8472 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008473
Miao Xiec1dc0892014-09-12 18:43:56 +08008474 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008475 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008476 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008477 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008478 dip->subio_endio = btrfs_subio_endio_read;
8479 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008480
Filipe Mananaf28a4922015-12-08 19:23:20 +00008481 /*
8482 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8483 * even if we fail to submit a bio, because in such case we do the
8484 * corresponding error handling below and it must not be done a second
8485 * time by btrfs_direct_IO().
8486 */
8487 if (write) {
8488 struct btrfs_dio_data *dio_data = current->journal_info;
8489
8490 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8491 dip->bytes;
8492 dio_data->unsubmitted_oe_range_start =
8493 dio_data->unsubmitted_oe_range_end;
8494 }
8495
Mike Christie81a75f672016-06-05 14:31:54 -05008496 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008497 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008498 return;
Chris Mason9be33952013-05-17 18:30:14 -04008499
Miao Xie23ea8e52014-09-12 18:43:54 +08008500 if (btrfs_bio->end_io)
8501 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008502
Josef Bacik4b46fce2010-05-23 11:00:55 -04008503free_ordered:
8504 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008505 * If we arrived here it means either we failed to submit the dip
8506 * or we either failed to clone the dio_bio or failed to allocate the
8507 * dip. If we cloned the dio_bio and allocated the dip, we can just
8508 * call bio_endio against our io_bio so that we get proper resource
8509 * cleanup if we fail to submit the dip, otherwise, we must do the
8510 * same as btrfs_endio_direct_[write|read] because we can't call these
8511 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008512 */
Filipe Manana61de7182015-07-01 12:13:10 +01008513 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008514 io_bio->bi_error = -EIO;
8515 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008516 /*
8517 * The end io callbacks free our dip, do the final put on io_bio
8518 * and all the cleanup and final put for dio_bio (through
8519 * dio_end_io()).
8520 */
8521 dip = NULL;
8522 io_bio = NULL;
8523 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008524 if (write)
8525 btrfs_endio_direct_write_update_ordered(inode,
8526 file_offset,
8527 dio_bio->bi_iter.bi_size,
8528 0);
8529 else
Filipe Manana61de7182015-07-01 12:13:10 +01008530 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8531 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008532
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008533 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008534 /*
8535 * Releases and cleans up our dio_bio, no need to bio_put()
8536 * nor bio_endio()/bio_io_error() against dio_bio.
8537 */
8538 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008539 }
Filipe Manana61de7182015-07-01 12:13:10 +01008540 if (io_bio)
8541 bio_put(io_bio);
8542 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008543}
8544
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008545static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8546 struct kiocb *iocb,
8547 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008548{
8549 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008550 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008551 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008552 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008553
8554 if (offset & blocksize_mask)
8555 goto out;
8556
Al Viro28060d52014-03-22 05:15:17 -04008557 if (iov_iter_alignment(iter) & blocksize_mask)
8558 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008559
Al Viro28060d52014-03-22 05:15:17 -04008560 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008561 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008562 return 0;
8563 /*
8564 * Check to make sure we don't have duplicate iov_base's in this
8565 * iovec, if so return EINVAL, otherwise we'll get csum errors
8566 * when reading back.
8567 */
8568 for (seg = 0; seg < iter->nr_segs; seg++) {
8569 for (i = seg + 1; i < iter->nr_segs; i++) {
8570 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008571 goto out;
8572 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008573 }
8574 retval = 0;
8575out:
8576 return retval;
8577}
Josef Bacikeb838e72012-07-31 16:28:48 -04008578
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008579static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008580{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008581 struct file *file = iocb->ki_filp;
8582 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008583 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308584 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008585 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008586 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008587 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008588 bool wakeup = true;
8589 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008590 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008591
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008592 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008593 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008594
Jens Axboefe0f07d2015-04-15 17:05:48 -06008595 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008596 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008597
Josef Bacik0e267c42013-07-02 10:38:02 -04008598 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008599 * The generic stuff only does filemap_write_and_wait_range, which
8600 * isn't enough if we've written compressed pages to this area, so
8601 * we need to flush the dirty pages again to make absolutely sure
8602 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008603 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008604 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008605 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8606 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008607 filemap_fdatawrite_range(inode->i_mapping, offset,
8608 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008609
Omar Sandoval6f673762015-03-16 04:33:52 -07008610 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008611 /*
8612 * If the write DIO is beyond the EOF, we need update
8613 * the isize, but it is protected by i_mutex. So we can
8614 * not unlock the i_mutex at this case.
8615 */
8616 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008617 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008618 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008619 relock = true;
8620 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008621 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008622 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008623 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008624 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008625
8626 /*
8627 * We need to know how many extents we reserved so that we can
8628 * do the accounting properly if we go over the number we
8629 * originally calculated. Abuse current->journal_info for this.
8630 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008631 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008632 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008633 dio_data.unsubmitted_oe_range_start = (u64)offset;
8634 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308635 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308636 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008637 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8638 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008639 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008640 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8641 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008642 }
8643
Omar Sandoval17f8c842015-03-16 04:33:50 -07008644 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008645 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008646 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008647 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008648 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308649 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008650 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008651 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308652 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008653 btrfs_delalloc_release_space(inode, offset,
8654 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008655 /*
8656 * On error we might have left some ordered extents
8657 * without submitting corresponding bios for them, so
8658 * cleanup them up to avoid other tasks getting them
8659 * and waiting for them to complete forever.
8660 */
8661 if (dio_data.unsubmitted_oe_range_start <
8662 dio_data.unsubmitted_oe_range_end)
8663 btrfs_endio_direct_write_update_ordered(inode,
8664 dio_data.unsubmitted_oe_range_start,
8665 dio_data.unsubmitted_oe_range_end -
8666 dio_data.unsubmitted_oe_range_start,
8667 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008668 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008669 btrfs_delalloc_release_space(inode, offset,
8670 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008671 }
Miao Xie38851cc2013-02-08 07:04:11 +00008672out:
Miao Xie2e60a512013-02-08 07:01:08 +00008673 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008674 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008675 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008676 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008677
8678 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008679}
8680
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008681#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8682
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008683static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8684 __u64 start, __u64 len)
8685{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008686 int ret;
8687
8688 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8689 if (ret)
8690 return ret;
8691
Chris Masonec29ed52011-02-23 16:23:20 -05008692 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008693}
8694
Chris Mason9ebefb182007-06-15 13:50:00 -04008695int btrfs_readpage(struct file *file, struct page *page)
8696{
Chris Masond1310b22008-01-24 16:13:08 -05008697 struct extent_io_tree *tree;
8698 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008699 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008700}
Chris Mason1832a6d2007-12-21 16:27:21 -05008701
Chris Mason39279cc2007-06-12 06:35:45 -04008702static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8703{
Chris Masond1310b22008-01-24 16:13:08 -05008704 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008705 struct inode *inode = page->mapping->host;
8706 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008707
8708 if (current->flags & PF_MEMALLOC) {
8709 redirty_page_for_writepage(wbc, page);
8710 unlock_page(page);
8711 return 0;
8712 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008713
8714 /*
8715 * If we are under memory pressure we will call this directly from the
8716 * VM, we need to make sure we have the inode referenced for the ordered
8717 * extent. If not just return like we didn't do anything.
8718 */
8719 if (!igrab(inode)) {
8720 redirty_page_for_writepage(wbc, page);
8721 return AOP_WRITEPAGE_ACTIVATE;
8722 }
Chris Masond1310b22008-01-24 16:13:08 -05008723 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008724 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8725 btrfs_add_delayed_iput(inode);
8726 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008727}
Chris Mason39279cc2007-06-12 06:35:45 -04008728
Eric Sandeen48a3b632013-04-25 20:41:01 +00008729static int btrfs_writepages(struct address_space *mapping,
8730 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008731{
Chris Masond1310b22008-01-24 16:13:08 -05008732 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008733
Chris Masond1310b22008-01-24 16:13:08 -05008734 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008735 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8736}
8737
Chris Mason3ab2fb52007-11-08 10:59:22 -05008738static int
8739btrfs_readpages(struct file *file, struct address_space *mapping,
8740 struct list_head *pages, unsigned nr_pages)
8741{
Chris Masond1310b22008-01-24 16:13:08 -05008742 struct extent_io_tree *tree;
8743 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008744 return extent_readpages(tree, mapping, pages, nr_pages,
8745 btrfs_get_extent);
8746}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008747static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008748{
Chris Masond1310b22008-01-24 16:13:08 -05008749 struct extent_io_tree *tree;
8750 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008751 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008752
Chris Masond1310b22008-01-24 16:13:08 -05008753 tree = &BTRFS_I(page->mapping->host)->io_tree;
8754 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008755 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008756 if (ret == 1) {
8757 ClearPagePrivate(page);
8758 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008759 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008760 }
8761 return ret;
8762}
Chris Mason39279cc2007-06-12 06:35:45 -04008763
Chris Masone6dcd2d2008-07-17 12:53:50 -04008764static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8765{
Chris Mason98509cf2008-09-11 15:51:43 -04008766 if (PageWriteback(page) || PageDirty(page))
8767 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008768 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008769}
8770
Lukas Czernerd47992f2013-05-21 23:17:23 -04008771static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8772 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008773{
Josef Bacik5fd02042012-05-02 14:00:54 -04008774 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008775 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008776 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008777 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008778 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008779 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308780 u64 start;
8781 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008782 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008783
Chris Mason8b62b722009-09-02 16:53:46 -04008784 /*
8785 * we have the page locked, so new writeback can't start,
8786 * and the dirty bit won't be cleared while we are here.
8787 *
8788 * Wait for IO on this page so that we can safely clear
8789 * the PagePrivate2 bit and do ordered accounting
8790 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008791 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008792
Josef Bacik5fd02042012-05-02 14:00:54 -04008793 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008794 if (offset) {
8795 btrfs_releasepage(page, GFP_NOFS);
8796 return;
8797 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008798
8799 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008800 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308801again:
8802 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008803 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308804 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008805 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308806 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008807 /*
8808 * IO on this page will never be started, so we need
8809 * to account for any ordered extents now
8810 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008811 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308812 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008813 EXTENT_DIRTY | EXTENT_DELALLOC |
8814 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8815 EXTENT_DEFRAG, 1, 0, &cached_state,
8816 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008817 /*
8818 * whoever cleared the private bit is responsible
8819 * for the finish_ordered_io
8820 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008821 if (TestClearPagePrivate2(page)) {
8822 struct btrfs_ordered_inode_tree *tree;
8823 u64 new_len;
8824
8825 tree = &BTRFS_I(inode)->ordered_tree;
8826
8827 spin_lock_irq(&tree->lock);
8828 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308829 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008830 if (new_len < ordered->truncated_len)
8831 ordered->truncated_len = new_len;
8832 spin_unlock_irq(&tree->lock);
8833
8834 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308835 start,
8836 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008837 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008838 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008839 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008840 if (!inode_evicting) {
8841 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308842 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008843 &cached_state);
8844 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308845
8846 start = end + 1;
8847 if (start < page_end)
8848 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008849 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008850
Qu Wenruob9d0b382015-09-29 10:35:16 +08008851 /*
8852 * Qgroup reserved space handler
8853 * Page here will be either
8854 * 1) Already written to disk
8855 * In this case, its reserved space is released from data rsv map
8856 * and will be freed by delayed_ref handler finally.
8857 * So even we call qgroup_free_data(), it won't decrease reserved
8858 * space.
8859 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008860 * This means the reserved space should be freed here. However,
8861 * if a truncate invalidates the page (by clearing PageDirty)
8862 * and the page is accounted for while allocating extent
8863 * in btrfs_check_data_free_space() we let delayed_ref to
8864 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008865 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008866 if (PageDirty(page))
8867 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008868 if (!inode_evicting) {
8869 clear_extent_bit(tree, page_start, page_end,
8870 EXTENT_LOCKED | EXTENT_DIRTY |
8871 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8872 EXTENT_DEFRAG, 1, 1,
8873 &cached_state, GFP_NOFS);
8874
8875 __btrfs_releasepage(page, GFP_NOFS);
8876 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008877
Chris Mason4a096752008-07-21 10:29:44 -04008878 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008879 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008880 ClearPagePrivate(page);
8881 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008882 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008883 }
Chris Mason39279cc2007-06-12 06:35:45 -04008884}
8885
Chris Mason9ebefb182007-06-15 13:50:00 -04008886/*
8887 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8888 * called from a page fault handler when a page is first dirtied. Hence we must
8889 * be careful to check for EOF conditions here. We set the page up correctly
8890 * for a written page which means we get ENOSPC checking when writing into
8891 * holes and correct delalloc and unwritten extent mapping on filesystems that
8892 * support these features.
8893 *
8894 * We are not allowed to take the i_mutex here so we have to play games to
8895 * protect against truncate races as the page could now be beyond EOF. Because
8896 * vmtruncate() writes the inode size before removing pages, once we have the
8897 * page lock we can determine safely if the page is beyond EOF. If it is not
8898 * beyond EOF, then the page is guaranteed safe against truncation until we
8899 * unlock the page.
8900 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008901int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008902{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008903 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008904 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008905 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008906 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8907 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008908 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008909 char *kaddr;
8910 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008911 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008912 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008913 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308914 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008915 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008916 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308917 u64 end;
8918
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008919 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008920
Jan Karab2b5ef52012-06-12 16:20:45 +02008921 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008922 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008923 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308924 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008925
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308926 /*
8927 * Reserving delalloc space after obtaining the page lock can lead to
8928 * deadlock. For example, if a dirty page is locked by this function
8929 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8930 * dirty page write out, then the btrfs_writepage() function could
8931 * end up waiting indefinitely to get a lock on the page currently
8932 * being processed by btrfs_page_mkwrite() function.
8933 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008934 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308935 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008936 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008937 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008938 reserved = 1;
8939 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008940 if (ret) {
8941 if (ret == -ENOMEM)
8942 ret = VM_FAULT_OOM;
8943 else /* -ENOSPC, -EIO, etc */
8944 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008945 if (reserved)
8946 goto out;
8947 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008948 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008949
Nick Piggin56a76f82009-03-31 15:23:23 -07008950 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008951again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008952 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008953 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008954
Chris Mason9ebefb182007-06-15 13:50:00 -04008955 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008956 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008957 /* page got truncated out from underneath us */
8958 goto out_unlock;
8959 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008960 wait_on_page_writeback(page);
8961
David Sterbaff13db42015-12-03 14:30:40 +01008962 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008963 set_page_extent_mapped(page);
8964
Chris Masoneb84ae02008-07-17 13:53:27 -04008965 /*
8966 * we can't set the delalloc bits if there are pending ordered
8967 * extents. Drop our locks and wait for them to finish
8968 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008969 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8970 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008971 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008972 unlock_extent_cached(io_tree, page_start, page_end,
8973 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008974 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008975 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008976 btrfs_put_ordered_extent(ordered);
8977 goto again;
8978 }
8979
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008980 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008981 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008982 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008983 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308984 end = page_start + reserved_space - 1;
8985 spin_lock(&BTRFS_I(inode)->lock);
8986 BTRFS_I(inode)->outstanding_extents++;
8987 spin_unlock(&BTRFS_I(inode)->lock);
8988 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008989 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308990 }
8991 }
8992
Josef Bacikfbf19082009-10-01 17:10:23 -04008993 /*
Liu Bo54160342016-12-13 12:15:19 -08008994 * page_mkwrite gets called when the page is firstly dirtied after it's
8995 * faulted in, but write(2) could also dirty a page and set delalloc
8996 * bits, thus in this case for space account reason, we still need to
8997 * clear any delalloc bits within this page range since we have to
8998 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008999 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309000 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009001 EXTENT_DIRTY | EXTENT_DELALLOC |
9002 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009003 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009004
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309005 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009006 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009007 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009008 unlock_extent_cached(io_tree, page_start, page_end,
9009 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009010 ret = VM_FAULT_SIGBUS;
9011 goto out_unlock;
9012 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009013 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009014
9015 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009016 if (page_start + PAGE_SIZE > size)
9017 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009018 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009019 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009020
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009021 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009022 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009023 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009024 flush_dcache_page(page);
9025 kunmap(page);
9026 }
Chris Mason247e7432008-07-17 12:53:51 -04009027 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009028 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009029 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009030
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009031 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009032 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009033 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009034
Josef Bacik2ac55d42010-02-03 19:33:23 +00009035 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009036
9037out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009038 if (!ret) {
9039 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009040 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009041 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009042 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009043out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309044 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009045out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009046 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009047 return ret;
9048}
9049
Josef Bacika41ad392011-01-31 15:30:16 -05009050static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009051{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009052 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009053 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009054 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009055 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009056 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009057 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009058 u64 mask = fs_info->sectorsize - 1;
9059 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009060
Josef Bacik0ef8b722013-10-25 16:13:35 -04009061 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9062 (u64)-1);
9063 if (ret)
9064 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009065
Josef Bacikfcb80c22011-05-03 10:40:22 -04009066 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009067 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009068 * 3 things going on here
9069 *
9070 * 1) We need to reserve space for our orphan item and the space to
9071 * delete our orphan item. Lord knows we don't want to have a dangling
9072 * orphan item because we didn't reserve space to remove it.
9073 *
9074 * 2) We need to reserve space to update our inode.
9075 *
9076 * 3) We need to have something to cache all the space that is going to
9077 * be free'd up by the truncate operation, but also have some slack
9078 * space reserved in case it uses space during the truncate (thank you
9079 * very much snapshotting).
9080 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009081 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009082 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009083 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009084 * doesn't end up using space reserved for updating the inode or
9085 * removing the orphan item. We also need to be able to stop the
9086 * transaction and start a new one, which means we need to be able to
9087 * update the inode several times, and we have no idea of knowing how
9088 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009089 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009090 * Then there is the orphan item, which does indeed need to be held on
9091 * to for the whole operation, and we need nobody to touch this reserved
9092 * space except the orphan code.
9093 *
9094 * So that leaves us with
9095 *
9096 * 1) root->orphan_block_rsv - for the orphan deletion.
9097 * 2) rsv - for the truncate reservation, which we will steal from the
9098 * transaction reservation.
9099 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9100 * updating the inode.
9101 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009102 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009103 if (!rsv)
9104 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009105 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009106 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009107
Josef Bacik907cbce2011-08-08 13:46:15 -04009108 /*
Josef Bacik07127182011-08-19 10:29:59 -04009109 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009110 * 1 for updating the inode.
9111 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009112 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009113 if (IS_ERR(trans)) {
9114 err = PTR_ERR(trans);
9115 goto out;
9116 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009117
Josef Bacik907cbce2011-08-08 13:46:15 -04009118 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009119 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009120 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009121 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009122
Chris Mason5a3f23d2009-03-31 13:27:11 -04009123 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009124 * So if we truncate and then write and fsync we normally would just
9125 * write the extents that changed, which is a problem if we need to
9126 * first truncate that entire inode. So set this flag so we write out
9127 * all of the extents in the inode to the sync log so we're completely
9128 * safe.
9129 */
9130 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009131 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009132
Yan, Zheng80825102009-11-12 09:35:36 +00009133 while (1) {
9134 ret = btrfs_truncate_inode_items(trans, root, inode,
9135 inode->i_size,
9136 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009137 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009138 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009139 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009140 }
Chris Mason39279cc2007-06-12 06:35:45 -04009141
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009142 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009143 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009144 if (ret) {
9145 err = ret;
9146 break;
9147 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009148
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009149 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009150 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009151
9152 trans = btrfs_start_transaction(root, 2);
9153 if (IS_ERR(trans)) {
9154 ret = err = PTR_ERR(trans);
9155 trans = NULL;
9156 break;
9157 }
9158
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009159 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009160 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009161 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009162 BUG_ON(ret); /* shouldn't happen */
9163 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009164 }
9165
9166 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009167 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009168 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009169 if (ret)
9170 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009171 }
9172
Chris Mason917c16b2011-11-08 14:49:59 -05009173 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009174 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009175 ret = btrfs_update_inode(trans, root, inode);
9176 if (ret && !err)
9177 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009178
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009179 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009180 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009181 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009182out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009183 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009184
Josef Bacik3893e332011-01-31 16:03:11 -05009185 if (ret && !err)
9186 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009187
Josef Bacik3893e332011-01-31 16:03:11 -05009188 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009189}
9190
Sven Wegener3b963622008-06-09 21:57:42 -04009191/*
Chris Masond352ac62008-09-29 15:18:18 -04009192 * create a new subvolume directory/inode (helper for the ioctl).
9193 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009194int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009195 struct btrfs_root *new_root,
9196 struct btrfs_root *parent_root,
9197 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009198{
Chris Mason39279cc2007-06-12 06:35:45 -04009199 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009200 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009201 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009202
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009203 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9204 new_dirid, new_dirid,
9205 S_IFDIR | (~current_umask() & S_IRWXUGO),
9206 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009207 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009208 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009209 inode->i_op = &btrfs_dir_inode_operations;
9210 inode->i_fop = &btrfs_dir_file_operations;
9211
Miklos Szeredibfe86842011-10-28 14:13:29 +02009212 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009213 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009214 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009215
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009216 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9217 if (err)
9218 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009219 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009220 new_root->root_key.objectid, err);
9221
Yan, Zheng76dda932009-09-21 16:00:26 -04009222 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009223
Yan, Zheng76dda932009-09-21 16:00:26 -04009224 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009225 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009226}
9227
Chris Mason39279cc2007-06-12 06:35:45 -04009228struct inode *btrfs_alloc_inode(struct super_block *sb)
9229{
9230 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009231 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009232
9233 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9234 if (!ei)
9235 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009236
9237 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009238 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009239 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009240 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009241 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009242 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009243 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009244 ei->disk_i_size = 0;
9245 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009246 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009247 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009248 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009249 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009250 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009251 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009252
Josef Bacik9e0baf62011-07-15 15:16:44 +00009253 spin_lock_init(&ei->lock);
9254 ei->outstanding_extents = 0;
9255 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009256
Josef Bacik72ac3c02012-05-23 14:13:11 -04009257 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009258 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009259
Miao Xie16cdcec2011-04-22 18:12:22 +08009260 ei->delayed_node = NULL;
9261
chandan r9cc97d62012-07-04 12:48:07 +05309262 ei->i_otime.tv_sec = 0;
9263 ei->i_otime.tv_nsec = 0;
9264
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009265 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009266 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009267 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9268 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009269 ei->io_tree.track_uptodate = 1;
9270 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009271 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009272 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009273 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009274 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009275 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009276 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009277 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009278 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009279
9280 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009281}
9282
Josef Bacikaaedb552013-10-11 14:44:09 -04009283#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9284void btrfs_test_destroy_inode(struct inode *inode)
9285{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009286 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009287 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9288}
9289#endif
9290
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009291static void btrfs_i_callback(struct rcu_head *head)
9292{
9293 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009294 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9295}
9296
Chris Mason39279cc2007-06-12 06:35:45 -04009297void btrfs_destroy_inode(struct inode *inode)
9298{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009299 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009300 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009301 struct btrfs_root *root = BTRFS_I(inode)->root;
9302
Al Virob3d9b7a2012-06-09 13:51:19 -04009303 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009304 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009305 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9306 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009307 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9308 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009309 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009310
Chris Mason5a3f23d2009-03-31 13:27:11 -04009311 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009312 * This can happen where we create an inode, but somebody else also
9313 * created the same inode and we need to destroy the one we already
9314 * created.
9315 */
9316 if (!root)
9317 goto free;
9318
Josef Bacik8a35d952012-05-23 14:26:42 -04009319 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9320 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009321 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009322 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009323 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009324 }
Josef Bacik7b128762008-07-24 12:17:14 -04009325
Chris Masond3977122009-01-05 21:25:51 -05009326 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009327 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9328 if (!ordered)
9329 break;
9330 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009331 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009332 "found ordered extent %llu %llu on inode cleanup",
9333 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009334 btrfs_remove_ordered_extent(inode, ordered);
9335 btrfs_put_ordered_extent(ordered);
9336 btrfs_put_ordered_extent(ordered);
9337 }
9338 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009339 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009340 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009341 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009342free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009343 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009344}
9345
Al Viro45321ac2010-06-07 13:43:19 -04009346int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009347{
9348 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009349
Naohiro Aota6379ef92013-06-06 09:56:34 +00009350 if (root == NULL)
9351 return 1;
9352
Liu Bofa6ac872013-02-20 14:10:23 +00009353 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009354 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009355 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009356 else
Al Viro45321ac2010-06-07 13:43:19 -04009357 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009358}
9359
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009360static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009361{
9362 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9363
9364 inode_init_once(&ei->vfs_inode);
9365}
9366
9367void btrfs_destroy_cachep(void)
9368{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009369 /*
9370 * Make sure all delayed rcu free inodes are flushed before we
9371 * destroy cache.
9372 */
9373 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009374 kmem_cache_destroy(btrfs_inode_cachep);
9375 kmem_cache_destroy(btrfs_trans_handle_cachep);
9376 kmem_cache_destroy(btrfs_transaction_cachep);
9377 kmem_cache_destroy(btrfs_path_cachep);
9378 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009379}
9380
9381int btrfs_init_cachep(void)
9382{
David Sterba837e1972012-09-07 03:00:48 -06009383 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009384 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009385 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9386 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009387 if (!btrfs_inode_cachep)
9388 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009389
David Sterba837e1972012-09-07 03:00:48 -06009390 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009391 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009392 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009393 if (!btrfs_trans_handle_cachep)
9394 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009395
David Sterba837e1972012-09-07 03:00:48 -06009396 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009397 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009398 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009399 if (!btrfs_transaction_cachep)
9400 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009401
David Sterba837e1972012-09-07 03:00:48 -06009402 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009403 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009404 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009405 if (!btrfs_path_cachep)
9406 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009407
David Sterba837e1972012-09-07 03:00:48 -06009408 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009409 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009410 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009411 if (!btrfs_free_space_cachep)
9412 goto fail;
9413
Chris Mason39279cc2007-06-12 06:35:45 -04009414 return 0;
9415fail:
9416 btrfs_destroy_cachep();
9417 return -ENOMEM;
9418}
9419
9420static int btrfs_getattr(struct vfsmount *mnt,
9421 struct dentry *dentry, struct kstat *stat)
9422{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009423 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009424 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009425 u32 blocksize = inode->i_sb->s_blocksize;
9426
Chris Mason39279cc2007-06-12 06:35:45 -04009427 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009428 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009429
9430 spin_lock(&BTRFS_I(inode)->lock);
9431 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9432 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009433 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009434 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009435 return 0;
9436}
9437
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009438static int btrfs_rename_exchange(struct inode *old_dir,
9439 struct dentry *old_dentry,
9440 struct inode *new_dir,
9441 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009442{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009443 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009444 struct btrfs_trans_handle *trans;
9445 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009446 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009447 struct inode *new_inode = new_dentry->d_inode;
9448 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009449 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009450 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009451 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9452 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009453 u64 old_idx = 0;
9454 u64 new_idx = 0;
9455 u64 root_objectid;
9456 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009457 bool root_log_pinned = false;
9458 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009459
9460 /* we only allow rename subvolume link between subvolumes */
9461 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9462 return -EXDEV;
9463
9464 /* close the race window with snapshot create/destroy ioctl */
9465 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009466 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009467 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009468 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009469
9470 /*
9471 * We want to reserve the absolute worst case amount of items. So if
9472 * both inodes are subvols and we need to unlink them then that would
9473 * require 4 item modifications, but if they are both normal inodes it
9474 * would require 5 item modifications, so we'll assume their normal
9475 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9476 * should cover the worst case number of items we'll modify.
9477 */
9478 trans = btrfs_start_transaction(root, 12);
9479 if (IS_ERR(trans)) {
9480 ret = PTR_ERR(trans);
9481 goto out_notrans;
9482 }
9483
9484 /*
9485 * We need to find a free sequence number both in the source and
9486 * in the destination directory for the exchange.
9487 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009488 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009489 if (ret)
9490 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009491 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009492 if (ret)
9493 goto out_fail;
9494
9495 BTRFS_I(old_inode)->dir_index = 0ULL;
9496 BTRFS_I(new_inode)->dir_index = 0ULL;
9497
9498 /* Reference for the source. */
9499 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9500 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009501 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009502 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009503 btrfs_pin_log_trans(root);
9504 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009505 ret = btrfs_insert_inode_ref(trans, dest,
9506 new_dentry->d_name.name,
9507 new_dentry->d_name.len,
9508 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009509 btrfs_ino(BTRFS_I(new_dir)),
9510 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009511 if (ret)
9512 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009513 }
9514
9515 /* And now for the dest. */
9516 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9517 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009518 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009519 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009520 btrfs_pin_log_trans(dest);
9521 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009522 ret = btrfs_insert_inode_ref(trans, root,
9523 old_dentry->d_name.name,
9524 old_dentry->d_name.len,
9525 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009526 btrfs_ino(BTRFS_I(old_dir)),
9527 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009528 if (ret)
9529 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009530 }
9531
9532 /* Update inode version and ctime/mtime. */
9533 inode_inc_iversion(old_dir);
9534 inode_inc_iversion(new_dir);
9535 inode_inc_iversion(old_inode);
9536 inode_inc_iversion(new_inode);
9537 old_dir->i_ctime = old_dir->i_mtime = ctime;
9538 new_dir->i_ctime = new_dir->i_mtime = ctime;
9539 old_inode->i_ctime = ctime;
9540 new_inode->i_ctime = ctime;
9541
9542 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009543 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9544 BTRFS_I(old_inode), 1);
9545 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9546 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009547 }
9548
9549 /* src is a subvolume */
9550 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9551 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9552 ret = btrfs_unlink_subvol(trans, root, old_dir,
9553 root_objectid,
9554 old_dentry->d_name.name,
9555 old_dentry->d_name.len);
9556 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009557 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9558 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009559 old_dentry->d_name.name,
9560 old_dentry->d_name.len);
9561 if (!ret)
9562 ret = btrfs_update_inode(trans, root, old_inode);
9563 }
9564 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009565 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009566 goto out_fail;
9567 }
9568
9569 /* dest is a subvolume */
9570 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9571 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9572 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9573 root_objectid,
9574 new_dentry->d_name.name,
9575 new_dentry->d_name.len);
9576 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009577 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9578 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009579 new_dentry->d_name.name,
9580 new_dentry->d_name.len);
9581 if (!ret)
9582 ret = btrfs_update_inode(trans, dest, new_inode);
9583 }
9584 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009585 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009586 goto out_fail;
9587 }
9588
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009589 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009590 new_dentry->d_name.name,
9591 new_dentry->d_name.len, 0, old_idx);
9592 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009593 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009594 goto out_fail;
9595 }
9596
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009597 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598 old_dentry->d_name.name,
9599 old_dentry->d_name.len, 0, new_idx);
9600 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009601 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009602 goto out_fail;
9603 }
9604
9605 if (old_inode->i_nlink == 1)
9606 BTRFS_I(old_inode)->dir_index = old_idx;
9607 if (new_inode->i_nlink == 1)
9608 BTRFS_I(new_inode)->dir_index = new_idx;
9609
Filipe Manana86e8aa02016-05-05 02:02:27 +01009610 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009611 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009612 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9613 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009615 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009616 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009617 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009619 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9620 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009621 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009622 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009623 }
9624out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009625 /*
9626 * If we have pinned a log and an error happened, we unpin tasks
9627 * trying to sync the log and force them to fallback to a transaction
9628 * commit if the log currently contains any of the inodes involved in
9629 * this rename operation (to ensure we do not persist a log with an
9630 * inconsistent state for any of these inodes or leading to any
9631 * inconsistencies when replayed). If the transaction was aborted, the
9632 * abortion reason is propagated to userspace when attempting to commit
9633 * the transaction. If the log does not contain any of these inodes, we
9634 * allow the tasks to sync it.
9635 */
9636 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009637 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9638 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9639 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009640 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009641 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009642 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009643
9644 if (root_log_pinned) {
9645 btrfs_end_log_trans(root);
9646 root_log_pinned = false;
9647 }
9648 if (dest_log_pinned) {
9649 btrfs_end_log_trans(dest);
9650 dest_log_pinned = false;
9651 }
9652 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009653 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654out_notrans:
9655 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009656 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009658 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009659
9660 return ret;
9661}
9662
9663static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9664 struct btrfs_root *root,
9665 struct inode *dir,
9666 struct dentry *dentry)
9667{
9668 int ret;
9669 struct inode *inode;
9670 u64 objectid;
9671 u64 index;
9672
9673 ret = btrfs_find_free_ino(root, &objectid);
9674 if (ret)
9675 return ret;
9676
9677 inode = btrfs_new_inode(trans, root, dir,
9678 dentry->d_name.name,
9679 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009680 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009681 objectid,
9682 S_IFCHR | WHITEOUT_MODE,
9683 &index);
9684
9685 if (IS_ERR(inode)) {
9686 ret = PTR_ERR(inode);
9687 return ret;
9688 }
9689
9690 inode->i_op = &btrfs_special_inode_operations;
9691 init_special_inode(inode, inode->i_mode,
9692 WHITEOUT_DEV);
9693
9694 ret = btrfs_init_inode_security(trans, inode, dir,
9695 &dentry->d_name);
9696 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009697 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009699 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9700 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009701 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009702 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009703
9704 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009705out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009706 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009707 if (ret)
9708 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709 iput(inode);
9710
Filipe Mananac9901612016-05-05 01:41:57 +01009711 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712}
9713
Chris Mason39279cc2007-06-12 06:35:45 -04009714static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715 struct inode *new_dir, struct dentry *new_dentry,
9716 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009717{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009718 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009719 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009720 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009721 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9722 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009723 struct inode *new_inode = d_inode(new_dentry);
9724 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009725 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009726 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009727 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009728 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009729 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009730
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009731 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009732 return -EPERM;
9733
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009734 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009735 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009736 return -EXDEV;
9737
Li Zefan33345d012011-04-20 10:31:50 +08009738 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009739 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009740 return -ENOTEMPTY;
9741
Chris Mason39279cc2007-06-12 06:35:45 -04009742 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009743 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009744 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009745
9746
9747 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009748 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009749 new_dentry->d_name.name,
9750 new_dentry->d_name.len);
9751
9752 if (ret) {
9753 if (ret == -EEXIST) {
9754 /* we shouldn't get
9755 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309756 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009757 return ret;
9758 }
9759 } else {
9760 /* maybe -EOVERFLOW */
9761 return ret;
9762 }
9763 }
9764 ret = 0;
9765
Chris Mason5a3f23d2009-03-31 13:27:11 -04009766 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009767 * we're using rename to replace one file with another. Start IO on it
9768 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009769 */
Chris Mason8d875f92014-08-12 10:47:42 -07009770 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009771 filemap_flush(old_inode->i_mapping);
9772
Yan, Zheng76dda932009-09-21 16:00:26 -04009773 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009774 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009775 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009776 /*
9777 * We want to reserve the absolute worst case amount of items. So if
9778 * both inodes are subvols and we need to unlink them then that would
9779 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009780 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009781 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9782 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009783 * If our rename has the whiteout flag, we need more 5 units for the
9784 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9785 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009786 */
Filipe Manana5062af32016-05-05 10:26:26 +01009787 trans_num_items = 11;
9788 if (flags & RENAME_WHITEOUT)
9789 trans_num_items += 5;
9790 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009791 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009792 ret = PTR_ERR(trans);
9793 goto out_notrans;
9794 }
Chris Mason5f39d392007-10-15 16:14:19 -04009795
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009796 if (dest != root)
9797 btrfs_record_root_in_trans(trans, dest);
9798
Nikolay Borisov877574e2017-02-20 13:50:33 +02009799 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009800 if (ret)
9801 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009802
Miao Xie67de1172013-12-26 13:07:06 +08009803 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009804 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009805 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009806 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009807 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009808 btrfs_pin_log_trans(root);
9809 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009810 ret = btrfs_insert_inode_ref(trans, dest,
9811 new_dentry->d_name.name,
9812 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009813 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009814 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009815 if (ret)
9816 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009817 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009818
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009819 inode_inc_iversion(old_dir);
9820 inode_inc_iversion(new_dir);
9821 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009822 old_dir->i_ctime = old_dir->i_mtime =
9823 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009824 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009825
Chris Mason12fcfd22009-03-24 10:24:20 -04009826 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009827 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9828 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009829
Li Zefan33345d012011-04-20 10:31:50 +08009830 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009831 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9832 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9833 old_dentry->d_name.name,
9834 old_dentry->d_name.len);
9835 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009836 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9837 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009838 old_dentry->d_name.name,
9839 old_dentry->d_name.len);
9840 if (!ret)
9841 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009842 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009843 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009844 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009845 goto out_fail;
9846 }
Chris Mason39279cc2007-06-12 06:35:45 -04009847
9848 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009849 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009850 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009851 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009852 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9853 root_objectid = BTRFS_I(new_inode)->location.objectid;
9854 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9855 root_objectid,
9856 new_dentry->d_name.name,
9857 new_dentry->d_name.len);
9858 BUG_ON(new_inode->i_nlink == 0);
9859 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009860 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9861 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009862 new_dentry->d_name.name,
9863 new_dentry->d_name.len);
9864 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009865 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009866 ret = btrfs_orphan_add(trans,
9867 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009868 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009869 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009870 goto out_fail;
9871 }
Chris Mason39279cc2007-06-12 06:35:45 -04009872 }
Josef Bacikaec74772008-07-24 12:12:38 -04009873
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009874 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009875 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009876 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009877 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009878 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009879 goto out_fail;
9880 }
Chris Mason39279cc2007-06-12 06:35:45 -04009881
Miao Xie67de1172013-12-26 13:07:06 +08009882 if (old_inode->i_nlink == 1)
9883 BTRFS_I(old_inode)->dir_index = index;
9884
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009885 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009886 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009887
David Sterbaf85b7372017-01-20 14:54:07 +01009888 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9889 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009890 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009891 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009892 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009893
9894 if (flags & RENAME_WHITEOUT) {
9895 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9896 old_dentry);
9897
9898 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009899 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009900 goto out_fail;
9901 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009902 }
Chris Mason39279cc2007-06-12 06:35:45 -04009903out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009904 /*
9905 * If we have pinned the log and an error happened, we unpin tasks
9906 * trying to sync the log and force them to fallback to a transaction
9907 * commit if the log currently contains any of the inodes involved in
9908 * this rename operation (to ensure we do not persist a log with an
9909 * inconsistent state for any of these inodes or leading to any
9910 * inconsistencies when replayed). If the transaction was aborted, the
9911 * abortion reason is propagated to userspace when attempting to commit
9912 * the transaction. If the log does not contain any of these inodes, we
9913 * allow the tasks to sync it.
9914 */
9915 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009916 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9917 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9918 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009919 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009920 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009921 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009922
9923 btrfs_end_log_trans(root);
9924 log_pinned = false;
9925 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009926 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009927out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009928 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009929 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009930
Chris Mason39279cc2007-06-12 06:35:45 -04009931 return ret;
9932}
9933
Miklos Szeredi80ace852014-07-23 15:15:32 +02009934static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9935 struct inode *new_dir, struct dentry *new_dentry,
9936 unsigned int flags)
9937{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009938 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009939 return -EINVAL;
9940
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009941 if (flags & RENAME_EXCHANGE)
9942 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9943 new_dentry);
9944
9945 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009946}
9947
Miao Xie8ccf6f192012-10-25 09:28:04 +00009948static void btrfs_run_delalloc_work(struct btrfs_work *work)
9949{
9950 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009951 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009952
9953 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9954 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009955 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009956 filemap_flush(inode->i_mapping);
9957 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9958 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009959 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009960
9961 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009962 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009963 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009964 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009965 complete(&delalloc_work->completion);
9966}
9967
9968struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009969 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009970{
9971 struct btrfs_delalloc_work *work;
9972
David Sterba100d5702015-12-08 14:39:32 +01009973 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009974 if (!work)
9975 return NULL;
9976
9977 init_completion(&work->completion);
9978 INIT_LIST_HEAD(&work->list);
9979 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009980 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009981 WARN_ON_ONCE(!inode);
9982 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9983 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009984
9985 return work;
9986}
9987
9988void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9989{
9990 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009991 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009992}
9993
Chris Masond352ac62008-09-29 15:18:18 -04009994/*
9995 * some fairly slow code that needs optimization. This walks the list
9996 * of all the inodes with pending delalloc and forces them to disk.
9997 */
Miao Xie6c255e62014-03-06 13:55:01 +08009998static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9999 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010000{
Chris Masonea8c2812008-08-04 23:17:27 -040010001 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010002 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010003 struct btrfs_delalloc_work *work, *next;
10004 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010005 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010006 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010007
Miao Xie8ccf6f192012-10-25 09:28:04 +000010008 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010009 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010010
Miao Xie573bfb72014-03-06 13:55:03 +080010011 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010012 spin_lock(&root->delalloc_lock);
10013 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010014 while (!list_empty(&splice)) {
10015 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010016 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010017
Miao Xieeb73c1b2013-05-15 07:48:22 +000010018 list_move_tail(&binode->delalloc_inodes,
10019 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010020 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010021 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010022 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010023 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010024 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010025 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010026
David Sterba651d4942015-11-27 19:24:16 +010010027 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010028 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010029 if (delay_iput)
10030 btrfs_add_delayed_iput(inode);
10031 else
10032 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010033 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010034 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010035 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010036 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010037 btrfs_queue_work(root->fs_info->flush_workers,
10038 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010039 ret++;
10040 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010041 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010042 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010043 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010044 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010045 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010046
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010047out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010048 list_for_each_entry_safe(work, next, &works, list) {
10049 list_del_init(&work->list);
10050 btrfs_wait_and_free_delalloc_work(work);
10051 }
10052
Miao Xieeb73c1b2013-05-15 07:48:22 +000010053 if (!list_empty_careful(&splice)) {
10054 spin_lock(&root->delalloc_lock);
10055 list_splice_tail(&splice, &root->delalloc_inodes);
10056 spin_unlock(&root->delalloc_lock);
10057 }
Miao Xie573bfb72014-03-06 13:55:03 +080010058 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010059 return ret;
10060}
10061
10062int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10063{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010064 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010065 int ret;
10066
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010067 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010068 return -EROFS;
10069
Miao Xie6c255e62014-03-06 13:55:01 +080010070 ret = __start_delalloc_inodes(root, delay_iput, -1);
10071 if (ret > 0)
10072 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010073 /*
10074 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010075 * we have to make sure the IO is actually started and that
10076 * ordered extents get created before we return
10077 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010078 atomic_inc(&fs_info->async_submit_draining);
10079 while (atomic_read(&fs_info->nr_async_submits) ||
10080 atomic_read(&fs_info->async_delalloc_pages)) {
10081 wait_event(fs_info->async_submit_wait,
10082 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10083 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010084 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010085 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010086 return ret;
10087}
10088
Miao Xie6c255e62014-03-06 13:55:01 +080010089int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10090 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010091{
10092 struct btrfs_root *root;
10093 struct list_head splice;
10094 int ret;
10095
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010096 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010097 return -EROFS;
10098
10099 INIT_LIST_HEAD(&splice);
10100
Miao Xie573bfb72014-03-06 13:55:03 +080010101 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010102 spin_lock(&fs_info->delalloc_root_lock);
10103 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010104 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010105 root = list_first_entry(&splice, struct btrfs_root,
10106 delalloc_root);
10107 root = btrfs_grab_fs_root(root);
10108 BUG_ON(!root);
10109 list_move_tail(&root->delalloc_root,
10110 &fs_info->delalloc_roots);
10111 spin_unlock(&fs_info->delalloc_root_lock);
10112
Miao Xie6c255e62014-03-06 13:55:01 +080010113 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010114 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010115 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010116 goto out;
10117
Miao Xie6c255e62014-03-06 13:55:01 +080010118 if (nr != -1) {
10119 nr -= ret;
10120 WARN_ON(nr < 0);
10121 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010122 spin_lock(&fs_info->delalloc_root_lock);
10123 }
10124 spin_unlock(&fs_info->delalloc_root_lock);
10125
Miao Xie6c255e62014-03-06 13:55:01 +080010126 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010127 atomic_inc(&fs_info->async_submit_draining);
10128 while (atomic_read(&fs_info->nr_async_submits) ||
10129 atomic_read(&fs_info->async_delalloc_pages)) {
10130 wait_event(fs_info->async_submit_wait,
10131 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10132 atomic_read(&fs_info->async_delalloc_pages) == 0));
10133 }
10134 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010135out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010136 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010137 spin_lock(&fs_info->delalloc_root_lock);
10138 list_splice_tail(&splice, &fs_info->delalloc_roots);
10139 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010140 }
Miao Xie573bfb72014-03-06 13:55:03 +080010141 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010142 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010143}
10144
Chris Mason39279cc2007-06-12 06:35:45 -040010145static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10146 const char *symname)
10147{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010148 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010149 struct btrfs_trans_handle *trans;
10150 struct btrfs_root *root = BTRFS_I(dir)->root;
10151 struct btrfs_path *path;
10152 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010153 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010154 int err;
10155 int drop_inode = 0;
10156 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010157 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010158 int name_len;
10159 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010160 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010161 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010162 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010163
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010164 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010165 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010166 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010167
Josef Bacik9ed74f22009-09-11 16:12:44 -040010168 /*
10169 * 2 items for inode item and ref
10170 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010171 * 1 item for updating parent inode item
10172 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010173 * 1 item for xattr if selinux is on
10174 */
Filipe Manana9269d122015-12-31 18:16:29 +000010175 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010176 if (IS_ERR(trans))
10177 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010178
Li Zefan581bb052011-04-20 10:06:11 +080010179 err = btrfs_find_free_ino(root, &objectid);
10180 if (err)
10181 goto out_unlock;
10182
Josef Bacikaec74772008-07-24 12:12:38 -040010183 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010184 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10185 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010186 if (IS_ERR(inode)) {
10187 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010188 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010189 }
Chris Mason39279cc2007-06-12 06:35:45 -040010190
Casey Schauflerad19db72011-12-15 10:09:07 -050010191 /*
10192 * If the active LSM wants to access the inode during
10193 * d_instantiate it needs these. Smack checks to see
10194 * if the filesystem supports xattrs by looking at the
10195 * ops vector.
10196 */
10197 inode->i_fop = &btrfs_file_operations;
10198 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010199 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010200 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10201
10202 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10203 if (err)
10204 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010205
Chris Mason39279cc2007-06-12 06:35:45 -040010206 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010207 if (!path) {
10208 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010209 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010210 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010211 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010212 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010213 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010214 datasize = btrfs_file_extent_calc_inline_size(name_len);
10215 err = btrfs_insert_empty_item(trans, root, path, &key,
10216 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010217 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010218 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010219 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010220 }
Chris Mason5f39d392007-10-15 16:14:19 -040010221 leaf = path->nodes[0];
10222 ei = btrfs_item_ptr(leaf, path->slots[0],
10223 struct btrfs_file_extent_item);
10224 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10225 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010226 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010227 btrfs_set_file_extent_encryption(leaf, ei, 0);
10228 btrfs_set_file_extent_compression(leaf, ei, 0);
10229 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10230 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10231
Chris Mason39279cc2007-06-12 06:35:45 -040010232 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010233 write_extent_buffer(leaf, symname, ptr, name_len);
10234 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010235 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010236
Chris Mason39279cc2007-06-12 06:35:45 -040010237 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010238 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010239 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010240 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010241 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010242 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010243 /*
10244 * Last step, add directory indexes for our symlink inode. This is the
10245 * last step to avoid extra cleanup of these indexes if an error happens
10246 * elsewhere above.
10247 */
10248 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010249 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10250 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010251 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010252 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010253 goto out_unlock_inode;
10254 }
10255
10256 unlock_new_inode(inode);
10257 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010258
10259out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010260 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010261 if (drop_inode) {
10262 inode_dec_link_count(inode);
10263 iput(inode);
10264 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010265 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010266 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010267
10268out_unlock_inode:
10269 drop_inode = 1;
10270 unlock_new_inode(inode);
10271 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010272}
Chris Mason16432982008-04-10 10:23:21 -040010273
Josef Bacik0af3d002010-06-21 14:48:16 -040010274static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10275 u64 start, u64 num_bytes, u64 min_size,
10276 loff_t actual_len, u64 *alloc_hint,
10277 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010278{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010280 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10281 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010282 struct btrfs_root *root = BTRFS_I(inode)->root;
10283 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010284 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010285 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010286 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010287 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010288 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010289 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010290 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010291
Josef Bacik0af3d002010-06-21 14:48:16 -040010292 if (trans)
10293 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010294 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010295 if (own_trans) {
10296 trans = btrfs_start_transaction(root, 3);
10297 if (IS_ERR(trans)) {
10298 ret = PTR_ERR(trans);
10299 break;
10300 }
Yan Zhengd899e052008-10-30 14:25:28 -040010301 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010302
Byongho Leeee221842015-12-15 01:42:10 +090010303 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010304 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010305 /*
10306 * If we are severely fragmented we could end up with really
10307 * small allocations, so if the allocator is returning small
10308 * chunks lets make its job easier by only searching for those
10309 * sized chunks.
10310 */
10311 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010312 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10313 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010314 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010315 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010316 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010317 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010318 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010319 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010320
Josef Bacik0b670dc2015-09-23 17:11:16 -040010321 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010322 ret = insert_reserved_file_extent(trans, inode,
10323 cur_offset, ins.objectid,
10324 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010325 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010326 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010327 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010328 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010329 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010330 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010331 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010332 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010333 break;
10334 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010335
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010336 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010337 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010338
Josef Bacik5dc562c2012-08-17 13:14:17 -040010339 em = alloc_extent_map();
10340 if (!em) {
10341 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10342 &BTRFS_I(inode)->runtime_flags);
10343 goto next;
10344 }
10345
10346 em->start = cur_offset;
10347 em->orig_start = cur_offset;
10348 em->len = ins.offset;
10349 em->block_start = ins.objectid;
10350 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010351 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010352 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010353 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010354 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10355 em->generation = trans->transid;
10356
10357 while (1) {
10358 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010359 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010360 write_unlock(&em_tree->lock);
10361 if (ret != -EEXIST)
10362 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010363 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010364 cur_offset + ins.offset - 1,
10365 0);
10366 }
10367 free_extent_map(em);
10368next:
Yan Zhengd899e052008-10-30 14:25:28 -040010369 num_bytes -= ins.offset;
10370 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010371 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010372
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010373 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010374 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010375 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010376 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010377 (actual_len > inode->i_size) &&
10378 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010379 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010380 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010381 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010382 i_size = cur_offset;
10383 i_size_write(inode, i_size);
10384 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010385 }
10386
Yan Zhengd899e052008-10-30 14:25:28 -040010387 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010388
10389 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010390 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010391 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010392 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010393 break;
10394 }
Yan Zhengd899e052008-10-30 14:25:28 -040010395
Josef Bacik0af3d002010-06-21 14:48:16 -040010396 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010397 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010398 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010399 if (cur_offset < end)
10400 btrfs_free_reserved_data_space(inode, cur_offset,
10401 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010402 return ret;
10403}
10404
Josef Bacik0af3d002010-06-21 14:48:16 -040010405int btrfs_prealloc_file_range(struct inode *inode, int mode,
10406 u64 start, u64 num_bytes, u64 min_size,
10407 loff_t actual_len, u64 *alloc_hint)
10408{
10409 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10410 min_size, actual_len, alloc_hint,
10411 NULL);
10412}
10413
10414int btrfs_prealloc_file_range_trans(struct inode *inode,
10415 struct btrfs_trans_handle *trans, int mode,
10416 u64 start, u64 num_bytes, u64 min_size,
10417 loff_t actual_len, u64 *alloc_hint)
10418{
10419 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10420 min_size, actual_len, alloc_hint, trans);
10421}
10422
Chris Masone6dcd2d2008-07-17 12:53:50 -040010423static int btrfs_set_page_dirty(struct page *page)
10424{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010425 return __set_page_dirty_nobuffers(page);
10426}
10427
Al Viro10556cb2011-06-20 19:28:19 -040010428static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010429{
Li Zefanb83cc962010-12-20 16:04:08 +080010430 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010431 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010432
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010433 if (mask & MAY_WRITE &&
10434 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10435 if (btrfs_root_readonly(root))
10436 return -EROFS;
10437 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10438 return -EACCES;
10439 }
Al Viro2830ba72011-06-20 19:16:29 -040010440 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010441}
Chris Mason39279cc2007-06-12 06:35:45 -040010442
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010443static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10444{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010445 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010446 struct btrfs_trans_handle *trans;
10447 struct btrfs_root *root = BTRFS_I(dir)->root;
10448 struct inode *inode = NULL;
10449 u64 objectid;
10450 u64 index;
10451 int ret = 0;
10452
10453 /*
10454 * 5 units required for adding orphan entry
10455 */
10456 trans = btrfs_start_transaction(root, 5);
10457 if (IS_ERR(trans))
10458 return PTR_ERR(trans);
10459
10460 ret = btrfs_find_free_ino(root, &objectid);
10461 if (ret)
10462 goto out;
10463
10464 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010465 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010466 if (IS_ERR(inode)) {
10467 ret = PTR_ERR(inode);
10468 inode = NULL;
10469 goto out;
10470 }
10471
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010472 inode->i_fop = &btrfs_file_operations;
10473 inode->i_op = &btrfs_file_inode_operations;
10474
10475 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010476 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10477
Chris Masonb0d5d102014-09-08 13:08:51 -070010478 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10479 if (ret)
10480 goto out_inode;
10481
10482 ret = btrfs_update_inode(trans, root, inode);
10483 if (ret)
10484 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010485 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010486 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010487 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010488
Filipe Manana5762b5c2014-08-01 00:10:32 +010010489 /*
10490 * We set number of links to 0 in btrfs_new_inode(), and here we set
10491 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10492 * through:
10493 *
10494 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10495 */
10496 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010497 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010498 d_tmpfile(dentry, inode);
10499 mark_inode_dirty(inode);
10500
10501out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010502 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010503 if (ret)
10504 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010505 btrfs_balance_delayed_items(fs_info);
10506 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010507 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010508
10509out_inode:
10510 unlock_new_inode(inode);
10511 goto out;
10512
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010513}
10514
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010515static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010516 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010517 .lookup = btrfs_lookup,
10518 .create = btrfs_create,
10519 .unlink = btrfs_unlink,
10520 .link = btrfs_link,
10521 .mkdir = btrfs_mkdir,
10522 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010523 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010524 .symlink = btrfs_symlink,
10525 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010526 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010527 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010528 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010529 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010530 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010531 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010532 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010533};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010534static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010535 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010536 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010537 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010538};
Yan, Zheng76dda932009-09-21 16:00:26 -040010539
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010540static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010541 .llseek = generic_file_llseek,
10542 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010543 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010544 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010545#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010546 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010547#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010548 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010549 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010550};
10551
David Sterba20e55062015-11-19 11:42:28 +010010552static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010553 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010554 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010555 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010556 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010557 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010558 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010559 .set_bit_hook = btrfs_set_bit_hook,
10560 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010561 .merge_extent_hook = btrfs_merge_extent_hook,
10562 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010563};
10564
Chris Mason35054392009-01-21 13:11:13 -050010565/*
10566 * btrfs doesn't support the bmap operation because swapfiles
10567 * use bmap to make a mapping of extents in the file. They assume
10568 * these extents won't change over the life of the file and they
10569 * use the bmap result to do IO directly to the drive.
10570 *
10571 * the btrfs bmap call would return logical addresses that aren't
10572 * suitable for IO and they also will change frequently as COW
10573 * operations happen. So, swapfile + btrfs == corruption.
10574 *
10575 * For now we're avoiding this by dropping bmap.
10576 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010577static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010578 .readpage = btrfs_readpage,
10579 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010580 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010581 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010582 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010583 .invalidatepage = btrfs_invalidatepage,
10584 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010585 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010586 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010587};
10588
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010589static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010590 .readpage = btrfs_readpage,
10591 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010592 .invalidatepage = btrfs_invalidatepage,
10593 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010594};
10595
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010596static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010597 .getattr = btrfs_getattr,
10598 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010599 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010600 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010601 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010602 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010603 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010604 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010605};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010606static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010607 .getattr = btrfs_getattr,
10608 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010609 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010610 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010611 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010612 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010613 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010614};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010615static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010616 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010617 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010618 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010619 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010620 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010621 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010622};
Yan, Zheng76dda932009-09-21 16:00:26 -040010623
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010624const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010625 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010626 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010627};