blob: dafd3fdd6f2bde5624132a11e0181de5310716a8 [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;
433 unsigned long nr_pages_ret = 0;
434 unsigned long total_compressed = 0;
435 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900436 unsigned long max_compressed = SZ_128K;
437 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400438 int i;
439 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400440 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400441 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400442
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200443 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
444 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400445
Chris Mason42dc7ba2008-12-15 11:44:56 -0500446 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400447again:
448 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300449 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
450 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400451
Chris Masonf03d9301f2009-02-04 09:31:06 -0500452 /*
453 * we don't want to send crud past the end of i_size through
454 * compression, that's just a waste of CPU time. So, if the
455 * end of the file is before the start of our current
456 * requested range of bytes, we bail out to the uncompressed
457 * cleanup code that can deal with all of this.
458 *
459 * It isn't really the fastest way to fix things, but this is a
460 * very uncommon corner.
461 */
462 if (actual_end <= start)
463 goto cleanup_and_bail_uncompressed;
464
Chris Masonc8b97812008-10-29 14:49:59 -0400465 total_compressed = actual_end - start;
466
Shilong Wang4bcbb332014-10-07 18:44:35 -0400467 /*
468 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400469 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400470 */
471 if (total_compressed <= blocksize &&
472 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
473 goto cleanup_and_bail_uncompressed;
474
Chris Masonc8b97812008-10-29 14:49:59 -0400475 /* we want to make sure that amount of ram required to uncompress
476 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500477 * of a compressed extent to 128k. This is a crucial number
478 * because it also controls how easily we can spread reads across
479 * cpus for decompression.
480 *
481 * We also want to make sure the amount of IO required to do
482 * a random read is reasonably small, so we limit the size of
483 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400484 */
485 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000486 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500487 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400488 total_in = 0;
489 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400490
Chris Mason771ed682008-11-06 22:02:51 -0500491 /*
492 * we do compression for mount -o compress and when the
493 * inode has not been flagged as nocompress. This flag can
494 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400495 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800496 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400497 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100498 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800499 if (!pages) {
500 /* just bail out to the uncompressed code */
501 goto cont;
502 }
Chris Mason179e29e2007-11-01 11:28:41 -0400503
Li Zefan261507a02010-12-17 14:21:50 +0800504 if (BTRFS_I(inode)->force_compress)
505 compress_type = BTRFS_I(inode)->force_compress;
506
Chris Mason4adaa612013-03-26 13:07:00 -0400507 /*
508 * we need to call clear_page_dirty_for_io on each
509 * page in the range. Otherwise applications with the file
510 * mmap'd can wander in and change the page contents while
511 * we are compressing them.
512 *
513 * If the compression fails for any reason, we set the pages
514 * dirty again later on.
515 */
516 extent_range_clear_dirty_for_io(inode, start, end);
517 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800518 ret = btrfs_compress_pages(compress_type,
519 inode->i_mapping, start,
520 total_compressed, pages,
521 nr_pages, &nr_pages_ret,
522 &total_in,
523 &total_compressed,
524 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400525
526 if (!ret) {
527 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300528 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 struct page *page = pages[nr_pages_ret - 1];
530 char *kaddr;
531
532 /* zero the tail end of the last page, we might be
533 * sending it down to disk
534 */
535 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800536 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400537 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300538 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800539 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400540 }
541 will_compress = 1;
542 }
543 }
Li Zefan560f7d72011-09-08 10:22:01 +0800544cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400545 if (start == 0) {
546 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500547 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400548 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500549 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400550 */
Josef Bacik00361582013-08-14 14:02:47 -0400551 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800552 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400553 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500554 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400555 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000556 total_compressed,
557 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400558 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100559 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400560 unsigned long clear_flags = EXTENT_DELALLOC |
561 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100562 unsigned long page_error_op;
563
Josef Bacik151a41b2013-07-29 13:22:24 -0400564 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100565 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400566
Chris Mason771ed682008-11-06 22:02:51 -0500567 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100568 * inline extent creation worked or returned error,
569 * we don't need to create any more async work items.
570 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500571 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800572 extent_clear_unlock_delalloc(inode, start, end, end,
573 NULL, clear_flags,
574 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400575 PAGE_CLEAR_DIRTY |
576 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100577 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400578 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800579 btrfs_free_reserved_data_space_noquota(inode, start,
580 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400581 goto free_pages_out;
582 }
583 }
584
585 if (will_compress) {
586 /*
587 * we aren't doing an inline extent round the compressed size
588 * up to a block size boundary so the allocator does sane
589 * things
590 */
Qu Wenruofda28322013-02-26 08:10:22 +0000591 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400592
593 /*
594 * one last check to make sure the compression is really a
595 * win, compare the page count read with the blocks on disk
596 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300597 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400598 if (total_compressed >= total_in) {
599 will_compress = 0;
600 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400601 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700602 *num_added += 1;
603
604 /*
605 * The async work queues will take care of doing actual
606 * allocation on disk for these compressed pages, and
607 * will submit them to the elevator.
608 */
609 add_async_extent(async_cow, start, num_bytes,
610 total_compressed, pages, nr_pages_ret,
611 compress_type);
612
613 if (start + num_bytes < end) {
614 start += num_bytes;
615 pages = NULL;
616 cond_resched();
617 goto again;
618 }
619 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400620 }
621 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700622 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400623 /*
624 * the compression code ran but failed to make things smaller,
625 * free any pages it allocated and our page pointer array
626 */
627 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400628 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300629 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400630 }
631 kfree(pages);
632 pages = NULL;
633 total_compressed = 0;
634 nr_pages_ret = 0;
635
636 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400637 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500638 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500639 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500640 }
Chris Masonc8b97812008-10-29 14:49:59 -0400641 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500642cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700643 /*
644 * No compression, but we still need to write the pages in the file
645 * we've been given so far. redirty the locked page if it corresponds
646 * to our extent and set things up for the async work queue to run
647 * cow_file_range to do the normal delalloc dance.
648 */
649 if (page_offset(locked_page) >= start &&
650 page_offset(locked_page) <= end)
651 __set_page_dirty_nobuffers(locked_page);
652 /* unlocked later on in the async handlers */
653
654 if (redirty)
655 extent_range_redirty_for_io(inode, start, end);
656 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
657 BTRFS_COMPRESS_NONE);
658 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500659
Filipe Mananac44f6492014-10-09 21:15:44 +0100660 return;
Chris Mason771ed682008-11-06 22:02:51 -0500661
662free_pages_out:
663 for (i = 0; i < nr_pages_ret; i++) {
664 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300665 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500666 }
Chris Masond3977122009-01-05 21:25:51 -0500667 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500668}
Chris Mason771ed682008-11-06 22:02:51 -0500669
Filipe Manana40ae8372014-10-06 22:14:24 +0100670static void free_async_extent_pages(struct async_extent *async_extent)
671{
672 int i;
673
674 if (!async_extent->pages)
675 return;
676
677 for (i = 0; i < async_extent->nr_pages; i++) {
678 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300679 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100680 }
681 kfree(async_extent->pages);
682 async_extent->nr_pages = 0;
683 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500684}
685
686/*
687 * phase two of compressed writeback. This is the ordered portion
688 * of the code, which only gets called in the order the work was
689 * queued. We walk all the async extents created by compress_file_range
690 * and send them down to the disk.
691 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100692static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500693 struct async_cow *async_cow)
694{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400695 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500696 struct async_extent *async_extent;
697 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500698 struct btrfs_key ins;
699 struct extent_map *em;
700 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500701 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500702 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500703
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500704again:
Chris Masond3977122009-01-05 21:25:51 -0500705 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500706 async_extent = list_entry(async_cow->extents.next,
707 struct async_extent, list);
708 list_del(&async_extent->list);
709
710 io_tree = &BTRFS_I(inode)->io_tree;
711
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500712retry:
Chris Mason771ed682008-11-06 22:02:51 -0500713 /* did the compression code fall back to uncompressed IO? */
714 if (!async_extent->pages) {
715 int page_started = 0;
716 unsigned long nr_written = 0;
717
718 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000719 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100720 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500721
722 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500723 ret = cow_file_range(inode, async_cow->locked_page,
724 async_extent->start,
725 async_extent->start +
726 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800727 async_extent->start +
728 async_extent->ram_size - 1,
729 &page_started, &nr_written, 0,
730 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500731
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100732 /* JDM XXX */
733
Chris Mason771ed682008-11-06 22:02:51 -0500734 /*
735 * if page_started, cow_file_range inserted an
736 * inline extent and took care of all the unlocking
737 * and IO for us. Otherwise, we need to submit
738 * all those pages down to the drive.
739 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500740 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500741 extent_write_locked_range(io_tree,
742 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500743 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500744 async_extent->ram_size - 1,
745 btrfs_get_extent,
746 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500747 else if (ret)
748 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500749 kfree(async_extent);
750 cond_resched();
751 continue;
752 }
753
754 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100755 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500756
Wang Xiaoguang18513092016-07-25 15:51:40 +0800757 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500758 async_extent->compressed_size,
759 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800760 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500761 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100762 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500763
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400764 if (ret == -ENOSPC) {
765 unlock_extent(io_tree, async_extent->start,
766 async_extent->start +
767 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800768
769 /*
770 * we need to redirty the pages if we decide to
771 * fallback to uncompressed IO, otherwise we
772 * will not submit these pages down to lower
773 * layers.
774 */
775 extent_range_redirty_for_io(inode,
776 async_extent->start,
777 async_extent->start +
778 async_extent->ram_size - 1);
779
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100780 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400781 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500782 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500783 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000784 /*
785 * here we're doing allocation and writeback of the
786 * compressed pages
787 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800788 em = create_io_em(inode, async_extent->start,
789 async_extent->ram_size, /* len */
790 async_extent->start, /* orig_start */
791 ins.objectid, /* block_start */
792 ins.offset, /* block_len */
793 ins.offset, /* orig_block_len */
794 async_extent->ram_size, /* ram_bytes */
795 async_extent->compress_type,
796 BTRFS_ORDERED_COMPRESSED);
797 if (IS_ERR(em))
798 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500799 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800800 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500801
Li Zefan261507a02010-12-17 14:21:50 +0800802 ret = btrfs_add_ordered_extent_compress(inode,
803 async_extent->start,
804 ins.objectid,
805 async_extent->ram_size,
806 ins.offset,
807 BTRFS_ORDERED_COMPRESSED,
808 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100809 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200810 btrfs_drop_extent_cache(BTRFS_I(inode),
811 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100812 async_extent->start +
813 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500814 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100815 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400816 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500817
Chris Mason771ed682008-11-06 22:02:51 -0500818 /*
819 * clear dirty, set writeback and unlock the pages.
820 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400821 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400822 async_extent->start +
823 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800824 async_extent->start +
825 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400826 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
827 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400828 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500829 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500830 async_extent->start,
831 async_extent->ram_size,
832 ins.objectid,
833 ins.offset, async_extent->pages,
834 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100835 if (ret) {
836 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
837 struct page *p = async_extent->pages[0];
838 const u64 start = async_extent->start;
839 const u64 end = start + async_extent->ram_size - 1;
840
841 p->mapping = inode->i_mapping;
842 tree->ops->writepage_end_io_hook(p, start, end,
843 NULL, 0);
844 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800845 extent_clear_unlock_delalloc(inode, start, end, end,
846 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100847 PAGE_END_WRITEBACK |
848 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100849 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100850 }
Chris Mason771ed682008-11-06 22:02:51 -0500851 alloc_hint = ins.objectid + ins.offset;
852 kfree(async_extent);
853 cond_resched();
854 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100855 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500856out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400857 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400858 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100859out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400860 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500861 async_extent->start +
862 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800863 async_extent->start +
864 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400865 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400866 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
867 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100868 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
869 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100870 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100871 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500873}
874
Josef Bacik4b46fce2010-05-23 11:00:55 -0400875static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
876 u64 num_bytes)
877{
878 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
879 struct extent_map *em;
880 u64 alloc_hint = 0;
881
882 read_lock(&em_tree->lock);
883 em = search_extent_mapping(em_tree, start, num_bytes);
884 if (em) {
885 /*
886 * if block start isn't an actual block number then find the
887 * first block in this inode and use that as a hint. If that
888 * block is also bogus then just don't worry about it.
889 */
890 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
891 free_extent_map(em);
892 em = search_extent_mapping(em_tree, 0, 0);
893 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
894 alloc_hint = em->block_start;
895 if (em)
896 free_extent_map(em);
897 } else {
898 alloc_hint = em->block_start;
899 free_extent_map(em);
900 }
901 }
902 read_unlock(&em_tree->lock);
903
904 return alloc_hint;
905}
906
Chris Mason771ed682008-11-06 22:02:51 -0500907/*
908 * when extent_io.c finds a delayed allocation range in the file,
909 * the call backs end up in this code. The basic idea is to
910 * allocate extents on disk for the range, and create ordered data structs
911 * in ram to track those extents.
912 *
913 * locked_page is the page that writepage had locked already. We use
914 * it to make sure we don't do extra locks or unlocks.
915 *
916 * *page_started is set to one if we unlock locked_page and do everything
917 * required to start IO on it. It may be clean and already done with
918 * IO when we return.
919 */
Josef Bacik00361582013-08-14 14:02:47 -0400920static noinline int cow_file_range(struct inode *inode,
921 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800922 u64 start, u64 end, u64 delalloc_end,
923 int *page_started, unsigned long *nr_written,
924 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500925{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400926 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400927 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500928 u64 alloc_hint = 0;
929 u64 num_bytes;
930 unsigned long ram_size;
931 u64 disk_num_bytes;
932 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400933 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500934 struct btrfs_key ins;
935 struct extent_map *em;
Chris Mason771ed682008-11-06 22:02:51 -0500936 int ret = 0;
937
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200938 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400939 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500940 ret = -EINVAL;
941 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400942 }
Chris Mason771ed682008-11-06 22:02:51 -0500943
Qu Wenruofda28322013-02-26 08:10:22 +0000944 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500945 num_bytes = max(blocksize, num_bytes);
946 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500947
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200948 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400949
Chris Mason771ed682008-11-06 22:02:51 -0500950 if (start == 0) {
951 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800952 ret = cow_file_range_inline(root, inode, start, end, 0,
953 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500954 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800955 extent_clear_unlock_delalloc(inode, start, end,
956 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400957 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400958 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400959 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
960 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800961 btrfs_free_reserved_data_space_noquota(inode, start,
962 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500963 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300964 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500965 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500966 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100967 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100968 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500969 }
970 }
Chris Masonc8b97812008-10-29 14:49:59 -0400971
972 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400973 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400974
Josef Bacik4b46fce2010-05-23 11:00:55 -0400975 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200976 btrfs_drop_extent_cache(BTRFS_I(inode), start,
977 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400978
Chris Masond3977122009-01-05 21:25:51 -0500979 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400980 unsigned long op;
981
Josef Bacik287a0ab2010-03-19 18:07:23 +0000982 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800983 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400984 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800985 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400986 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500988
Chris Mason771ed682008-11-06 22:02:51 -0500989 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -0800990 em = create_io_em(inode, start, ins.offset, /* len */
991 start, /* orig_start */
992 ins.objectid, /* block_start */
993 ins.offset, /* block_len */
994 ins.offset, /* orig_block_len */
995 ram_size, /* ram_bytes */
996 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -0800997 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -0800998 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +0000999 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001000 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001001
Chris Mason98d20f62008-04-14 09:46:10 -04001002 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001003 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001004 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001005 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001006 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001007
Yan Zheng17d217f2008-12-12 10:03:38 -05001008 if (root->root_key.objectid ==
1009 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1010 ret = btrfs_reloc_clone_csums(inode, start,
1011 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001012 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001013 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001014 }
1015
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001016 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001017
Chris Masond3977122009-01-05 21:25:51 -05001018 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001019 break;
Chris Masond3977122009-01-05 21:25:51 -05001020
Chris Masonc8b97812008-10-29 14:49:59 -04001021 /* we're not doing compressed IO, don't unlock the first
1022 * page (which the caller expects to stay locked), don't
1023 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001024 *
1025 * Do set the Private2 bit so we know this page was properly
1026 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001027 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001028 op = unlock ? PAGE_UNLOCK : 0;
1029 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001030
Josef Bacikc2790a22013-07-29 11:20:47 -04001031 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001032 start + ram_size - 1,
1033 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001034 EXTENT_LOCKED | EXTENT_DELALLOC,
1035 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001036 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001037 num_bytes -= cur_alloc_size;
1038 alloc_hint = ins.objectid + ins.offset;
1039 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001040 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001041out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001042 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001043
Filipe Mananad9f85962014-08-25 10:43:00 +01001044out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001045 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001046out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001047 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001048 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001049out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001050 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1051 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001052 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1053 EXTENT_DELALLOC | EXTENT_DEFRAG,
1054 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1055 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001056 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001057}
Chris Masonc8b97812008-10-29 14:49:59 -04001058
Chris Mason771ed682008-11-06 22:02:51 -05001059/*
1060 * work queue call back to started compression on a file and pages
1061 */
1062static noinline void async_cow_start(struct btrfs_work *work)
1063{
1064 struct async_cow *async_cow;
1065 int num_added = 0;
1066 async_cow = container_of(work, struct async_cow, work);
1067
1068 compress_file_range(async_cow->inode, async_cow->locked_page,
1069 async_cow->start, async_cow->end, async_cow,
1070 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001071 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001072 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001073 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001074 }
Chris Mason771ed682008-11-06 22:02:51 -05001075}
1076
1077/*
1078 * work queue call back to submit previously compressed pages
1079 */
1080static noinline void async_cow_submit(struct btrfs_work *work)
1081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001082 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001083 struct async_cow *async_cow;
1084 struct btrfs_root *root;
1085 unsigned long nr_pages;
1086
1087 async_cow = container_of(work, struct async_cow, work);
1088
1089 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001090 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001091 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1092 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001093
David Sterbaee863952015-02-16 19:41:40 +01001094 /*
1095 * atomic_sub_return implies a barrier for waitqueue_active
1096 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001097 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001098 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001099 waitqueue_active(&fs_info->async_submit_wait))
1100 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001101
Chris Masond3977122009-01-05 21:25:51 -05001102 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001103 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001104}
Chris Masonc8b97812008-10-29 14:49:59 -04001105
Chris Mason771ed682008-11-06 22:02:51 -05001106static noinline void async_cow_free(struct btrfs_work *work)
1107{
1108 struct async_cow *async_cow;
1109 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001110 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001111 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001112 kfree(async_cow);
1113}
1114
1115static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1116 u64 start, u64 end, int *page_started,
1117 unsigned long *nr_written)
1118{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001119 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001120 struct async_cow *async_cow;
1121 struct btrfs_root *root = BTRFS_I(inode)->root;
1122 unsigned long nr_pages;
1123 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001124
Chris Masona3429ab2009-10-08 12:30:20 -04001125 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1126 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001127 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001128 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001129 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001130 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001131 async_cow->root = root;
1132 async_cow->locked_page = locked_page;
1133 async_cow->start = start;
1134
Wang Shilongf79707b2014-07-17 11:44:09 +08001135 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001136 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001137 cur_end = end;
1138 else
Byongho Leeee221842015-12-15 01:42:10 +09001139 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001140
1141 async_cow->end = cur_end;
1142 INIT_LIST_HEAD(&async_cow->extents);
1143
Liu Bo9e0af232014-08-15 23:36:53 +08001144 btrfs_init_work(&async_cow->work,
1145 btrfs_delalloc_helper,
1146 async_cow_start, async_cow_submit,
1147 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001148
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001149 nr_pages = (cur_end - start + PAGE_SIZE) >>
1150 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001151 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001152
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001153 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001154
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001155 while (atomic_read(&fs_info->async_submit_draining) &&
1156 atomic_read(&fs_info->async_delalloc_pages)) {
1157 wait_event(fs_info->async_submit_wait,
1158 (atomic_read(&fs_info->async_delalloc_pages) ==
1159 0));
Chris Mason771ed682008-11-06 22:02:51 -05001160 }
1161
1162 *nr_written += nr_pages;
1163 start = cur_end + 1;
1164 }
1165 *page_started = 1;
1166 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001167}
1168
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001169static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001170 u64 bytenr, u64 num_bytes)
1171{
1172 int ret;
1173 struct btrfs_ordered_sum *sums;
1174 LIST_HEAD(list);
1175
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001176 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001177 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001178 if (ret == 0 && list_empty(&list))
1179 return 0;
1180
1181 while (!list_empty(&list)) {
1182 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1183 list_del(&sums->list);
1184 kfree(sums);
1185 }
1186 return 1;
1187}
1188
Chris Masond352ac62008-09-29 15:18:18 -04001189/*
1190 * when nowcow writeback call back. This checks for snapshots or COW copies
1191 * of the extents that exist in the file, and COWs the file as required.
1192 *
1193 * If no cow copies or snapshots exist, we write directly to the existing
1194 * blocks on disk
1195 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001196static noinline int run_delalloc_nocow(struct inode *inode,
1197 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001198 u64 start, u64 end, int *page_started, int force,
1199 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001200{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001201 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001202 struct btrfs_root *root = BTRFS_I(inode)->root;
1203 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001204 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001205 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001206 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001207 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 u64 cow_start;
1209 u64 cur_offset;
1210 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001211 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001212 u64 disk_bytenr;
1213 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001214 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001215 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001217 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001218 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 int nocow;
1220 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001221 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001222 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001223
1224 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001225 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001226 extent_clear_unlock_delalloc(inode, start, end, end,
1227 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001228 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001229 EXTENT_DO_ACCOUNTING |
1230 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001231 PAGE_CLEAR_DIRTY |
1232 PAGE_SET_WRITEBACK |
1233 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001234 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001235 }
Li Zefan82d59022011-04-20 10:33:24 +08001236
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001237 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001238
Yan Zheng80ff3852008-10-30 14:20:02 -04001239 cow_start = (u64)-1;
1240 cur_offset = start;
1241 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001242 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001244 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001245 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1247 leaf = path->nodes[0];
1248 btrfs_item_key_to_cpu(leaf, &found_key,
1249 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001250 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 found_key.type == BTRFS_EXTENT_DATA_KEY)
1252 path->slots[0]--;
1253 }
1254 check_prev = 0;
1255next_slot:
1256 leaf = path->nodes[0];
1257 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1258 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001259 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001260 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 if (ret > 0)
1262 break;
1263 leaf = path->nodes[0];
1264 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001265
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 nocow = 0;
1267 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001268 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001270
Filipe Manana1d512cb2015-11-09 00:33:58 +00001271 if (found_key.objectid > ino)
1272 break;
1273 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1274 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1275 path->slots[0]++;
1276 goto next_slot;
1277 }
1278 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 found_key.offset > end)
1280 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001281
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 if (found_key.offset > cur_offset) {
1283 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001284 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 goto out_check;
1286 }
Chris Masonc31f8832008-01-08 15:46:31 -05001287
Yan Zheng80ff3852008-10-30 14:20:02 -04001288 fi = btrfs_item_ptr(leaf, path->slots[0],
1289 struct btrfs_file_extent_item);
1290 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001291
Josef Bacikcc95bef2013-04-04 14:31:27 -04001292 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001293 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1294 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001296 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 extent_end = found_key.offset +
1298 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001299 disk_num_bytes =
1300 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 if (extent_end <= start) {
1302 path->slots[0]++;
1303 goto next_slot;
1304 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001305 if (disk_bytenr == 0)
1306 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 if (btrfs_file_extent_compression(leaf, fi) ||
1308 btrfs_file_extent_encryption(leaf, fi) ||
1309 btrfs_file_extent_other_encoding(leaf, fi))
1310 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001311 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1312 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001313 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001315 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001316 found_key.offset -
1317 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001318 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001319 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001320 disk_bytenr += cur_offset - found_key.offset;
1321 num_bytes = min(end + 1, extent_end) - cur_offset;
1322 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001323 * if there are pending snapshots for this root,
1324 * we fall into common COW way.
1325 */
1326 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001327 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001328 if (!err)
1329 goto out_check;
1330 }
1331 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001332 * force cow if csum exists in the range.
1333 * this ensure that csum for a given extent are
1334 * either valid or do not exist.
1335 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001336 if (csum_exist_in_range(fs_info, disk_bytenr,
1337 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001338 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001339 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001340 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 nocow = 1;
1342 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1343 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001344 btrfs_file_extent_inline_len(leaf,
1345 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001346 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001347 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 } else {
1349 BUG_ON(1);
1350 }
1351out_check:
1352 if (extent_end <= start) {
1353 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001354 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001355 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001356 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001357 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 goto next_slot;
1359 }
1360 if (!nocow) {
1361 if (cow_start == (u64)-1)
1362 cow_start = cur_offset;
1363 cur_offset = extent_end;
1364 if (cur_offset > end)
1365 break;
1366 path->slots[0]++;
1367 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001368 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001369
David Sterbab3b4aa72011-04-21 01:20:15 +02001370 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001372 ret = cow_file_range(inode, locked_page,
1373 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001374 end, page_started, nr_written, 1,
1375 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001376 if (ret) {
1377 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001378 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001379 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001380 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001381 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001382 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001383 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001384 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001385 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001386
Yan Zhengd899e052008-10-30 14:25:28 -04001387 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001388 u64 orig_start = found_key.offset - extent_offset;
1389
1390 em = create_io_em(inode, cur_offset, num_bytes,
1391 orig_start,
1392 disk_bytenr, /* block_start */
1393 num_bytes, /* block_len */
1394 disk_num_bytes, /* orig_block_len */
1395 ram_bytes, BTRFS_COMPRESS_NONE,
1396 BTRFS_ORDERED_PREALLOC);
1397 if (IS_ERR(em)) {
1398 if (!nolock && nocow)
1399 btrfs_end_write_no_snapshoting(root);
1400 if (nocow)
1401 btrfs_dec_nocow_writers(fs_info,
1402 disk_bytenr);
1403 ret = PTR_ERR(em);
1404 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001405 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001406 free_extent_map(em);
1407 }
1408
1409 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001410 type = BTRFS_ORDERED_PREALLOC;
1411 } else {
1412 type = BTRFS_ORDERED_NOCOW;
1413 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001414
1415 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001416 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001417 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001418 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001419 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001420
Yan, Zhengefa56462010-05-16 10:49:59 -04001421 if (root->root_key.objectid ==
1422 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1423 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1424 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001425 if (ret) {
1426 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001427 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001428 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001429 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001430 }
1431
Josef Bacikc2790a22013-07-29 11:20:47 -04001432 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001433 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001434 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001435 EXTENT_DELALLOC |
1436 EXTENT_CLEAR_DATA_RESV,
1437 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1438
Wang Shilonge9894fd2014-03-27 11:12:25 +08001439 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001440 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 cur_offset = extent_end;
1442 if (cur_offset > end)
1443 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001444 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001445 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001446
Josef Bacik17ca04a2012-05-31 15:58:55 -04001447 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001448 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001449 cur_offset = end;
1450 }
1451
Yan Zheng80ff3852008-10-30 14:20:02 -04001452 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001453 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1454 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001455 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001456 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001457 }
1458
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001459error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001460 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001461 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001462 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001463 EXTENT_DELALLOC | EXTENT_DEFRAG |
1464 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1465 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001466 PAGE_SET_WRITEBACK |
1467 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001468 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001469 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001470}
1471
Wang Shilong47059d92014-07-03 18:22:07 +08001472static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1473{
1474
1475 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1476 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1477 return 0;
1478
1479 /*
1480 * @defrag_bytes is a hint value, no spinlock held here,
1481 * if is not zero, it means the file is defragging.
1482 * Force cow if given extent needs to be defragged.
1483 */
1484 if (BTRFS_I(inode)->defrag_bytes &&
1485 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1486 EXTENT_DEFRAG, 0, NULL))
1487 return 1;
1488
1489 return 0;
1490}
1491
Chris Masond352ac62008-09-29 15:18:18 -04001492/*
1493 * extent_io.c call back to do delayed allocation processing
1494 */
Chris Masonc8b97812008-10-29 14:49:59 -04001495static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001496 u64 start, u64 end, int *page_started,
1497 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001498{
Chris Masonbe20aa92007-12-17 20:14:01 -05001499 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001500 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001501
Wang Shilong47059d92014-07-03 18:22:07 +08001502 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001503 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001504 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001505 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001506 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001507 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001508 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001509 ret = cow_file_range(inode, locked_page, start, end, end,
1510 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001511 } else {
1512 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1513 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001514 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001515 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001516 }
Chris Masonb888db22007-08-27 16:49:44 -04001517 return ret;
1518}
1519
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001520static void btrfs_split_extent_hook(struct inode *inode,
1521 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001522{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001523 u64 size;
1524
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001525 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001527 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001528
Josef Bacikdcab6a32015-02-11 15:08:59 -05001529 size = orig->end - orig->start + 1;
1530 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001531 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001532 u64 new_size;
1533
1534 /*
Josef Bacikba117212015-03-13 15:01:24 -04001535 * See the explanation in btrfs_merge_extent_hook, the same
1536 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001537 */
1538 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001539 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001540 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001541 num_extents += count_max_extents(new_size);
1542 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001543 return;
1544 }
1545
Josef Bacik9e0baf62011-07-15 15:16:44 +00001546 spin_lock(&BTRFS_I(inode)->lock);
1547 BTRFS_I(inode)->outstanding_extents++;
1548 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001549}
1550
1551/*
1552 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1553 * extents so we can keep track of new extents that are just merged onto old
1554 * extents, such as when we are doing sequential writes, so we can properly
1555 * account for the metadata space we'll need.
1556 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001557static void btrfs_merge_extent_hook(struct inode *inode,
1558 struct extent_state *new,
1559 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001560{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001561 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001562 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001563
Josef Bacik9ed74f22009-09-11 16:12:44 -04001564 /* not delalloc, ignore it */
1565 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001566 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001567
Josef Bacik8461a3d2015-03-13 15:12:08 -04001568 if (new->start > other->start)
1569 new_size = new->end - other->start + 1;
1570 else
1571 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001572
1573 /* we're not bigger than the max, unreserve the space and go */
1574 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1575 spin_lock(&BTRFS_I(inode)->lock);
1576 BTRFS_I(inode)->outstanding_extents--;
1577 spin_unlock(&BTRFS_I(inode)->lock);
1578 return;
1579 }
1580
1581 /*
Josef Bacikba117212015-03-13 15:01:24 -04001582 * We have to add up either side to figure out how many extents were
1583 * accounted for before we merged into one big extent. If the number of
1584 * extents we accounted for is <= the amount we need for the new range
1585 * then we can return, otherwise drop. Think of it like this
1586 *
1587 * [ 4k][MAX_SIZE]
1588 *
1589 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1590 * need 2 outstanding extents, on one side we have 1 and the other side
1591 * we have 1 so they are == and we can return. But in this case
1592 *
1593 * [MAX_SIZE+4k][MAX_SIZE+4k]
1594 *
1595 * Each range on their own accounts for 2 extents, but merged together
1596 * they are only 3 extents worth of accounting, so we need to drop in
1597 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001598 */
Josef Bacikba117212015-03-13 15:01:24 -04001599 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001600 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001601 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001602 num_extents += count_max_extents(old_size);
1603 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001604 return;
1605
Josef Bacik9e0baf62011-07-15 15:16:44 +00001606 spin_lock(&BTRFS_I(inode)->lock);
1607 BTRFS_I(inode)->outstanding_extents--;
1608 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001609}
1610
Miao Xieeb73c1b2013-05-15 07:48:22 +00001611static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1612 struct inode *inode)
1613{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001614 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1615
Miao Xieeb73c1b2013-05-15 07:48:22 +00001616 spin_lock(&root->delalloc_lock);
1617 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1618 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1619 &root->delalloc_inodes);
1620 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1621 &BTRFS_I(inode)->runtime_flags);
1622 root->nr_delalloc_inodes++;
1623 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001624 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001625 BUG_ON(!list_empty(&root->delalloc_root));
1626 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001627 &fs_info->delalloc_roots);
1628 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001629 }
1630 }
1631 spin_unlock(&root->delalloc_lock);
1632}
1633
1634static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001635 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001636{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001637 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001638
Miao Xieeb73c1b2013-05-15 07:48:22 +00001639 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001640 if (!list_empty(&inode->delalloc_inodes)) {
1641 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001642 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001643 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001644 root->nr_delalloc_inodes--;
1645 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001646 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001647 BUG_ON(list_empty(&root->delalloc_root));
1648 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001649 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001650 }
1651 }
1652 spin_unlock(&root->delalloc_lock);
1653}
1654
Chris Masond352ac62008-09-29 15:18:18 -04001655/*
1656 * extent_io.c set_bit_hook, used to track delayed allocation
1657 * bytes in this file, and to maintain the list of inodes that
1658 * have pending delalloc work to be done.
1659 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001660static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001661 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001662{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001663
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001664 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1665
Wang Shilong47059d92014-07-03 18:22:07 +08001666 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1667 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001668 /*
1669 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001670 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001671 * bit, which is only set or cleared with irqs on
1672 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001673 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001674 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001675 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001676 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001677
Josef Bacik9e0baf62011-07-15 15:16:44 +00001678 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001679 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001680 } else {
1681 spin_lock(&BTRFS_I(inode)->lock);
1682 BTRFS_I(inode)->outstanding_extents++;
1683 spin_unlock(&BTRFS_I(inode)->lock);
1684 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001685
Josef Bacik6a3891c2015-03-16 17:38:52 -04001686 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001687 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001688 return;
1689
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001690 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1691 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001692 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001693 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001694 if (*bits & EXTENT_DEFRAG)
1695 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001696 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001697 &BTRFS_I(inode)->runtime_flags))
1698 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001699 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001700 }
Chris Mason291d6732008-01-29 15:55:23 -05001701}
1702
Chris Masond352ac62008-09-29 15:18:18 -04001703/*
1704 * extent_io.c clear_bit_hook, see set_bit_hook for why
1705 */
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001706static void btrfs_clear_bit_hook(struct btrfs_inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001707 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001708 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001709{
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001710 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001711 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001712 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001713
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001714 spin_lock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001715 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001716 inode->defrag_bytes -= len;
1717 spin_unlock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001718
Chris Mason75eff682008-12-15 15:54:40 -05001719 /*
1720 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001721 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001722 * bit, which is only set or cleared with irqs on
1723 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001724 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001725 struct btrfs_root *root = inode->root;
1726 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001727
Josef Bacik9e0baf62011-07-15 15:16:44 +00001728 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001729 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001730 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001731 spin_lock(&inode->lock);
1732 inode->outstanding_extents -= num_extents;
1733 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001734 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001735
Josef Bacikb6d08f02013-09-27 14:57:43 -04001736 /*
1737 * We don't reserve metadata space for space cache inodes so we
1738 * don't need to call dellalloc_release_metadata if there is an
1739 * error.
1740 */
1741 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001742 root != fs_info->tree_root)
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001743 btrfs_delalloc_release_metadata(inode, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001744
Josef Bacik6a3891c2015-03-16 17:38:52 -04001745 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001746 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001747 return;
1748
Josef Bacik0cb59c92010-07-02 12:14:14 -04001749 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001750 && do_list && !(state->state & EXTENT_NORESERVE)
1751 && (*bits & (EXTENT_DO_ACCOUNTING |
1752 EXTENT_CLEAR_DATA_RESV)))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001753 btrfs_free_reserved_data_space_noquota(
1754 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001755 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001756
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001757 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1758 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001759 spin_lock(&inode->lock);
1760 inode->delalloc_bytes -= len;
1761 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001762 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001763 &inode->runtime_flags))
1764 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001765 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001766 }
Chris Mason291d6732008-01-29 15:55:23 -05001767}
1768
Chris Masond352ac62008-09-29 15:18:18 -04001769/*
1770 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1771 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001772 *
1773 * return 1 if page cannot be merged to bio
1774 * return 0 if page can be merged to bio
1775 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001776 */
Mike Christie81a75f672016-06-05 14:31:54 -05001777int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001778 size_t size, struct bio *bio,
1779 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001780{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001781 struct inode *inode = page->mapping->host;
1782 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001783 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001784 u64 length = 0;
1785 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001786 int ret;
1787
Chris Mason771ed682008-11-06 22:02:51 -05001788 if (bio_flags & EXTENT_BIO_COMPRESSED)
1789 return 0;
1790
Kent Overstreet4f024f32013-10-11 15:44:27 -07001791 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001792 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001793 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1794 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001795 if (ret < 0)
1796 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001797 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001798 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001799 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001800}
1801
Chris Masond352ac62008-09-29 15:18:18 -04001802/*
1803 * in order to insert checksums into the metadata in large chunks,
1804 * we wait until bio submission time. All the pages in the bio are
1805 * checksummed and sums are attached onto the ordered extent record.
1806 *
1807 * At IO completion time the cums attached on the ordered extent record
1808 * are inserted into the btree
1809 */
Mike Christie81a75f672016-06-05 14:31:54 -05001810static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1811 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001812 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001813{
Chris Mason065631f2008-02-20 12:07:25 -05001814 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001815
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001816 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001817 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001818 return 0;
1819}
Chris Masone0156402008-04-16 11:15:20 -04001820
Chris Mason4a69a412008-11-06 22:03:00 -05001821/*
1822 * in order to insert checksums into the metadata in large chunks,
1823 * we wait until bio submission time. All the pages in the bio are
1824 * checksummed and sums are attached onto the ordered extent record.
1825 *
1826 * At IO completion time the cums attached on the ordered extent record
1827 * are inserted into the btree
1828 */
Mike Christie81a75f672016-06-05 14:31:54 -05001829static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001830 int mirror_num, unsigned long bio_flags,
1831 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001832{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001833 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001834 int ret;
1835
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001836 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001837 if (ret) {
1838 bio->bi_error = ret;
1839 bio_endio(bio);
1840 }
Stefan Behrens61891922012-11-05 18:51:52 +01001841 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001842}
1843
Chris Masond352ac62008-09-29 15:18:18 -04001844/*
Chris Masoncad321a2008-12-17 14:51:42 -05001845 * extent_io.c submission hook. This does the right thing for csum calculation
1846 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001847 */
Mike Christie81a75f672016-06-05 14:31:54 -05001848static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001849 int mirror_num, unsigned long bio_flags,
1850 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001851{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001852 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001853 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301854 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001855 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001856 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001857 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001858
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001859 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001860
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001861 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301862 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001863
Mike Christie37226b22016-06-05 14:31:52 -05001864 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001865 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001866 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001867 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001868
Chris Masond20f7042008-12-08 16:58:54 -05001869 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001870 ret = btrfs_submit_compressed_read(inode, bio,
1871 mirror_num,
1872 bio_flags);
1873 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001874 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001875 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001876 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001877 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001878 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001879 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001880 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001881 /* csum items have already been cloned */
1882 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1883 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001884 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001885 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1886 bio_flags, bio_offset,
1887 __btrfs_submit_bio_start,
1888 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001889 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001890 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001891 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001892 if (ret)
1893 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001894 }
1895
Chris Mason0b86a832008-03-24 15:01:56 -04001896mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001897 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001898
1899out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001900 if (ret < 0) {
1901 bio->bi_error = ret;
1902 bio_endio(bio);
1903 }
Stefan Behrens61891922012-11-05 18:51:52 +01001904 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001905}
Chris Mason6885f302008-02-20 16:11:05 -05001906
Chris Masond352ac62008-09-29 15:18:18 -04001907/*
1908 * given a list of ordered sums record them in the inode. This happens
1909 * at IO completion time based on sums calculated at bio submission time.
1910 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001911static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01001912 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001913{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001914 struct btrfs_ordered_sum *sum;
1915
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001916 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001917 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001918 btrfs_csum_file_blocks(trans,
1919 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001920 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001921 }
1922 return 0;
1923}
1924
Josef Bacik2ac55d42010-02-03 19:33:23 +00001925int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001926 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04001927{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001928 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001929 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001930 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001931}
1932
Chris Masond352ac62008-09-29 15:18:18 -04001933/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001934struct btrfs_writepage_fixup {
1935 struct page *page;
1936 struct btrfs_work work;
1937};
1938
Christoph Hellwigb2950862008-12-02 09:54:17 -05001939static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001940{
1941 struct btrfs_writepage_fixup *fixup;
1942 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001943 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001944 struct page *page;
1945 struct inode *inode;
1946 u64 page_start;
1947 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001948 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001949
1950 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1951 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001952again:
Chris Mason247e7432008-07-17 12:53:51 -04001953 lock_page(page);
1954 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1955 ClearPageChecked(page);
1956 goto out_page;
1957 }
1958
1959 inode = page->mapping->host;
1960 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001961 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04001962
David Sterbaff13db42015-12-03 14:30:40 +01001963 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001964 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001965
1966 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001967 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001968 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001969
Nikolay Borisova776c6f2017-02-20 13:50:49 +02001970 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001971 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04001972 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001973 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1974 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001975 unlock_page(page);
1976 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001977 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001978 goto again;
1979 }
Chris Mason247e7432008-07-17 12:53:51 -04001980
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001981 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001982 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001983 if (ret) {
1984 mapping_set_error(page->mapping, ret);
1985 end_extent_writepage(page, ret, page_start, page_end);
1986 ClearPageChecked(page);
1987 goto out;
1988 }
1989
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001990 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
1991 0);
Chris Mason247e7432008-07-17 12:53:51 -04001992 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001993 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001994out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001995 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1996 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001997out_page:
1998 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001999 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002000 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002001}
2002
2003/*
2004 * There are a few paths in the higher layers of the kernel that directly
2005 * set the page dirty bit without asking the filesystem if it is a
2006 * good idea. This causes problems because we want to make sure COW
2007 * properly happens and the data=ordered rules are followed.
2008 *
Chris Masonc8b97812008-10-29 14:49:59 -04002009 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002010 * hasn't been properly setup for IO. We kick off an async process
2011 * to fix it up. The async helper will wait for ordered extents, set
2012 * the delalloc bit and make it safe to write the page.
2013 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002014static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002015{
2016 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002017 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002018 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002019
Chris Mason8b62b722009-09-02 16:53:46 -04002020 /* this page is properly in the ordered list */
2021 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002022 return 0;
2023
2024 if (PageChecked(page))
2025 return -EAGAIN;
2026
2027 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2028 if (!fixup)
2029 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002030
Chris Mason247e7432008-07-17 12:53:51 -04002031 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002032 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002033 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2034 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002035 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002036 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002037 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002038}
2039
Yan Zhengd899e052008-10-30 14:25:28 -04002040static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2041 struct inode *inode, u64 file_pos,
2042 u64 disk_bytenr, u64 disk_num_bytes,
2043 u64 num_bytes, u64 ram_bytes,
2044 u8 compression, u8 encryption,
2045 u16 other_encoding, int extent_type)
2046{
2047 struct btrfs_root *root = BTRFS_I(inode)->root;
2048 struct btrfs_file_extent_item *fi;
2049 struct btrfs_path *path;
2050 struct extent_buffer *leaf;
2051 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002052 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002053 int ret;
2054
2055 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002056 if (!path)
2057 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002058
Chris Masona1ed8352009-09-11 12:27:37 -04002059 /*
2060 * we may be replacing one extent in the tree with another.
2061 * The new extent is pinned in the extent map, and we don't want
2062 * to drop it from the cache until it is completely in the btree.
2063 *
2064 * So, tell btrfs_drop_extents to leave this extent in the cache.
2065 * the caller is expected to unpin it and allow it to be merged
2066 * with the others.
2067 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002068 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2069 file_pos + num_bytes, NULL, 0,
2070 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002071 if (ret)
2072 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002073
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002074 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002075 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002076 ins.offset = file_pos;
2077 ins.type = BTRFS_EXTENT_DATA_KEY;
2078
2079 path->leave_spinning = 1;
2080 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2081 sizeof(*fi));
2082 if (ret)
2083 goto out;
2084 }
Yan Zhengd899e052008-10-30 14:25:28 -04002085 leaf = path->nodes[0];
2086 fi = btrfs_item_ptr(leaf, path->slots[0],
2087 struct btrfs_file_extent_item);
2088 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2089 btrfs_set_file_extent_type(leaf, fi, extent_type);
2090 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2091 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2092 btrfs_set_file_extent_offset(leaf, fi, 0);
2093 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2094 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2095 btrfs_set_file_extent_compression(leaf, fi, compression);
2096 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2097 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002098
Yan Zhengd899e052008-10-30 14:25:28 -04002099 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002100 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002101
2102 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002103
2104 ins.objectid = disk_bytenr;
2105 ins.offset = disk_num_bytes;
2106 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002107 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002108 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002109 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002110 * Release the reserved range from inode dirty range map, as it is
2111 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002112 */
2113 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002114out:
Yan Zhengd899e052008-10-30 14:25:28 -04002115 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002116
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002117 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002118}
2119
Liu Bo38c227d2013-01-29 03:18:40 +00002120/* snapshot-aware defrag */
2121struct sa_defrag_extent_backref {
2122 struct rb_node node;
2123 struct old_sa_defrag_extent *old;
2124 u64 root_id;
2125 u64 inum;
2126 u64 file_pos;
2127 u64 extent_offset;
2128 u64 num_bytes;
2129 u64 generation;
2130};
2131
2132struct old_sa_defrag_extent {
2133 struct list_head list;
2134 struct new_sa_defrag_extent *new;
2135
2136 u64 extent_offset;
2137 u64 bytenr;
2138 u64 offset;
2139 u64 len;
2140 int count;
2141};
2142
2143struct new_sa_defrag_extent {
2144 struct rb_root root;
2145 struct list_head head;
2146 struct btrfs_path *path;
2147 struct inode *inode;
2148 u64 file_pos;
2149 u64 len;
2150 u64 bytenr;
2151 u64 disk_len;
2152 u8 compress_type;
2153};
2154
2155static int backref_comp(struct sa_defrag_extent_backref *b1,
2156 struct sa_defrag_extent_backref *b2)
2157{
2158 if (b1->root_id < b2->root_id)
2159 return -1;
2160 else if (b1->root_id > b2->root_id)
2161 return 1;
2162
2163 if (b1->inum < b2->inum)
2164 return -1;
2165 else if (b1->inum > b2->inum)
2166 return 1;
2167
2168 if (b1->file_pos < b2->file_pos)
2169 return -1;
2170 else if (b1->file_pos > b2->file_pos)
2171 return 1;
2172
2173 /*
2174 * [------------------------------] ===> (a range of space)
2175 * |<--->| |<---->| =============> (fs/file tree A)
2176 * |<---------------------------->| ===> (fs/file tree B)
2177 *
2178 * A range of space can refer to two file extents in one tree while
2179 * refer to only one file extent in another tree.
2180 *
2181 * So we may process a disk offset more than one time(two extents in A)
2182 * and locate at the same extent(one extent in B), then insert two same
2183 * backrefs(both refer to the extent in B).
2184 */
2185 return 0;
2186}
2187
2188static void backref_insert(struct rb_root *root,
2189 struct sa_defrag_extent_backref *backref)
2190{
2191 struct rb_node **p = &root->rb_node;
2192 struct rb_node *parent = NULL;
2193 struct sa_defrag_extent_backref *entry;
2194 int ret;
2195
2196 while (*p) {
2197 parent = *p;
2198 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2199
2200 ret = backref_comp(backref, entry);
2201 if (ret < 0)
2202 p = &(*p)->rb_left;
2203 else
2204 p = &(*p)->rb_right;
2205 }
2206
2207 rb_link_node(&backref->node, parent, p);
2208 rb_insert_color(&backref->node, root);
2209}
2210
2211/*
2212 * Note the backref might has changed, and in this case we just return 0.
2213 */
2214static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2215 void *ctx)
2216{
2217 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002218 struct old_sa_defrag_extent *old = ctx;
2219 struct new_sa_defrag_extent *new = old->new;
2220 struct btrfs_path *path = new->path;
2221 struct btrfs_key key;
2222 struct btrfs_root *root;
2223 struct sa_defrag_extent_backref *backref;
2224 struct extent_buffer *leaf;
2225 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002226 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002227 int slot;
2228 int ret;
2229 u64 extent_offset;
2230 u64 num_bytes;
2231
2232 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002233 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002234 return 0;
2235
2236 key.objectid = root_id;
2237 key.type = BTRFS_ROOT_ITEM_KEY;
2238 key.offset = (u64)-1;
2239
Liu Bo38c227d2013-01-29 03:18:40 +00002240 root = btrfs_read_fs_root_no_name(fs_info, &key);
2241 if (IS_ERR(root)) {
2242 if (PTR_ERR(root) == -ENOENT)
2243 return 0;
2244 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002245 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002246 inum, offset, root_id);
2247 return PTR_ERR(root);
2248 }
2249
2250 key.objectid = inum;
2251 key.type = BTRFS_EXTENT_DATA_KEY;
2252 if (offset > (u64)-1 << 32)
2253 key.offset = 0;
2254 else
2255 key.offset = offset;
2256
2257 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302258 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002259 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002260 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002261
2262 while (1) {
2263 cond_resched();
2264
2265 leaf = path->nodes[0];
2266 slot = path->slots[0];
2267
2268 if (slot >= btrfs_header_nritems(leaf)) {
2269 ret = btrfs_next_leaf(root, path);
2270 if (ret < 0) {
2271 goto out;
2272 } else if (ret > 0) {
2273 ret = 0;
2274 goto out;
2275 }
2276 continue;
2277 }
2278
2279 path->slots[0]++;
2280
2281 btrfs_item_key_to_cpu(leaf, &key, slot);
2282
2283 if (key.objectid > inum)
2284 goto out;
2285
2286 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2287 continue;
2288
2289 extent = btrfs_item_ptr(leaf, slot,
2290 struct btrfs_file_extent_item);
2291
2292 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2293 continue;
2294
Liu Boe68afa42013-07-01 22:13:26 +08002295 /*
2296 * 'offset' refers to the exact key.offset,
2297 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2298 * (key.offset - extent_offset).
2299 */
2300 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002301 continue;
2302
Liu Boe68afa42013-07-01 22:13:26 +08002303 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002304 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002305
Liu Bo38c227d2013-01-29 03:18:40 +00002306 if (extent_offset >= old->extent_offset + old->offset +
2307 old->len || extent_offset + num_bytes <=
2308 old->extent_offset + old->offset)
2309 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002310 break;
2311 }
2312
2313 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2314 if (!backref) {
2315 ret = -ENOENT;
2316 goto out;
2317 }
2318
2319 backref->root_id = root_id;
2320 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002321 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002322 backref->num_bytes = num_bytes;
2323 backref->extent_offset = extent_offset;
2324 backref->generation = btrfs_file_extent_generation(leaf, extent);
2325 backref->old = old;
2326 backref_insert(&new->root, backref);
2327 old->count++;
2328out:
2329 btrfs_release_path(path);
2330 WARN_ON(ret);
2331 return ret;
2332}
2333
2334static noinline bool record_extent_backrefs(struct btrfs_path *path,
2335 struct new_sa_defrag_extent *new)
2336{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002337 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002338 struct old_sa_defrag_extent *old, *tmp;
2339 int ret;
2340
2341 new->path = path;
2342
2343 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002344 ret = iterate_inodes_from_logical(old->bytenr +
2345 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002346 path, record_one_backref,
2347 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002348 if (ret < 0 && ret != -ENOENT)
2349 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002350
2351 /* no backref to be processed for this extent */
2352 if (!old->count) {
2353 list_del(&old->list);
2354 kfree(old);
2355 }
2356 }
2357
2358 if (list_empty(&new->head))
2359 return false;
2360
2361 return true;
2362}
2363
2364static int relink_is_mergable(struct extent_buffer *leaf,
2365 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002366 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002367{
Liu Bo116e0022013-08-02 16:30:40 +08002368 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002369 return 0;
2370
2371 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2372 return 0;
2373
Liu Bo116e0022013-08-02 16:30:40 +08002374 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2375 return 0;
2376
2377 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002378 btrfs_file_extent_other_encoding(leaf, fi))
2379 return 0;
2380
2381 return 1;
2382}
2383
2384/*
2385 * Note the backref might has changed, and in this case we just return 0.
2386 */
2387static noinline int relink_extent_backref(struct btrfs_path *path,
2388 struct sa_defrag_extent_backref *prev,
2389 struct sa_defrag_extent_backref *backref)
2390{
2391 struct btrfs_file_extent_item *extent;
2392 struct btrfs_file_extent_item *item;
2393 struct btrfs_ordered_extent *ordered;
2394 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002395 struct btrfs_root *root;
2396 struct btrfs_key key;
2397 struct extent_buffer *leaf;
2398 struct old_sa_defrag_extent *old = backref->old;
2399 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002400 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002401 struct inode *inode;
2402 struct extent_state *cached = NULL;
2403 int ret = 0;
2404 u64 start;
2405 u64 len;
2406 u64 lock_start;
2407 u64 lock_end;
2408 bool merge = false;
2409 int index;
2410
2411 if (prev && prev->root_id == backref->root_id &&
2412 prev->inum == backref->inum &&
2413 prev->file_pos + prev->num_bytes == backref->file_pos)
2414 merge = true;
2415
2416 /* step 1: get root */
2417 key.objectid = backref->root_id;
2418 key.type = BTRFS_ROOT_ITEM_KEY;
2419 key.offset = (u64)-1;
2420
Liu Bo38c227d2013-01-29 03:18:40 +00002421 index = srcu_read_lock(&fs_info->subvol_srcu);
2422
2423 root = btrfs_read_fs_root_no_name(fs_info, &key);
2424 if (IS_ERR(root)) {
2425 srcu_read_unlock(&fs_info->subvol_srcu, index);
2426 if (PTR_ERR(root) == -ENOENT)
2427 return 0;
2428 return PTR_ERR(root);
2429 }
Liu Bo38c227d2013-01-29 03:18:40 +00002430
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002431 if (btrfs_root_readonly(root)) {
2432 srcu_read_unlock(&fs_info->subvol_srcu, index);
2433 return 0;
2434 }
2435
Liu Bo38c227d2013-01-29 03:18:40 +00002436 /* step 2: get inode */
2437 key.objectid = backref->inum;
2438 key.type = BTRFS_INODE_ITEM_KEY;
2439 key.offset = 0;
2440
2441 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2442 if (IS_ERR(inode)) {
2443 srcu_read_unlock(&fs_info->subvol_srcu, index);
2444 return 0;
2445 }
2446
2447 srcu_read_unlock(&fs_info->subvol_srcu, index);
2448
2449 /* step 3: relink backref */
2450 lock_start = backref->file_pos;
2451 lock_end = backref->file_pos + backref->num_bytes - 1;
2452 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002453 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002454
2455 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2456 if (ordered) {
2457 btrfs_put_ordered_extent(ordered);
2458 goto out_unlock;
2459 }
2460
2461 trans = btrfs_join_transaction(root);
2462 if (IS_ERR(trans)) {
2463 ret = PTR_ERR(trans);
2464 goto out_unlock;
2465 }
2466
2467 key.objectid = backref->inum;
2468 key.type = BTRFS_EXTENT_DATA_KEY;
2469 key.offset = backref->file_pos;
2470
2471 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2472 if (ret < 0) {
2473 goto out_free_path;
2474 } else if (ret > 0) {
2475 ret = 0;
2476 goto out_free_path;
2477 }
2478
2479 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2480 struct btrfs_file_extent_item);
2481
2482 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2483 backref->generation)
2484 goto out_free_path;
2485
2486 btrfs_release_path(path);
2487
2488 start = backref->file_pos;
2489 if (backref->extent_offset < old->extent_offset + old->offset)
2490 start += old->extent_offset + old->offset -
2491 backref->extent_offset;
2492
2493 len = min(backref->extent_offset + backref->num_bytes,
2494 old->extent_offset + old->offset + old->len);
2495 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2496
2497 ret = btrfs_drop_extents(trans, root, inode, start,
2498 start + len, 1);
2499 if (ret)
2500 goto out_free_path;
2501again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002502 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002503 key.type = BTRFS_EXTENT_DATA_KEY;
2504 key.offset = start;
2505
Liu Boa09a0a72013-03-11 09:20:58 +00002506 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002507 if (merge) {
2508 struct btrfs_file_extent_item *fi;
2509 u64 extent_len;
2510 struct btrfs_key found_key;
2511
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002512 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002513 if (ret < 0)
2514 goto out_free_path;
2515
2516 path->slots[0]--;
2517 leaf = path->nodes[0];
2518 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2519
2520 fi = btrfs_item_ptr(leaf, path->slots[0],
2521 struct btrfs_file_extent_item);
2522 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2523
Liu Bo116e0022013-08-02 16:30:40 +08002524 if (extent_len + found_key.offset == start &&
2525 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002526 btrfs_set_file_extent_num_bytes(leaf, fi,
2527 extent_len + len);
2528 btrfs_mark_buffer_dirty(leaf);
2529 inode_add_bytes(inode, len);
2530
2531 ret = 1;
2532 goto out_free_path;
2533 } else {
2534 merge = false;
2535 btrfs_release_path(path);
2536 goto again;
2537 }
2538 }
2539
2540 ret = btrfs_insert_empty_item(trans, root, path, &key,
2541 sizeof(*extent));
2542 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002543 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002544 goto out_free_path;
2545 }
2546
2547 leaf = path->nodes[0];
2548 item = btrfs_item_ptr(leaf, path->slots[0],
2549 struct btrfs_file_extent_item);
2550 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2551 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2552 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2553 btrfs_set_file_extent_num_bytes(leaf, item, len);
2554 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2555 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2556 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2557 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2558 btrfs_set_file_extent_encryption(leaf, item, 0);
2559 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2560
2561 btrfs_mark_buffer_dirty(leaf);
2562 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002563 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002564
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002565 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002566 new->disk_len, 0,
2567 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002568 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002569 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002570 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002571 goto out_free_path;
2572 }
2573
2574 ret = 1;
2575out_free_path:
2576 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002577 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002578 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002579out_unlock:
2580 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2581 &cached, GFP_NOFS);
2582 iput(inode);
2583 return ret;
2584}
2585
Liu Bo6f519562013-10-29 10:45:05 +08002586static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2587{
2588 struct old_sa_defrag_extent *old, *tmp;
2589
2590 if (!new)
2591 return;
2592
2593 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002594 kfree(old);
2595 }
2596 kfree(new);
2597}
2598
Liu Bo38c227d2013-01-29 03:18:40 +00002599static void relink_file_extents(struct new_sa_defrag_extent *new)
2600{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002601 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002602 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002603 struct sa_defrag_extent_backref *backref;
2604 struct sa_defrag_extent_backref *prev = NULL;
2605 struct inode *inode;
2606 struct btrfs_root *root;
2607 struct rb_node *node;
2608 int ret;
2609
2610 inode = new->inode;
2611 root = BTRFS_I(inode)->root;
2612
2613 path = btrfs_alloc_path();
2614 if (!path)
2615 return;
2616
2617 if (!record_extent_backrefs(path, new)) {
2618 btrfs_free_path(path);
2619 goto out;
2620 }
2621 btrfs_release_path(path);
2622
2623 while (1) {
2624 node = rb_first(&new->root);
2625 if (!node)
2626 break;
2627 rb_erase(node, &new->root);
2628
2629 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2630
2631 ret = relink_extent_backref(path, prev, backref);
2632 WARN_ON(ret < 0);
2633
2634 kfree(prev);
2635
2636 if (ret == 1)
2637 prev = backref;
2638 else
2639 prev = NULL;
2640 cond_resched();
2641 }
2642 kfree(prev);
2643
2644 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002645out:
Liu Bo6f519562013-10-29 10:45:05 +08002646 free_sa_defrag_extent(new);
2647
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002648 atomic_dec(&fs_info->defrag_running);
2649 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002650}
2651
2652static struct new_sa_defrag_extent *
2653record_old_file_extents(struct inode *inode,
2654 struct btrfs_ordered_extent *ordered)
2655{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002656 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002657 struct btrfs_root *root = BTRFS_I(inode)->root;
2658 struct btrfs_path *path;
2659 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002660 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002661 struct new_sa_defrag_extent *new;
2662 int ret;
2663
2664 new = kmalloc(sizeof(*new), GFP_NOFS);
2665 if (!new)
2666 return NULL;
2667
2668 new->inode = inode;
2669 new->file_pos = ordered->file_offset;
2670 new->len = ordered->len;
2671 new->bytenr = ordered->start;
2672 new->disk_len = ordered->disk_len;
2673 new->compress_type = ordered->compress_type;
2674 new->root = RB_ROOT;
2675 INIT_LIST_HEAD(&new->head);
2676
2677 path = btrfs_alloc_path();
2678 if (!path)
2679 goto out_kfree;
2680
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002681 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002682 key.type = BTRFS_EXTENT_DATA_KEY;
2683 key.offset = new->file_pos;
2684
2685 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2686 if (ret < 0)
2687 goto out_free_path;
2688 if (ret > 0 && path->slots[0] > 0)
2689 path->slots[0]--;
2690
2691 /* find out all the old extents for the file range */
2692 while (1) {
2693 struct btrfs_file_extent_item *extent;
2694 struct extent_buffer *l;
2695 int slot;
2696 u64 num_bytes;
2697 u64 offset;
2698 u64 end;
2699 u64 disk_bytenr;
2700 u64 extent_offset;
2701
2702 l = path->nodes[0];
2703 slot = path->slots[0];
2704
2705 if (slot >= btrfs_header_nritems(l)) {
2706 ret = btrfs_next_leaf(root, path);
2707 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002708 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002709 else if (ret > 0)
2710 break;
2711 continue;
2712 }
2713
2714 btrfs_item_key_to_cpu(l, &key, slot);
2715
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002716 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002717 break;
2718 if (key.type != BTRFS_EXTENT_DATA_KEY)
2719 break;
2720 if (key.offset >= new->file_pos + new->len)
2721 break;
2722
2723 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2724
2725 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2726 if (key.offset + num_bytes < new->file_pos)
2727 goto next;
2728
2729 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2730 if (!disk_bytenr)
2731 goto next;
2732
2733 extent_offset = btrfs_file_extent_offset(l, extent);
2734
2735 old = kmalloc(sizeof(*old), GFP_NOFS);
2736 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002737 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002738
2739 offset = max(new->file_pos, key.offset);
2740 end = min(new->file_pos + new->len, key.offset + num_bytes);
2741
2742 old->bytenr = disk_bytenr;
2743 old->extent_offset = extent_offset;
2744 old->offset = offset - key.offset;
2745 old->len = end - offset;
2746 old->new = new;
2747 old->count = 0;
2748 list_add_tail(&old->list, &new->head);
2749next:
2750 path->slots[0]++;
2751 cond_resched();
2752 }
2753
2754 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002755 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002756
2757 return new;
2758
Liu Bo38c227d2013-01-29 03:18:40 +00002759out_free_path:
2760 btrfs_free_path(path);
2761out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002762 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002763 return NULL;
2764}
2765
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002766static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002767 u64 start, u64 len)
2768{
2769 struct btrfs_block_group_cache *cache;
2770
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002771 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002772 ASSERT(cache);
2773
2774 spin_lock(&cache->lock);
2775 cache->delalloc_bytes -= len;
2776 spin_unlock(&cache->lock);
2777
2778 btrfs_put_block_group(cache);
2779}
2780
Chris Masond352ac62008-09-29 15:18:18 -04002781/* as ordered data IO finishes, this gets called so we can finish
2782 * an ordered extent if the range of bytes in the file it covers are
2783 * fully written.
2784 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002785static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002786{
Josef Bacik5fd02042012-05-02 14:00:54 -04002787 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002788 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002789 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002790 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002791 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002792 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002793 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002794 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002795 int ret = 0;
2796 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002797 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002798 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002799
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002800 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002801
Josef Bacik5fd02042012-05-02 14:00:54 -04002802 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2803 ret = -EIO;
2804 goto out;
2805 }
2806
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002807 btrfs_free_io_failure_record(BTRFS_I(inode),
2808 ordered_extent->file_offset,
2809 ordered_extent->file_offset +
2810 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002811
Josef Bacik77cef2e2013-08-29 13:57:21 -04002812 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2813 truncated = true;
2814 logical_len = ordered_extent->truncated_len;
2815 /* Truncated the entire extent, don't bother adding */
2816 if (!logical_len)
2817 goto out;
2818 }
2819
Yan, Zhengc2167752009-11-12 09:34:21 +00002820 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002821 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002822
2823 /*
2824 * For mwrite(mmap + memset to write) case, we still reserve
2825 * space for NOCOW range.
2826 * As NOCOW won't cause a new delayed ref, just free the space
2827 */
2828 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2829 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002830 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2831 if (nolock)
2832 trans = btrfs_join_transaction_nolock(root);
2833 else
2834 trans = btrfs_join_transaction(root);
2835 if (IS_ERR(trans)) {
2836 ret = PTR_ERR(trans);
2837 trans = NULL;
2838 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002839 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002840 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002841 ret = btrfs_update_inode_fallback(trans, root, inode);
2842 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002843 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002844 goto out;
2845 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002846
Josef Bacik2ac55d42010-02-03 19:33:23 +00002847 lock_extent_bits(io_tree, ordered_extent->file_offset,
2848 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002849 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002850
Liu Bo38c227d2013-01-29 03:18:40 +00002851 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2852 ordered_extent->file_offset + ordered_extent->len - 1,
2853 EXTENT_DEFRAG, 1, cached_state);
2854 if (ret) {
2855 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002856 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002857 /* the inode is shared */
2858 new = record_old_file_extents(inode, ordered_extent);
2859
2860 clear_extent_bit(io_tree, ordered_extent->file_offset,
2861 ordered_extent->file_offset + ordered_extent->len - 1,
2862 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2863 }
2864
Josef Bacik0cb59c92010-07-02 12:14:14 -04002865 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002866 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002867 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002868 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002869 if (IS_ERR(trans)) {
2870 ret = PTR_ERR(trans);
2871 trans = NULL;
2872 goto out_unlock;
2873 }
Chris Masona79b7d42014-05-22 16:18:52 -07002874
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002875 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002876
Chris Masonc8b97812008-10-29 14:49:59 -04002877 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002878 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002879 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002880 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002881 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002882 ordered_extent->file_offset,
2883 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002884 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002885 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002886 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002887 ret = insert_reserved_file_extent(trans, inode,
2888 ordered_extent->file_offset,
2889 ordered_extent->start,
2890 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002891 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002892 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002893 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002894 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002895 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002896 ordered_extent->start,
2897 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002898 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002899 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2900 ordered_extent->file_offset, ordered_extent->len,
2901 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002902 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002903 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002904 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002905 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002906
David Sterbadf9f6282017-02-10 19:35:37 +01002907 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002908
Josef Bacik6c760c02012-11-09 10:53:21 -05002909 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2910 ret = btrfs_update_inode_fallback(trans, root, inode);
2911 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002912 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002913 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002914 }
2915 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002916out_unlock:
2917 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2918 ordered_extent->file_offset +
2919 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002920out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002921 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02002922 btrfs_delalloc_release_metadata(BTRFS_I(inode),
2923 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002924 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002925 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002926
Josef Bacik77cef2e2013-08-29 13:57:21 -04002927 if (ret || truncated) {
2928 u64 start, end;
2929
2930 if (truncated)
2931 start = ordered_extent->file_offset + logical_len;
2932 else
2933 start = ordered_extent->file_offset;
2934 end = ordered_extent->file_offset + ordered_extent->len - 1;
2935 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2936
2937 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02002938 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002939
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002940 /*
2941 * If the ordered extent had an IOERR or something else went
2942 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002943 * back to the allocator. We only free the extent in the
2944 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002945 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002946 if ((ret || !logical_len) &&
2947 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002948 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002949 btrfs_free_reserved_extent(fs_info,
2950 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002951 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002952 }
2953
2954
Josef Bacik5fd02042012-05-02 14:00:54 -04002955 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002956 * This needs to be done to make sure anybody waiting knows we are done
2957 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002958 */
2959 btrfs_remove_ordered_extent(inode, ordered_extent);
2960
Liu Bo38c227d2013-01-29 03:18:40 +00002961 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002962 if (new) {
2963 if (ret) {
2964 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002965 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08002966 } else {
2967 relink_file_extents(new);
2968 }
2969 }
Liu Bo38c227d2013-01-29 03:18:40 +00002970
Chris Masone6dcd2d2008-07-17 12:53:50 -04002971 /* once for us */
2972 btrfs_put_ordered_extent(ordered_extent);
2973 /* once for the tree */
2974 btrfs_put_ordered_extent(ordered_extent);
2975
Josef Bacik5fd02042012-05-02 14:00:54 -04002976 return ret;
2977}
2978
2979static void finish_ordered_fn(struct btrfs_work *work)
2980{
2981 struct btrfs_ordered_extent *ordered_extent;
2982 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2983 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002984}
2985
Christoph Hellwigb2950862008-12-02 09:54:17 -05002986static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002987 struct extent_state *state, int uptodate)
2988{
Josef Bacik5fd02042012-05-02 14:00:54 -04002989 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002990 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002991 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002992 struct btrfs_workqueue *wq;
2993 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002994
liubo1abe9b82011-03-24 11:18:59 +00002995 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2996
Chris Mason8b62b722009-09-02 16:53:46 -04002997 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002998 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2999 end - start + 1, uptodate))
3000 return 0;
3001
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003002 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003003 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003004 func = btrfs_freespace_write_helper;
3005 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003006 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003007 func = btrfs_endio_write_helper;
3008 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003009
Liu Bo9e0af232014-08-15 23:36:53 +08003010 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3011 NULL);
3012 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003013
3014 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003015}
3016
Miao Xiedc380ae2014-09-12 18:43:55 +08003017static int __readpage_endio_check(struct inode *inode,
3018 struct btrfs_io_bio *io_bio,
3019 int icsum, struct page *page,
3020 int pgoff, u64 start, size_t len)
3021{
3022 char *kaddr;
3023 u32 csum_expected;
3024 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003025
3026 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3027
3028 kaddr = kmap_atomic(page);
3029 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003030 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003031 if (csum != csum_expected)
3032 goto zeroit;
3033
3034 kunmap_atomic(kaddr);
3035 return 0;
3036zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003037 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003038 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003039 memset(kaddr + pgoff, 1, len);
3040 flush_dcache_page(page);
3041 kunmap_atomic(kaddr);
3042 if (csum_expected == 0)
3043 return 0;
3044 return -EIO;
3045}
3046
Chris Masond352ac62008-09-29 15:18:18 -04003047/*
Chris Masond352ac62008-09-29 15:18:18 -04003048 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003049 * if there's a match, we allow the bio to finish. If not, the code in
3050 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003051 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003052static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3053 u64 phy_offset, struct page *page,
3054 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003055{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003056 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003057 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003058 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003059 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003060
Chris Masond20f7042008-12-08 16:58:54 -05003061 if (PageChecked(page)) {
3062 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003063 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003064 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003065
3066 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003067 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003068
Yan Zheng17d217f2008-12-12 10:03:38 -05003069 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003070 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003071 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003072 return 0;
3073 }
3074
Miao Xiefacc8a222013-07-25 19:22:34 +08003075 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003076 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3077 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003078}
Chris Masonb888db22007-08-27 16:49:44 -04003079
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003080void btrfs_add_delayed_iput(struct inode *inode)
3081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003082 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003083 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003084
3085 if (atomic_add_unless(&inode->i_count, -1, 1))
3086 return;
3087
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003088 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003089 if (binode->delayed_iput_count == 0) {
3090 ASSERT(list_empty(&binode->delayed_iput));
3091 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3092 } else {
3093 binode->delayed_iput_count++;
3094 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003095 spin_unlock(&fs_info->delayed_iput_lock);
3096}
3097
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003098void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003099{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003100
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003101 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003102 while (!list_empty(&fs_info->delayed_iputs)) {
3103 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003104
David Sterba8089fe62015-11-19 14:15:51 +01003105 inode = list_first_entry(&fs_info->delayed_iputs,
3106 struct btrfs_inode, delayed_iput);
3107 if (inode->delayed_iput_count) {
3108 inode->delayed_iput_count--;
3109 list_move_tail(&inode->delayed_iput,
3110 &fs_info->delayed_iputs);
3111 } else {
3112 list_del_init(&inode->delayed_iput);
3113 }
3114 spin_unlock(&fs_info->delayed_iput_lock);
3115 iput(&inode->vfs_inode);
3116 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003117 }
David Sterba8089fe62015-11-19 14:15:51 +01003118 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003119}
3120
Yan, Zhengd68fc572010-05-16 10:49:58 -04003121/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003122 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003123 * files in the subvolume, it removes orphan item and frees block_rsv
3124 * structure.
3125 */
3126void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3127 struct btrfs_root *root)
3128{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003129 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003130 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003131 int ret;
3132
Josef Bacik8a35d952012-05-23 14:26:42 -04003133 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003134 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3135 return;
3136
Josef Bacik90290e12011-12-02 15:44:12 -05003137 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003138 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003139 spin_unlock(&root->orphan_lock);
3140 return;
3141 }
3142
3143 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3144 spin_unlock(&root->orphan_lock);
3145 return;
3146 }
3147
3148 block_rsv = root->orphan_block_rsv;
3149 root->orphan_block_rsv = NULL;
3150 spin_unlock(&root->orphan_lock);
3151
Miao Xie27cdeb72014-04-02 19:51:05 +08003152 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003153 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003154 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003155 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003156 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003157 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003158 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003159 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3160 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003161 }
3162
Josef Bacik90290e12011-12-02 15:44:12 -05003163 if (block_rsv) {
3164 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003165 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003166 }
3167}
3168
3169/*
Josef Bacik7b128762008-07-24 12:17:14 -04003170 * This creates an orphan entry for the given inode in case something goes
3171 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003172 *
3173 * NOTE: caller of this function should reserve 5 units of metadata for
3174 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003175 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003176int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3177 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003178{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003179 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3180 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003181 struct btrfs_block_rsv *block_rsv = NULL;
3182 int reserve = 0;
3183 int insert = 0;
3184 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003185
Yan, Zhengd68fc572010-05-16 10:49:58 -04003186 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003187 block_rsv = btrfs_alloc_block_rsv(fs_info,
3188 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003189 if (!block_rsv)
3190 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003191 }
3192
Yan, Zhengd68fc572010-05-16 10:49:58 -04003193 spin_lock(&root->orphan_lock);
3194 if (!root->orphan_block_rsv) {
3195 root->orphan_block_rsv = block_rsv;
3196 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003197 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003198 block_rsv = NULL;
3199 }
Josef Bacik7b128762008-07-24 12:17:14 -04003200
Josef Bacik8a35d952012-05-23 14:26:42 -04003201 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003202 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003203#if 0
3204 /*
3205 * For proper ENOSPC handling, we should do orphan
3206 * cleanup when mounting. But this introduces backward
3207 * compatibility issue.
3208 */
3209 if (!xchg(&root->orphan_item_inserted, 1))
3210 insert = 2;
3211 else
3212 insert = 1;
3213#endif
3214 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003215 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003216 }
Josef Bacik7b128762008-07-24 12:17:14 -04003217
Josef Bacik72ac3c02012-05-23 14:13:11 -04003218 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003219 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003220 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003221 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003222
Yan, Zhengd68fc572010-05-16 10:49:58 -04003223 /* grab metadata reservation from transaction handle */
3224 if (reserve) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003225 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003226 ASSERT(!ret);
3227 if (ret) {
3228 atomic_dec(&root->orphan_inodes);
3229 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003230 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003231 if (insert)
3232 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003233 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003234 return ret;
3235 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003236 }
3237
3238 /* insert an orphan item to track this unlinked/truncated file */
3239 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003240 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003241 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003242 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003243 if (reserve) {
3244 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003245 &inode->runtime_flags);
3246 btrfs_orphan_release_metadata(inode);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003247 }
3248 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003249 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003250 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003251 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003252 return ret;
3253 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003254 }
3255 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003256 }
3257
3258 /* insert an orphan item to track subvolume contains orphan files */
3259 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003260 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003261 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003262 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003263 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003264 return ret;
3265 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003266 }
3267 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003268}
3269
3270/*
3271 * We have done the truncate/delete so we can go ahead and remove the orphan
3272 * item for this particular inode.
3273 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003274static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003275 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003276{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003277 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 int delete_item = 0;
3279 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003280 int ret = 0;
3281
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003283 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003284 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003285 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003286
Josef Bacik72ac3c02012-05-23 14:13:11 -04003287 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003288 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003289 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290 spin_unlock(&root->orphan_lock);
3291
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003292 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003293 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003294 if (trans)
3295 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003296 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003297 }
Josef Bacik7b128762008-07-24 12:17:14 -04003298
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003299 if (release_rsv)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003300 btrfs_orphan_release_metadata(inode);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003301
Josef Bacik4ef31a42013-08-13 14:10:08 -04003302 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003303}
3304
3305/*
3306 * this cleans up any orphans that may be left on the list from the last use
3307 * of this root.
3308 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003309int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003310{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003311 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003312 struct btrfs_path *path;
3313 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003314 struct btrfs_key key, found_key;
3315 struct btrfs_trans_handle *trans;
3316 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003317 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003318 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3319
Yan, Zhengd68fc572010-05-16 10:49:58 -04003320 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003321 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003322
3323 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003324 if (!path) {
3325 ret = -ENOMEM;
3326 goto out;
3327 }
David Sterbae4058b52015-11-27 16:31:35 +01003328 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003329
3330 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003331 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003332 key.offset = (u64)-1;
3333
Josef Bacik7b128762008-07-24 12:17:14 -04003334 while (1) {
3335 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003336 if (ret < 0)
3337 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003338
3339 /*
3340 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003341 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003342 * find the key and see if we have stuff that matches
3343 */
3344 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003345 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003346 if (path->slots[0] == 0)
3347 break;
3348 path->slots[0]--;
3349 }
3350
3351 /* pull out the item */
3352 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003353 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3354
3355 /* make sure the item matches what we want */
3356 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3357 break;
David Sterba962a2982014-06-04 18:41:45 +02003358 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003359 break;
3360
3361 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003362 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003363
3364 /*
3365 * this is where we are basically btrfs_lookup, without the
3366 * crossing root thing. we store the inode number in the
3367 * offset of the orphan item.
3368 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003369
3370 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003371 btrfs_err(fs_info,
3372 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003373 ret = -EINVAL;
3374 goto out;
3375 }
3376
3377 last_objectid = found_key.offset;
3378
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003379 found_key.objectid = found_key.offset;
3380 found_key.type = BTRFS_INODE_ITEM_KEY;
3381 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003382 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303383 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003384 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003385 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003386
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003387 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003388 struct btrfs_root *dead_root;
3389 struct btrfs_fs_info *fs_info = root->fs_info;
3390 int is_dead_root = 0;
3391
3392 /*
3393 * this is an orphan in the tree root. Currently these
3394 * could come from 2 sources:
3395 * a) a snapshot deletion in progress
3396 * b) a free space cache inode
3397 * We need to distinguish those two, as the snapshot
3398 * orphan must not get deleted.
3399 * find_dead_roots already ran before us, so if this
3400 * is a snapshot deletion, we should find the root
3401 * in the dead_roots list
3402 */
3403 spin_lock(&fs_info->trans_lock);
3404 list_for_each_entry(dead_root, &fs_info->dead_roots,
3405 root_list) {
3406 if (dead_root->root_key.objectid ==
3407 found_key.objectid) {
3408 is_dead_root = 1;
3409 break;
3410 }
3411 }
3412 spin_unlock(&fs_info->trans_lock);
3413 if (is_dead_root) {
3414 /* prevent this orphan from being found again */
3415 key.offset = found_key.objectid - 1;
3416 continue;
3417 }
3418 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003419 /*
3420 * Inode is already gone but the orphan item is still there,
3421 * kill the orphan item.
3422 */
Filipe Manana67710892016-06-06 11:51:25 +01003423 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003424 trans = btrfs_start_transaction(root, 1);
3425 if (IS_ERR(trans)) {
3426 ret = PTR_ERR(trans);
3427 goto out;
3428 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003429 btrfs_debug(fs_info, "auto deleting %Lu",
3430 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003431 ret = btrfs_del_orphan_item(trans, root,
3432 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003433 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003434 if (ret)
3435 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003436 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003437 }
Josef Bacik7b128762008-07-24 12:17:14 -04003438
Josef Bacik7b128762008-07-24 12:17:14 -04003439 /*
3440 * add this inode to the orphan list so btrfs_orphan_del does
3441 * the proper thing when we hit it
3442 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003443 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3444 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003445 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003446
Josef Bacik7b128762008-07-24 12:17:14 -04003447 /* if we have links, this was a truncate, lets do that */
3448 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303449 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003450 iput(inode);
3451 continue;
3452 }
Josef Bacik7b128762008-07-24 12:17:14 -04003453 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003454
3455 /* 1 for the orphan item deletion. */
3456 trans = btrfs_start_transaction(root, 1);
3457 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003458 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003459 ret = PTR_ERR(trans);
3460 goto out;
3461 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003462 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003463 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003464 if (ret) {
3465 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003466 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003467 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003468
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003469 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003470 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003471 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003472 } else {
3473 nr_unlink++;
3474 }
3475
3476 /* this will do delete_inode and everything for us */
3477 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003478 if (ret)
3479 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003480 }
Miao Xie3254c872011-11-10 20:45:05 -05003481 /* release the path since we're done with it */
3482 btrfs_release_path(path);
3483
Yan, Zhengd68fc572010-05-16 10:49:58 -04003484 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3485
3486 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003487 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003488 (u64)-1);
3489
Miao Xie27cdeb72014-04-02 19:51:05 +08003490 if (root->orphan_block_rsv ||
3491 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003492 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003493 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003494 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003495 }
Josef Bacik7b128762008-07-24 12:17:14 -04003496
3497 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003498 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003499 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003500 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003501
3502out:
3503 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003504 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003505 btrfs_free_path(path);
3506 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003507}
3508
Chris Masond352ac62008-09-29 15:18:18 -04003509/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003510 * very simple check to peek ahead in the leaf looking for xattrs. If we
3511 * don't find any xattrs, we know there can't be any acls.
3512 *
3513 * slot is the slot the inode is in, objectid is the objectid of the inode
3514 */
3515static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003516 int slot, u64 objectid,
3517 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003518{
3519 u32 nritems = btrfs_header_nritems(leaf);
3520 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003521 static u64 xattr_access = 0;
3522 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003523 int scanned = 0;
3524
Josef Bacikf23b5a52013-06-19 10:16:26 -04003525 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003526 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3527 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3528 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3529 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003530 }
3531
Chris Mason46a53cc2009-04-27 11:47:50 -04003532 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003533 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003534 while (slot < nritems) {
3535 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3536
3537 /* we found a different objectid, there must not be acls */
3538 if (found_key.objectid != objectid)
3539 return 0;
3540
3541 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003542 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003543 if (*first_xattr_slot == -1)
3544 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003545 if (found_key.offset == xattr_access ||
3546 found_key.offset == xattr_default)
3547 return 1;
3548 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003549
3550 /*
3551 * we found a key greater than an xattr key, there can't
3552 * be any acls later on
3553 */
3554 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3555 return 0;
3556
3557 slot++;
3558 scanned++;
3559
3560 /*
3561 * it goes inode, inode backrefs, xattrs, extents,
3562 * so if there are a ton of hard links to an inode there can
3563 * be a lot of backrefs. Don't waste time searching too hard,
3564 * this is just an optimization
3565 */
3566 if (scanned >= 8)
3567 break;
3568 }
3569 /* we hit the end of the leaf before we found an xattr or
3570 * something larger than an xattr. We have to assume the inode
3571 * has acls
3572 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003573 if (*first_xattr_slot == -1)
3574 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003575 return 1;
3576}
3577
3578/*
Chris Masond352ac62008-09-29 15:18:18 -04003579 * read an inode from the btree into the in-memory inode
3580 */
Filipe Manana67710892016-06-06 11:51:25 +01003581static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003582{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003583 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003584 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003585 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003586 struct btrfs_inode_item *inode_item;
3587 struct btrfs_root *root = BTRFS_I(inode)->root;
3588 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003589 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003590 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003591 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003592 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003593 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003594 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003595
3596 ret = btrfs_fill_inode(inode, &rdev);
3597 if (!ret)
3598 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003599
3600 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003601 if (!path) {
3602 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003603 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003604 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003605
Chris Mason39279cc2007-06-12 06:35:45 -04003606 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003607
Chris Mason39279cc2007-06-12 06:35:45 -04003608 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003609 if (ret) {
3610 if (ret > 0)
3611 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003612 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003613 }
Chris Mason39279cc2007-06-12 06:35:45 -04003614
Chris Mason5f39d392007-10-15 16:14:19 -04003615 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003616
3617 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003618 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003619
Chris Mason5f39d392007-10-15 16:14:19 -04003620 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3621 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003622 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003623 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003624 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3625 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003626 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003627
David Sterbaa937b972014-12-12 17:39:12 +01003628 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3629 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003630
David Sterbaa937b972014-12-12 17:39:12 +01003631 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3632 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003633
David Sterbaa937b972014-12-12 17:39:12 +01003634 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3635 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003636
chandan r9cc97d62012-07-04 12:48:07 +05303637 BTRFS_I(inode)->i_otime.tv_sec =
3638 btrfs_timespec_sec(leaf, &inode_item->otime);
3639 BTRFS_I(inode)->i_otime.tv_nsec =
3640 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003641
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003642 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003643 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003644 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3645
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003646 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003647 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003648 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003649 rdev = btrfs_inode_rdev(leaf, inode_item);
3650
Josef Bacikaec74772008-07-24 12:12:38 -04003651 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003652 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003653
3654cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003655 /*
3656 * If we were modified in the current generation and evicted from memory
3657 * and then re-read we need to do a full sync since we don't have any
3658 * idea about which extents were modified before we were evicted from
3659 * cache.
3660 *
3661 * This is required for both inode re-read from disk and delayed inode
3662 * in delayed_nodes_tree.
3663 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003664 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003665 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3666 &BTRFS_I(inode)->runtime_flags);
3667
Filipe Mananabde6c242015-07-24 00:00:19 +01003668 /*
3669 * We don't persist the id of the transaction where an unlink operation
3670 * against the inode was last made. So here we assume the inode might
3671 * have been evicted, and therefore the exact value of last_unlink_trans
3672 * lost, and set it to last_trans to avoid metadata inconsistencies
3673 * between the inode and its parent if the inode is fsync'ed and the log
3674 * replayed. For example, in the scenario:
3675 *
3676 * touch mydir/foo
3677 * ln mydir/foo mydir/bar
3678 * sync
3679 * unlink mydir/bar
3680 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3681 * xfs_io -c fsync mydir/foo
3682 * <power failure>
3683 * mount fs, triggers fsync log replay
3684 *
3685 * We must make sure that when we fsync our inode foo we also log its
3686 * parent inode, otherwise after log replay the parent still has the
3687 * dentry with the "bar" name but our inode foo has a link count of 1
3688 * and doesn't have an inode ref with the name "bar" anymore.
3689 *
3690 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003691 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003692 * transaction commits on fsync if our inode is a directory, or if our
3693 * inode is not a directory, logging its parent unnecessarily.
3694 */
3695 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3696
Miao Xie67de1172013-12-26 13:07:06 +08003697 path->slots[0]++;
3698 if (inode->i_nlink != 1 ||
3699 path->slots[0] >= btrfs_header_nritems(leaf))
3700 goto cache_acl;
3701
3702 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003703 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003704 goto cache_acl;
3705
3706 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3707 if (location.type == BTRFS_INODE_REF_KEY) {
3708 struct btrfs_inode_ref *ref;
3709
3710 ref = (struct btrfs_inode_ref *)ptr;
3711 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3712 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3713 struct btrfs_inode_extref *extref;
3714
3715 extref = (struct btrfs_inode_extref *)ptr;
3716 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3717 extref);
3718 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003719cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003720 /*
3721 * try to precache a NULL acl entry for files that don't have
3722 * any xattrs or acls
3723 */
Li Zefan33345d012011-04-20 10:31:50 +08003724 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003725 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003726 if (first_xattr_slot != -1) {
3727 path->slots[0] = first_xattr_slot;
3728 ret = btrfs_load_inode_props(inode, path);
3729 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003730 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003731 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003732 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003733 root->root_key.objectid, ret);
3734 }
3735 btrfs_free_path(path);
3736
Al Viro72c04902009-06-24 16:58:48 -04003737 if (!maybe_acls)
3738 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003739
Chris Mason39279cc2007-06-12 06:35:45 -04003740 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003741 case S_IFREG:
3742 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003743 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003744 inode->i_fop = &btrfs_file_operations;
3745 inode->i_op = &btrfs_file_inode_operations;
3746 break;
3747 case S_IFDIR:
3748 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003749 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003750 break;
3751 case S_IFLNK:
3752 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003753 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003754 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3755 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003756 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003757 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003758 init_special_inode(inode, inode->i_mode, rdev);
3759 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003760 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003761
3762 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003763 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003764
3765make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003766 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003767 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003768 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003769}
3770
Chris Masond352ac62008-09-29 15:18:18 -04003771/*
3772 * given a leaf and an inode, copy the inode fields into the leaf
3773 */
Chris Masone02119d2008-09-05 16:13:11 -04003774static void fill_inode_item(struct btrfs_trans_handle *trans,
3775 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003776 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003777 struct inode *inode)
3778{
Liu Bo51fab692012-12-27 09:01:21 +00003779 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003780
Liu Bo51fab692012-12-27 09:01:21 +00003781 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003782
Liu Bo51fab692012-12-27 09:01:21 +00003783 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3784 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3785 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3786 &token);
3787 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3788 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003789
David Sterbaa937b972014-12-12 17:39:12 +01003790 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003791 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003792 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003793 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003794
David Sterbaa937b972014-12-12 17:39:12 +01003795 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003796 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003797 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003798 inode->i_mtime.tv_nsec, &token);
3799
David Sterbaa937b972014-12-12 17:39:12 +01003800 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003801 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003802 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003803 inode->i_ctime.tv_nsec, &token);
3804
chandan r9cc97d62012-07-04 12:48:07 +05303805 btrfs_set_token_timespec_sec(leaf, &item->otime,
3806 BTRFS_I(inode)->i_otime.tv_sec, &token);
3807 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3808 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3809
Liu Bo51fab692012-12-27 09:01:21 +00003810 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3811 &token);
3812 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3813 &token);
3814 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3815 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3816 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3817 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3818 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003819}
3820
Chris Masond352ac62008-09-29 15:18:18 -04003821/*
3822 * copy everything in the in-memory inode into the btree.
3823 */
Chris Mason21151332011-11-10 20:39:08 -05003824static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003825 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003826{
3827 struct btrfs_inode_item *inode_item;
3828 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003829 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003830 int ret;
3831
3832 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003833 if (!path)
3834 return -ENOMEM;
3835
Chris Masonb9473432009-03-13 11:00:37 -04003836 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003837 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3838 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003839 if (ret) {
3840 if (ret > 0)
3841 ret = -ENOENT;
3842 goto failed;
3843 }
3844
Chris Mason5f39d392007-10-15 16:14:19 -04003845 leaf = path->nodes[0];
3846 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003847 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003848
Chris Masone02119d2008-09-05 16:13:11 -04003849 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003850 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003851 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003852 ret = 0;
3853failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003854 btrfs_free_path(path);
3855 return ret;
3856}
3857
Chris Masond352ac62008-09-29 15:18:18 -04003858/*
Chris Mason21151332011-11-10 20:39:08 -05003859 * copy everything in the in-memory inode into the btree.
3860 */
3861noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3862 struct btrfs_root *root, struct inode *inode)
3863{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003864 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003865 int ret;
3866
3867 /*
3868 * If the inode is a free space inode, we can deadlock during commit
3869 * if we put it into the delayed code.
3870 *
3871 * The data relocation inode should also be directly updated
3872 * without delay
3873 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003874 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003875 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003876 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003877 btrfs_update_root_times(trans, root);
3878
Chris Mason21151332011-11-10 20:39:08 -05003879 ret = btrfs_delayed_update_inode(trans, root, inode);
3880 if (!ret)
3881 btrfs_set_inode_last_trans(trans, inode);
3882 return ret;
3883 }
3884
3885 return btrfs_update_inode_item(trans, root, inode);
3886}
3887
Josef Bacikbe6aef62012-10-22 15:43:12 -04003888noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3889 struct btrfs_root *root,
3890 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003891{
3892 int ret;
3893
3894 ret = btrfs_update_inode(trans, root, inode);
3895 if (ret == -ENOSPC)
3896 return btrfs_update_inode_item(trans, root, inode);
3897 return ret;
3898}
3899
3900/*
Chris Masond352ac62008-09-29 15:18:18 -04003901 * unlink helper that gets used here in inode.c and in the tree logging
3902 * recovery code. It remove a link in a directory with a given name, and
3903 * also drops the back refs in the inode to the directory
3904 */
Al Viro92986792011-03-04 17:14:37 +00003905static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3906 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003907 struct btrfs_inode *dir,
3908 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003909 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003910{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003911 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003912 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003913 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003914 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003915 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003916 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003917 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003918 u64 ino = btrfs_ino(inode);
3919 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003920
3921 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003922 if (!path) {
3923 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003924 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003925 }
3926
Chris Masonb9473432009-03-13 11:00:37 -04003927 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003928 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003929 name, name_len, -1);
3930 if (IS_ERR(di)) {
3931 ret = PTR_ERR(di);
3932 goto err;
3933 }
3934 if (!di) {
3935 ret = -ENOENT;
3936 goto err;
3937 }
Chris Mason5f39d392007-10-15 16:14:19 -04003938 leaf = path->nodes[0];
3939 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003940 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003941 if (ret)
3942 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003943 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003944
Miao Xie67de1172013-12-26 13:07:06 +08003945 /*
3946 * If we don't have dir index, we have to get it by looking up
3947 * the inode ref, since we get the inode ref, remove it directly,
3948 * it is unnecessary to do delayed deletion.
3949 *
3950 * But if we have dir index, needn't search inode ref to get it.
3951 * Since the inode ref is close to the inode item, it is better
3952 * that we delay to delete it, and just do this deletion when
3953 * we update the inode item.
3954 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003955 if (inode->dir_index) {
3956 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08003957 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003958 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003959 goto skip_backref;
3960 }
3961 }
3962
Li Zefan33345d012011-04-20 10:31:50 +08003963 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3964 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003965 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003966 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003967 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003968 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003969 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003970 goto err;
3971 }
Miao Xie67de1172013-12-26 13:07:06 +08003972skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003973 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003974 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003975 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003976 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003977 }
Chris Mason39279cc2007-06-12 06:35:45 -04003978
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003979 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3980 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003981 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003982 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003983 goto err;
3984 }
Chris Masone02119d2008-09-05 16:13:11 -04003985
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003986 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3987 index);
Chris Mason6418c962010-10-30 07:34:24 -04003988 if (ret == -ENOENT)
3989 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003990 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003991 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003992err:
3993 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003994 if (ret)
3995 goto out;
3996
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003997 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003998 inode_inc_iversion(&inode->vfs_inode);
3999 inode_inc_iversion(&dir->vfs_inode);
4000 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4001 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4002 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004003out:
Chris Mason39279cc2007-06-12 06:35:45 -04004004 return ret;
4005}
4006
Al Viro92986792011-03-04 17:14:37 +00004007int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4008 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004009 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004010 const char *name, int name_len)
4011{
4012 int ret;
4013 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4014 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004015 drop_nlink(&inode->vfs_inode);
4016 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004017 }
4018 return ret;
4019}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020
4021/*
4022 * helper to start transaction for unlink and rmdir.
4023 *
Josef Bacikd52be812013-05-29 14:54:47 -04004024 * unlink and rmdir are special in btrfs, they do not always free space, so
4025 * if we cannot make our reservations the normal way try and see if there is
4026 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4027 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004028 */
Josef Bacikd52be812013-05-29 14:54:47 -04004029static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004030{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004031 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004032
Josef Bacike70bea52011-10-11 14:18:24 -04004033 /*
4034 * 1 for the possible orphan item
4035 * 1 for the dir item
4036 * 1 for the dir index
4037 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004038 * 1 for the inode
4039 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004040 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004041}
4042
Chris Mason39279cc2007-06-12 06:35:45 -04004043static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4044{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004046 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004047 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004048 int ret;
4049
Josef Bacikd52be812013-05-29 14:54:47 -04004050 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004051 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004052 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004053
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004054 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4055 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004056
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004057 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4058 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4059 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004060 if (ret)
4061 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004062
Yan, Zhenga22285a2010-05-16 10:48:46 -04004063 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004064 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004065 if (ret)
4066 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004067 }
Josef Bacik7b128762008-07-24 12:17:14 -04004068
Tsutomu Itohb5324022011-07-19 07:27:20 +00004069out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004070 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004071 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004072 return ret;
4073}
4074
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004075int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4076 struct btrfs_root *root,
4077 struct inode *dir, u64 objectid,
4078 const char *name, int name_len)
4079{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004080 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004081 struct btrfs_path *path;
4082 struct extent_buffer *leaf;
4083 struct btrfs_dir_item *di;
4084 struct btrfs_key key;
4085 u64 index;
4086 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004087 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004088
4089 path = btrfs_alloc_path();
4090 if (!path)
4091 return -ENOMEM;
4092
Li Zefan33345d012011-04-20 10:31:50 +08004093 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004094 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004095 if (IS_ERR_OR_NULL(di)) {
4096 if (!di)
4097 ret = -ENOENT;
4098 else
4099 ret = PTR_ERR(di);
4100 goto out;
4101 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004102
4103 leaf = path->nodes[0];
4104 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4105 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4106 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004107 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004108 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004109 goto out;
4110 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004111 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004113 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4114 root->root_key.objectid, dir_ino,
4115 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004116 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004117 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004118 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004119 goto out;
4120 }
Li Zefan33345d012011-04-20 10:31:50 +08004121 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004122 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004123 if (IS_ERR_OR_NULL(di)) {
4124 if (!di)
4125 ret = -ENOENT;
4126 else
4127 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004128 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004129 goto out;
4130 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004131
4132 leaf = path->nodes[0];
4133 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004134 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004135 index = key.offset;
4136 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004137 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004138
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004139 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004140 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004141 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004142 goto out;
4143 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004144
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004145 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004146 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004147 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004148 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004149 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004150 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004151out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004152 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004153 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004154}
4155
Chris Mason39279cc2007-06-12 06:35:45 -04004156static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4157{
David Howells2b0143b2015-03-17 22:25:59 +00004158 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004159 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004160 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004161 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004162 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004163
David Sterbab3ae2442012-09-13 16:04:34 -06004164 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004165 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004166 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004167 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004168
Josef Bacikd52be812013-05-29 14:54:47 -04004169 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004170 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004171 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004172
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004173 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004174 err = btrfs_unlink_subvol(trans, root, dir,
4175 BTRFS_I(inode)->location.objectid,
4176 dentry->d_name.name,
4177 dentry->d_name.len);
4178 goto out;
4179 }
4180
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004181 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004182 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004183 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004184
Filipe Manana44f714d2016-06-06 16:11:13 +01004185 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4186
Chris Mason39279cc2007-06-12 06:35:45 -04004187 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004188 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4189 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4190 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004191 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004192 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004193 /*
4194 * Propagate the last_unlink_trans value of the deleted dir to
4195 * its parent directory. This is to prevent an unrecoverable
4196 * log tree in the case we do something like this:
4197 * 1) create dir foo
4198 * 2) create snapshot under dir foo
4199 * 3) delete the snapshot
4200 * 4) rmdir foo
4201 * 5) mkdir foo
4202 * 6) fsync foo or some file inside foo
4203 */
4204 if (last_unlink_trans >= trans->transid)
4205 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4206 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004207out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004208 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004209 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004210
Chris Mason39279cc2007-06-12 06:35:45 -04004211 return err;
4212}
4213
Chris Mason28f75a02015-02-04 06:59:29 -08004214static int truncate_space_check(struct btrfs_trans_handle *trans,
4215 struct btrfs_root *root,
4216 u64 bytes_deleted)
4217{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004218 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004219 int ret;
4220
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004221 /*
4222 * This is only used to apply pressure to the enospc system, we don't
4223 * intend to use this reservation at all.
4224 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004225 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004226 bytes_deleted *= fs_info->nodesize;
4227 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004228 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004229 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004230 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004231 trans->transid,
4232 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004233 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004234 }
Chris Mason28f75a02015-02-04 06:59:29 -08004235 return ret;
4236
4237}
4238
Filipe Manana0305cd52015-10-16 12:34:25 +01004239static int truncate_inline_extent(struct inode *inode,
4240 struct btrfs_path *path,
4241 struct btrfs_key *found_key,
4242 const u64 item_end,
4243 const u64 new_size)
4244{
4245 struct extent_buffer *leaf = path->nodes[0];
4246 int slot = path->slots[0];
4247 struct btrfs_file_extent_item *fi;
4248 u32 size = (u32)(new_size - found_key->offset);
4249 struct btrfs_root *root = BTRFS_I(inode)->root;
4250
4251 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4252
4253 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4254 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004255 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004256
4257 /*
4258 * Zero out the remaining of the last page of our inline extent,
4259 * instead of directly truncating our inline extent here - that
4260 * would be much more complex (decompressing all the data, then
4261 * compressing the truncated data, which might be bigger than
4262 * the size of the inline extent, resize the extent, etc).
4263 * We release the path because to get the page we might need to
4264 * read the extent item from disk (data not in the page cache).
4265 */
4266 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304267 return btrfs_truncate_block(inode, offset, page_end - offset,
4268 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004269 }
4270
4271 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4272 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004273 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004274
4275 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4276 inode_sub_bytes(inode, item_end + 1 - new_size);
4277
4278 return 0;
4279}
4280
Chris Mason323ac952008-10-01 19:05:46 -04004281/*
Chris Mason39279cc2007-06-12 06:35:45 -04004282 * this can truncate away extent items, csum items and directory items.
4283 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004284 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004285 *
4286 * csum items that cross the new i_size are truncated to the new size
4287 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004288 *
4289 * min_type is the minimum key type to truncate down to. If set to 0, this
4290 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004291 */
Yan, Zheng80825102009-11-12 09:35:36 +00004292int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4293 struct btrfs_root *root,
4294 struct inode *inode,
4295 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004296{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004297 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004298 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004299 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004300 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004301 struct btrfs_key key;
4302 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004303 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004304 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004305 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004306 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004307 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004308 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004309 int found_extent;
4310 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004311 int pending_del_nr = 0;
4312 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004313 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004314 int ret;
4315 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004316 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004317 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004318 bool be_nice = 0;
4319 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004320 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004321
4322 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004323
Chris Mason28ed1342014-12-17 09:41:04 -08004324 /*
4325 * for non-free space inodes and ref cows, we want to back off from
4326 * time to time
4327 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004328 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004329 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4330 be_nice = 1;
4331
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004332 path = btrfs_alloc_path();
4333 if (!path)
4334 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004335 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004336
Josef Bacik5dc562c2012-08-17 13:14:17 -04004337 /*
4338 * We want to drop from the next block forward in case this new size is
4339 * not block aligned since we will be keeping the last block of the
4340 * extent just the way it is.
4341 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004342 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004343 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004344 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004345 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004346 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004347
Miao Xie16cdcec2011-04-22 18:12:22 +08004348 /*
4349 * This function is also used to drop the items in the log tree before
4350 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4351 * it is used to drop the loged items. So we shouldn't kill the delayed
4352 * items.
4353 */
4354 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004355 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004356
Li Zefan33345d012011-04-20 10:31:50 +08004357 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004358 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004359 key.type = (u8)-1;
4360
Chris Mason85e21ba2008-01-29 15:11:36 -05004361search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004362 /*
4363 * with a 16K leaf size and 128MB extents, you can actually queue
4364 * up a huge file in a single leaf. Most of the time that
4365 * bytes_deleted is > 0, it will be huge by the time we get here
4366 */
Byongho Leeee221842015-12-15 01:42:10 +09004367 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004368 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004369 err = -EAGAIN;
4370 goto error;
4371 }
4372 }
4373
4374
Chris Masonb9473432009-03-13 11:00:37 -04004375 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004376 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004377 if (ret < 0) {
4378 err = ret;
4379 goto out;
4380 }
Chris Masond3977122009-01-05 21:25:51 -05004381
Chris Mason85e21ba2008-01-29 15:11:36 -05004382 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004383 /* there are no items in the tree for us to truncate, we're
4384 * done
4385 */
Yan, Zheng80825102009-11-12 09:35:36 +00004386 if (path->slots[0] == 0)
4387 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004388 path->slots[0]--;
4389 }
4390
Chris Masond3977122009-01-05 21:25:51 -05004391 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004392 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004393 leaf = path->nodes[0];
4394 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004395 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004396
Li Zefan33345d012011-04-20 10:31:50 +08004397 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004398 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004399
Chris Mason85e21ba2008-01-29 15:11:36 -05004400 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004401 break;
4402
Chris Mason5f39d392007-10-15 16:14:19 -04004403 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004404 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004405 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004406 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004407 extent_type = btrfs_file_extent_type(leaf, fi);
4408 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004409 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004410 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004411 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004412 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004413 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004414 }
Yan008630c2007-11-07 13:31:09 -05004415 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004416 }
Yan, Zheng80825102009-11-12 09:35:36 +00004417 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004418 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004419 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004420 if (item_end < new_size) {
4421 /*
4422 * With NO_HOLES mode, for the following mapping
4423 *
4424 * [0-4k][hole][8k-12k]
4425 *
4426 * if truncating isize down to 6k, it ends up
4427 * isize being 8k.
4428 */
4429 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4430 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004431 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004432 }
Yan, Zheng80825102009-11-12 09:35:36 +00004433 if (found_key.offset >= new_size)
4434 del_item = 1;
4435 else
4436 del_item = 0;
4437 }
Chris Mason39279cc2007-06-12 06:35:45 -04004438 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004439 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004440 if (found_type != BTRFS_EXTENT_DATA_KEY)
4441 goto delete;
4442
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004443 if (del_item)
4444 last_size = found_key.offset;
4445 else
4446 last_size = new_size;
4447
Chris Mason179e29e2007-11-01 11:28:41 -04004448 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004449 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004450 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004451 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004452 u64 orig_num_bytes =
4453 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004454 extent_num_bytes = ALIGN(new_size -
4455 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004456 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004457 btrfs_set_file_extent_num_bytes(leaf, fi,
4458 extent_num_bytes);
4459 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004460 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004461 if (test_bit(BTRFS_ROOT_REF_COWS,
4462 &root->state) &&
4463 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004464 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004465 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004466 } else {
Chris Masondb945352007-10-15 16:15:53 -04004467 extent_num_bytes =
4468 btrfs_file_extent_disk_num_bytes(leaf,
4469 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004470 extent_offset = found_key.offset -
4471 btrfs_file_extent_offset(leaf, fi);
4472
Chris Mason39279cc2007-06-12 06:35:45 -04004473 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004474 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004475 if (extent_start != 0) {
4476 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004477 if (test_bit(BTRFS_ROOT_REF_COWS,
4478 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004479 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004480 }
4481 }
Chris Mason90692182008-02-08 13:49:28 -05004482 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004483 /*
4484 * we can't truncate inline items that have had
4485 * special encodings
4486 */
4487 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004488 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4489 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004490
4491 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004492 * Need to release path in order to truncate a
4493 * compressed extent. So delete any accumulated
4494 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004495 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004496 if (btrfs_file_extent_compression(leaf, fi) !=
4497 BTRFS_COMPRESS_NONE && pending_del_nr) {
4498 err = btrfs_del_items(trans, root, path,
4499 pending_del_slot,
4500 pending_del_nr);
4501 if (err) {
4502 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004503 err);
4504 goto error;
4505 }
4506 pending_del_nr = 0;
4507 }
4508
4509 err = truncate_inline_extent(inode, path,
4510 &found_key,
4511 item_end,
4512 new_size);
4513 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004514 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004515 goto error;
4516 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004517 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4518 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004519 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004520 }
Chris Mason39279cc2007-06-12 06:35:45 -04004521 }
Chris Mason179e29e2007-11-01 11:28:41 -04004522delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004523 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004524 if (!pending_del_nr) {
4525 /* no pending yet, add ourselves */
4526 pending_del_slot = path->slots[0];
4527 pending_del_nr = 1;
4528 } else if (pending_del_nr &&
4529 path->slots[0] + 1 == pending_del_slot) {
4530 /* hop on the pending chunk */
4531 pending_del_nr++;
4532 pending_del_slot = path->slots[0];
4533 } else {
Chris Masond3977122009-01-05 21:25:51 -05004534 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004535 }
Chris Mason39279cc2007-06-12 06:35:45 -04004536 } else {
4537 break;
4538 }
Chris Mason28f75a02015-02-04 06:59:29 -08004539 should_throttle = 0;
4540
Miao Xie27cdeb72014-04-02 19:51:05 +08004541 if (found_extent &&
4542 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004543 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004544 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004545 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004546 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004547 extent_num_bytes, 0,
4548 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004549 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004550 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004551 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4552 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004553 trans->delayed_ref_updates * 2,
4554 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004555 if (be_nice) {
4556 if (truncate_space_check(trans, root,
4557 extent_num_bytes)) {
4558 should_end = 1;
4559 }
4560 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004561 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004562 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004563 }
Chris Mason39279cc2007-06-12 06:35:45 -04004564 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004565
Yan, Zheng80825102009-11-12 09:35:36 +00004566 if (found_type == BTRFS_INODE_ITEM_KEY)
4567 break;
4568
4569 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004570 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004571 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004572 if (pending_del_nr) {
4573 ret = btrfs_del_items(trans, root, path,
4574 pending_del_slot,
4575 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004576 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004577 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004578 goto error;
4579 }
Yan, Zheng80825102009-11-12 09:35:36 +00004580 pending_del_nr = 0;
4581 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004582 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004583 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004584 unsigned long updates = trans->delayed_ref_updates;
4585 if (updates) {
4586 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004587 ret = btrfs_run_delayed_refs(trans,
4588 fs_info,
4589 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004590 if (ret && !err)
4591 err = ret;
4592 }
4593 }
Chris Mason28f75a02015-02-04 06:59:29 -08004594 /*
4595 * if we failed to refill our space rsv, bail out
4596 * and let the transaction restart
4597 */
4598 if (should_end) {
4599 err = -EAGAIN;
4600 goto error;
4601 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004602 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004603 } else {
4604 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004605 }
Chris Mason39279cc2007-06-12 06:35:45 -04004606 }
Yan, Zheng80825102009-11-12 09:35:36 +00004607out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004608 if (pending_del_nr) {
4609 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4610 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004611 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004612 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004613 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004614error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004615 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004616 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004617
Chris Mason39279cc2007-06-12 06:35:45 -04004618 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004619
Liu Bo19fd2df2016-12-01 13:01:02 -08004620 if (err == 0) {
4621 /* only inline file may have last_size != new_size */
4622 if (new_size >= fs_info->sectorsize ||
4623 new_size > fs_info->max_inline)
4624 ASSERT(last_size == new_size);
4625 }
4626
Byongho Leeee221842015-12-15 01:42:10 +09004627 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004628 unsigned long updates = trans->delayed_ref_updates;
4629 if (updates) {
4630 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004631 ret = btrfs_run_delayed_refs(trans, fs_info,
4632 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004633 if (ret && !err)
4634 err = ret;
4635 }
4636 }
Yan, Zheng80825102009-11-12 09:35:36 +00004637 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004638}
4639
Chris Masona52d9a82007-08-27 16:49:44 -04004640/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304641 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004642 * @inode - inode that we're zeroing
4643 * @from - the offset to start zeroing
4644 * @len - the length to zero, 0 to zero the entire range respective to the
4645 * offset
4646 * @front - zero up to the offset instead of from the offset on
4647 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304648 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004649 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004650 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304651int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004652 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004653{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004654 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004655 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004656 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4657 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004658 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004659 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004660 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004661 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304662 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004663 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004664 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004665 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304666 u64 block_start;
4667 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004668
Josef Bacik2aaa6652012-08-29 14:27:18 -04004669 if ((offset & (blocksize - 1)) == 0 &&
4670 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004671 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304672
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004673 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304674 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004675 if (ret)
4676 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004677
Chris Mason211c17f2008-05-15 09:13:45 -04004678again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004679 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004680 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004681 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304682 round_down(from, blocksize),
4683 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004684 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004685 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004686 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004687
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304688 block_start = round_down(from, blocksize);
4689 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004690
Chris Masona52d9a82007-08-27 16:49:44 -04004691 if (!PageUptodate(page)) {
4692 ret = btrfs_readpage(NULL, page);
4693 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004694 if (page->mapping != mapping) {
4695 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004696 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004697 goto again;
4698 }
Chris Masona52d9a82007-08-27 16:49:44 -04004699 if (!PageUptodate(page)) {
4700 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004701 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004702 }
4703 }
Chris Mason211c17f2008-05-15 09:13:45 -04004704 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004705
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304706 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004707 set_page_extent_mapped(page);
4708
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304709 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004710 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304711 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004712 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004713 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004714 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004715 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004716 btrfs_put_ordered_extent(ordered);
4717 goto again;
4718 }
4719
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304720 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004721 EXTENT_DIRTY | EXTENT_DELALLOC |
4722 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004723 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004724
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304725 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004726 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004727 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304728 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004729 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004730 goto out_unlock;
4731 }
4732
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304733 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004734 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304735 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004736 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004737 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304738 memset(kaddr + (block_start - page_offset(page)),
4739 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004740 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304741 memset(kaddr + (block_start - page_offset(page)) + offset,
4742 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004743 flush_dcache_page(page);
4744 kunmap(page);
4745 }
Chris Mason247e7432008-07-17 12:53:51 -04004746 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004747 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304748 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004749 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004750
Chris Mason89642222008-07-24 09:41:53 -04004751out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004752 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304753 btrfs_delalloc_release_space(inode, block_start,
4754 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004755 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004756 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004757out:
4758 return ret;
4759}
4760
Josef Bacik16e75492013-10-22 12:18:51 -04004761static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4762 u64 offset, u64 len)
4763{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004764 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004765 struct btrfs_trans_handle *trans;
4766 int ret;
4767
4768 /*
4769 * Still need to make sure the inode looks like it's been updated so
4770 * that any holes get logged if we fsync.
4771 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004772 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4773 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004774 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4775 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4776 return 0;
4777 }
4778
4779 /*
4780 * 1 - for the one we're dropping
4781 * 1 - for the one we're adding
4782 * 1 - for updating the inode.
4783 */
4784 trans = btrfs_start_transaction(root, 3);
4785 if (IS_ERR(trans))
4786 return PTR_ERR(trans);
4787
4788 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4789 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004790 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004791 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004792 return ret;
4793 }
4794
David Sterbaf85b7372017-01-20 14:54:07 +01004795 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4796 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004797 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004798 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004799 else
4800 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004801 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004802 return ret;
4803}
4804
Josef Bacik695a0d02011-03-04 15:46:53 -05004805/*
4806 * This function puts in dummy file extents for the area we're creating a hole
4807 * for. So if we are truncating this file to a larger size we need to insert
4808 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4809 * the range between oldsize and size
4810 */
Josef Bacika41ad392011-01-31 15:30:16 -05004811int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004812{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004813 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004814 struct btrfs_root *root = BTRFS_I(inode)->root;
4815 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004816 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004817 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004818 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004819 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4820 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004821 u64 last_byte;
4822 u64 cur_offset;
4823 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004824 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004825
Josef Bacika71754f2013-06-17 17:14:39 -04004826 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304827 * If our size started in the middle of a block we need to zero out the
4828 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004829 * expose stale data.
4830 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304831 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004832 if (err)
4833 return err;
4834
Yan Zheng9036c102008-10-30 14:19:41 -04004835 if (size <= hole_start)
4836 return 0;
4837
Yan Zheng9036c102008-10-30 14:19:41 -04004838 while (1) {
4839 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004840
David Sterbaff13db42015-12-03 14:30:40 +01004841 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004842 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004843 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004844 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004845 if (!ordered)
4846 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004847 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4848 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004849 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004850 btrfs_put_ordered_extent(ordered);
4851 }
4852
Yan Zheng9036c102008-10-30 14:19:41 -04004853 cur_offset = hole_start;
4854 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02004855 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004856 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004857 if (IS_ERR(em)) {
4858 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004859 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004860 break;
4861 }
Yan Zheng9036c102008-10-30 14:19:41 -04004862 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004863 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004864 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004865 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004866 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004867
Josef Bacik16e75492013-10-22 12:18:51 -04004868 err = maybe_insert_hole(root, inode, cur_offset,
4869 hole_size);
4870 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004871 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004872 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004873 cur_offset + hole_size - 1, 0);
4874 hole_em = alloc_extent_map();
4875 if (!hole_em) {
4876 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4877 &BTRFS_I(inode)->runtime_flags);
4878 goto next;
4879 }
4880 hole_em->start = cur_offset;
4881 hole_em->len = hole_size;
4882 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004883
Josef Bacik5dc562c2012-08-17 13:14:17 -04004884 hole_em->block_start = EXTENT_MAP_HOLE;
4885 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004886 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004887 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004888 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004889 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004890 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004891
4892 while (1) {
4893 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004894 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004895 write_unlock(&em_tree->lock);
4896 if (err != -EEXIST)
4897 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004898 btrfs_drop_extent_cache(BTRFS_I(inode),
4899 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004900 cur_offset +
4901 hole_size - 1, 0);
4902 }
4903 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004904 }
Josef Bacik16e75492013-10-22 12:18:51 -04004905next:
Yan Zheng9036c102008-10-30 14:19:41 -04004906 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004907 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004908 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004909 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004910 break;
4911 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004912 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004913 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4914 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004915 return err;
4916}
4917
Eric Sandeen3972f262013-01-12 02:57:22 +00004918static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004919{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004920 struct btrfs_root *root = BTRFS_I(inode)->root;
4921 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004922 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004923 loff_t newsize = attr->ia_size;
4924 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004925 int ret;
4926
Eric Sandeen3972f262013-01-12 02:57:22 +00004927 /*
4928 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4929 * special case where we need to update the times despite not having
4930 * these flags set. For all other operations the VFS set these flags
4931 * explicitly if it wants a timestamp update.
4932 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004933 if (newsize != oldsize) {
4934 inode_inc_iversion(inode);
4935 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4936 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004937 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004938 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004939
Josef Bacika41ad392011-01-31 15:30:16 -05004940 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004941 /*
4942 * Don't do an expanding truncate while snapshoting is ongoing.
4943 * This is to ensure the snapshot captures a fully consistent
4944 * state of this file - if the snapshot captures this expanding
4945 * truncation, it must capture all writes that happened before
4946 * this truncation.
4947 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004948 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004949 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004950 if (ret) {
4951 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004952 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004953 }
Yan, Zheng80825102009-11-12 09:35:36 +00004954
Miao Xief4a2f4c2011-12-14 20:12:01 -05004955 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004956 if (IS_ERR(trans)) {
4957 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004958 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004959 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004960
4961 i_size_write(inode, newsize);
4962 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304963 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004964 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004965 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004966 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004967 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004968
Josef Bacika41ad392011-01-31 15:30:16 -05004969 /*
4970 * We're truncating a file that used to have good data down to
4971 * zero. Make sure it gets into the ordered flush list so that
4972 * any new writes get down to disk quickly.
4973 */
4974 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004975 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4976 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004977
Josef Bacikf3fe8202013-01-07 17:03:21 -05004978 /*
4979 * 1 for the orphan item we're going to add
4980 * 1 for the orphan item deletion.
4981 */
4982 trans = btrfs_start_transaction(root, 2);
4983 if (IS_ERR(trans))
4984 return PTR_ERR(trans);
4985
4986 /*
4987 * We need to do this in case we fail at _any_ point during the
4988 * actual truncate. Once we do the truncate_setsize we could
4989 * invalidate pages which forces any outstanding ordered io to
4990 * be instantly completed which will give us extents that need
4991 * to be truncated. If we fail to get an orphan inode down we
4992 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004993 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05004994 * will be consistent.
4995 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004996 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004997 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004998 if (ret)
4999 return ret;
5000
Josef Bacika41ad392011-01-31 15:30:16 -05005001 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5002 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005003
5004 /* Disable nonlocked read DIO to avoid the end less truncate */
5005 btrfs_inode_block_unlocked_dio(inode);
5006 inode_dio_wait(inode);
5007 btrfs_inode_resume_unlocked_dio(inode);
5008
Josef Bacika41ad392011-01-31 15:30:16 -05005009 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005010 if (ret && inode->i_nlink) {
5011 int err;
5012
Liu Bo19fd2df2016-12-01 13:01:02 -08005013 /* To get a stable disk_i_size */
5014 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5015 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005016 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005017 return err;
5018 }
5019
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005020 /*
5021 * failed to truncate, disk_i_size is only adjusted down
5022 * as we remove extents, so it should represent the true
5023 * size of the inode, so reset the in memory size and
5024 * delete our orphan entry.
5025 */
5026 trans = btrfs_join_transaction(root);
5027 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005028 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005029 return ret;
5030 }
5031 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005032 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005033 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005034 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005035 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005036 }
Yan, Zheng80825102009-11-12 09:35:36 +00005037 }
5038
Josef Bacika41ad392011-01-31 15:30:16 -05005039 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005040}
5041
Chris Mason39279cc2007-06-12 06:35:45 -04005042static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5043{
David Howells2b0143b2015-03-17 22:25:59 +00005044 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005045 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005046 int err;
5047
Li Zefanb83cc962010-12-20 16:04:08 +08005048 if (btrfs_root_readonly(root))
5049 return -EROFS;
5050
Jan Kara31051c82016-05-26 16:55:18 +02005051 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005052 if (err)
5053 return err;
5054
Chris Mason5a3f23d2009-03-31 13:27:11 -04005055 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005056 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005057 if (err)
5058 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005059 }
Yan Zheng9036c102008-10-30 14:19:41 -04005060
Christoph Hellwig10257742010-06-04 11:30:02 +02005061 if (attr->ia_valid) {
5062 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005063 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005064 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005065
Josef Bacik22c44fe2011-11-30 10:45:38 -05005066 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005067 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005068 }
5069
Chris Mason39279cc2007-06-12 06:35:45 -04005070 return err;
5071}
Chris Mason61295eb2008-01-14 16:24:38 -05005072
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005073/*
5074 * While truncating the inode pages during eviction, we get the VFS calling
5075 * btrfs_invalidatepage() against each page of the inode. This is slow because
5076 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5077 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5078 * extent_state structures over and over, wasting lots of time.
5079 *
5080 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5081 * those expensive operations on a per page basis and do only the ordered io
5082 * finishing, while we release here the extent_map and extent_state structures,
5083 * without the excessive merging and splitting.
5084 */
5085static void evict_inode_truncate_pages(struct inode *inode)
5086{
5087 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5088 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5089 struct rb_node *node;
5090
5091 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005092 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005093
5094 write_lock(&map_tree->lock);
5095 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5096 struct extent_map *em;
5097
5098 node = rb_first(&map_tree->map);
5099 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005100 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5101 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005102 remove_extent_mapping(map_tree, em);
5103 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005104 if (need_resched()) {
5105 write_unlock(&map_tree->lock);
5106 cond_resched();
5107 write_lock(&map_tree->lock);
5108 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005109 }
5110 write_unlock(&map_tree->lock);
5111
Filipe Manana6ca07092015-05-26 00:55:42 +01005112 /*
5113 * Keep looping until we have no more ranges in the io tree.
5114 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005115 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5116 * still in progress (unlocked the pages in the bio but did not yet
5117 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005118 * ranges can still be locked and eviction started because before
5119 * submitting those bios, which are executed by a separate task (work
5120 * queue kthread), inode references (inode->i_count) were not taken
5121 * (which would be dropped in the end io callback of each bio).
5122 * Therefore here we effectively end up waiting for those bios and
5123 * anyone else holding locked ranges without having bumped the inode's
5124 * reference count - if we don't do it, when they access the inode's
5125 * io_tree to unlock a range it may be too late, leading to an
5126 * use-after-free issue.
5127 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005128 spin_lock(&io_tree->lock);
5129 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5130 struct extent_state *state;
5131 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005132 u64 start;
5133 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005134
5135 node = rb_first(&io_tree->state);
5136 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005137 start = state->start;
5138 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005139 spin_unlock(&io_tree->lock);
5140
David Sterbaff13db42015-12-03 14:30:40 +01005141 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005142
5143 /*
5144 * If still has DELALLOC flag, the extent didn't reach disk,
5145 * and its reserved space won't be freed by delayed_ref.
5146 * So we need to free its reserved space here.
5147 * (Refer to comment in btrfs_invalidatepage, case 2)
5148 *
5149 * Note, end is the bytenr of last byte, so we need + 1 here.
5150 */
5151 if (state->state & EXTENT_DELALLOC)
5152 btrfs_qgroup_free_data(inode, start, end - start + 1);
5153
Filipe Manana6ca07092015-05-26 00:55:42 +01005154 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005155 EXTENT_LOCKED | EXTENT_DIRTY |
5156 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5157 EXTENT_DEFRAG, 1, 1,
5158 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005159
Filipe Manana7064dd52014-08-08 02:47:05 +01005160 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005161 spin_lock(&io_tree->lock);
5162 }
5163 spin_unlock(&io_tree->lock);
5164}
5165
Al Virobd555972010-06-07 11:35:40 -04005166void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005167{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005168 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005169 struct btrfs_trans_handle *trans;
5170 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005171 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005172 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005173 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005174 int ret;
5175
liubo1abe9b82011-03-24 11:18:59 +00005176 trace_btrfs_inode_evict(inode);
5177
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005178 if (!root) {
5179 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5180 return;
5181 }
5182
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005183 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005184
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005185 evict_inode_truncate_pages(inode);
5186
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005187 if (inode->i_nlink &&
5188 ((btrfs_root_refs(&root->root_item) != 0 &&
5189 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005190 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005191 goto no_delete;
5192
Chris Mason39279cc2007-06-12 06:35:45 -04005193 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005194 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005195 goto no_delete;
5196 }
Al Virobd555972010-06-07 11:35:40 -04005197 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005198 if (!special_file(inode->i_mode))
5199 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005200
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005201 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005202
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005203 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005204 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005205 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005206 goto no_delete;
5207 }
5208
Yan, Zheng76dda932009-09-21 16:00:26 -04005209 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005210 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5211 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005212 goto no_delete;
5213 }
5214
Nikolay Borisovaa790212017-01-10 20:35:40 +02005215 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005216 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005217 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005218 goto no_delete;
5219 }
5220
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005221 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005222 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005223 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005224 goto no_delete;
5225 }
Josef Bacik4a338542011-08-29 11:01:31 -04005226 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005227 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005228 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005229
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005230 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005231
Josef Bacik4289a662011-08-05 13:22:24 -04005232 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005233 * This is a bit simpler than btrfs_truncate since we've already
5234 * reserved our space for our orphan item in the unlink, so we just
5235 * need to reserve some slack space in case we add bytes and update
5236 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005237 */
Yan, Zheng80825102009-11-12 09:35:36 +00005238 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005239 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5240 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005241
Josef Bacik726c35f2011-09-26 15:46:06 -04005242 /*
5243 * Try and steal from the global reserve since we will
5244 * likely not use this space anyway, we want to try as
5245 * hard as possible to get this to work.
5246 */
5247 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005248 steal_from_global++;
5249 else
5250 steal_from_global = 0;
5251 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005252
Josef Bacik3bce8762015-02-24 12:35:51 -08005253 /*
5254 * steal_from_global == 0: we reserved stuff, hooray!
5255 * steal_from_global == 1: we didn't reserve stuff, boo!
5256 * steal_from_global == 2: we've committed, still not a lot of
5257 * room but maybe we'll have room in the global reserve this
5258 * time.
5259 * steal_from_global == 3: abandon all hope!
5260 */
5261 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005262 btrfs_warn(fs_info,
5263 "Could not get space for a delete, will truncate on mount %d",
5264 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005265 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005266 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005267 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005268 }
5269
Miao Xie0e8c36a2012-12-19 06:59:51 +00005270 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005271 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005272 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005273 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005274 goto no_delete;
5275 }
5276
Josef Bacik3bce8762015-02-24 12:35:51 -08005277 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005278 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005279 * sure there is room to do it, if not we need to commit and try
5280 * again.
5281 */
5282 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005283 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005284 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005285 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005286 else
5287 ret = -ENOSPC;
5288 }
5289
5290 /*
5291 * Couldn't steal from the global reserve, we have too much
5292 * pending stuff built up, commit the transaction and try it
5293 * again.
5294 */
5295 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005296 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005297 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005298 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005299 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005300 goto no_delete;
5301 }
5302 continue;
5303 } else {
5304 steal_from_global = 0;
5305 }
5306
Josef Bacik4289a662011-08-05 13:22:24 -04005307 trans->block_rsv = rsv;
5308
Yan, Zhengd68fc572010-05-16 10:49:58 -04005309 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005310 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005311 break;
5312
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005313 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005314 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005315 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005316 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005317 }
5318
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005319 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005320
Josef Bacik4ef31a42013-08-13 14:10:08 -04005321 /*
5322 * Errors here aren't a big deal, it just means we leave orphan items
5323 * in the tree. They will be cleaned up on the next mount.
5324 */
Yan, Zheng80825102009-11-12 09:35:36 +00005325 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005326 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005327 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005328 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005329 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005330 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005331
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005332 trans->block_rsv = &fs_info->trans_block_rsv;
5333 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005334 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005335 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005336
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005337 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005338 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005339no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005340 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005341 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005342}
5343
5344/*
5345 * this returns the key found in the dir entry in the location pointer.
5346 * If no dir entries were found, location->objectid is 0.
5347 */
5348static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5349 struct btrfs_key *location)
5350{
5351 const char *name = dentry->d_name.name;
5352 int namelen = dentry->d_name.len;
5353 struct btrfs_dir_item *di;
5354 struct btrfs_path *path;
5355 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005356 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005357
5358 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005359 if (!path)
5360 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005361
David Sterbaf85b7372017-01-20 14:54:07 +01005362 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5363 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005364 if (IS_ERR(di))
5365 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005366
David Sterbac7040052011-04-19 18:00:01 +02005367 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005368 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005369
Chris Mason5f39d392007-10-15 16:14:19 -04005370 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005371out:
Chris Mason39279cc2007-06-12 06:35:45 -04005372 btrfs_free_path(path);
5373 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005374out_err:
5375 location->objectid = 0;
5376 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005377}
5378
5379/*
5380 * when we hit a tree root in a directory, the btrfs part of the inode
5381 * needs to be changed to reflect the root directory of the tree root. This
5382 * is kind of like crossing a mount point.
5383 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005384static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005385 struct inode *dir,
5386 struct dentry *dentry,
5387 struct btrfs_key *location,
5388 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005389{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005390 struct btrfs_path *path;
5391 struct btrfs_root *new_root;
5392 struct btrfs_root_ref *ref;
5393 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005394 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005395 int ret;
5396 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005397
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005398 path = btrfs_alloc_path();
5399 if (!path) {
5400 err = -ENOMEM;
5401 goto out;
5402 }
Chris Mason39279cc2007-06-12 06:35:45 -04005403
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005404 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005405 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5406 key.type = BTRFS_ROOT_REF_KEY;
5407 key.offset = location->objectid;
5408
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005409 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005410 if (ret) {
5411 if (ret < 0)
5412 err = ret;
5413 goto out;
5414 }
Chris Mason39279cc2007-06-12 06:35:45 -04005415
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005416 leaf = path->nodes[0];
5417 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005418 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005419 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5420 goto out;
5421
5422 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5423 (unsigned long)(ref + 1),
5424 dentry->d_name.len);
5425 if (ret)
5426 goto out;
5427
David Sterbab3b4aa72011-04-21 01:20:15 +02005428 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005429
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005430 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005431 if (IS_ERR(new_root)) {
5432 err = PTR_ERR(new_root);
5433 goto out;
5434 }
5435
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005436 *sub_root = new_root;
5437 location->objectid = btrfs_root_dirid(&new_root->root_item);
5438 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005439 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005440 err = 0;
5441out:
5442 btrfs_free_path(path);
5443 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005444}
5445
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005446static void inode_tree_add(struct inode *inode)
5447{
5448 struct btrfs_root *root = BTRFS_I(inode)->root;
5449 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005450 struct rb_node **p;
5451 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005452 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005453 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005454
Al Viro1d3382cb2010-10-23 15:19:20 -04005455 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005456 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005457 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005458 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005459 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005460 while (*p) {
5461 parent = *p;
5462 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5463
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005464 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005465 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005466 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005467 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005468 else {
5469 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005470 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005471 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005472 RB_CLEAR_NODE(parent);
5473 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005474 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005475 }
5476 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005477 rb_link_node(new, parent, p);
5478 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005479 spin_unlock(&root->inode_lock);
5480}
5481
5482static void inode_tree_del(struct inode *inode)
5483{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005484 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005485 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005486 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005487
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005488 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005489 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005490 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005491 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005492 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005493 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005494 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005495
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005496 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005497 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005498 spin_lock(&root->inode_lock);
5499 empty = RB_EMPTY_ROOT(&root->inode_tree);
5500 spin_unlock(&root->inode_lock);
5501 if (empty)
5502 btrfs_add_dead_root(root);
5503 }
5504}
5505
Jeff Mahoney143bede2012-03-01 14:56:26 +01005506void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005507{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005508 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005509 struct rb_node *node;
5510 struct rb_node *prev;
5511 struct btrfs_inode *entry;
5512 struct inode *inode;
5513 u64 objectid = 0;
5514
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005515 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005516 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005517
5518 spin_lock(&root->inode_lock);
5519again:
5520 node = root->inode_tree.rb_node;
5521 prev = NULL;
5522 while (node) {
5523 prev = node;
5524 entry = rb_entry(node, struct btrfs_inode, rb_node);
5525
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 = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005528 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005529 node = node->rb_right;
5530 else
5531 break;
5532 }
5533 if (!node) {
5534 while (prev) {
5535 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005536 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005537 node = prev;
5538 break;
5539 }
5540 prev = rb_next(prev);
5541 }
5542 }
5543 while (node) {
5544 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005545 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005546 inode = igrab(&entry->vfs_inode);
5547 if (inode) {
5548 spin_unlock(&root->inode_lock);
5549 if (atomic_read(&inode->i_count) > 1)
5550 d_prune_aliases(inode);
5551 /*
Al Viro45321ac2010-06-07 13:43:19 -04005552 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005553 * the inode cache when its usage count
5554 * hits zero.
5555 */
5556 iput(inode);
5557 cond_resched();
5558 spin_lock(&root->inode_lock);
5559 goto again;
5560 }
5561
5562 if (cond_resched_lock(&root->inode_lock))
5563 goto again;
5564
5565 node = rb_next(node);
5566 }
5567 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005568}
5569
Chris Masone02119d2008-09-05 16:13:11 -04005570static int btrfs_init_locked_inode(struct inode *inode, void *p)
5571{
5572 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005573 inode->i_ino = args->location->objectid;
5574 memcpy(&BTRFS_I(inode)->location, args->location,
5575 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005576 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005577 return 0;
5578}
5579
5580static int btrfs_find_actor(struct inode *inode, void *opaque)
5581{
5582 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005583 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005584 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005585}
5586
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005587static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005588 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005590{
5591 struct inode *inode;
5592 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005593 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005594
Chris Mason90d3e592014-01-09 17:28:00 -08005595 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005596 args.root = root;
5597
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005598 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005599 btrfs_init_locked_inode,
5600 (void *)&args);
5601 return inode;
5602}
5603
Balaji Rao1a54ef82008-07-21 02:01:04 +05305604/* Get an inode object given its location and corresponding root.
5605 * Returns in *is_new if the inode was read from disk
5606 */
5607struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005608 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305609{
5610 struct inode *inode;
5611
Chris Mason90d3e592014-01-09 17:28:00 -08005612 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305613 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005614 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305615
5616 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005617 int ret;
5618
5619 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005620 if (!is_bad_inode(inode)) {
5621 inode_tree_add(inode);
5622 unlock_new_inode(inode);
5623 if (new)
5624 *new = 1;
5625 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005626 unlock_new_inode(inode);
5627 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005628 ASSERT(ret < 0);
5629 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005630 }
5631 }
5632
Balaji Rao1a54ef82008-07-21 02:01:04 +05305633 return inode;
5634}
5635
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005636static struct inode *new_simple_dir(struct super_block *s,
5637 struct btrfs_key *key,
5638 struct btrfs_root *root)
5639{
5640 struct inode *inode = new_inode(s);
5641
5642 if (!inode)
5643 return ERR_PTR(-ENOMEM);
5644
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005645 BTRFS_I(inode)->root = root;
5646 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005647 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005648
5649 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005650 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005651 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652 inode->i_fop = &simple_dir_operations;
5653 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005654 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305655 inode->i_atime = inode->i_mtime;
5656 inode->i_ctime = inode->i_mtime;
5657 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005658
5659 return inode;
5660}
5661
Chris Mason3de45862008-11-17 21:02:50 -05005662struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005663{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005664 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005665 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005666 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005667 struct btrfs_root *sub_root = root;
5668 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005669 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005670 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005671
5672 if (dentry->d_name.len > BTRFS_NAME_LEN)
5673 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005674
Jeff Layton39e3c952012-11-28 11:30:53 -05005675 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005676 if (ret < 0)
5677 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005678
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005679 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005680 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005681
5682 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005683 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005684 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005685 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005686
5687 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5688
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005689 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005690 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005691 &location, &sub_root);
5692 if (ret < 0) {
5693 if (ret != -ENOENT)
5694 inode = ERR_PTR(ret);
5695 else
5696 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5697 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005698 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005699 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005700 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005701
Julia Lawall34d19ba2011-01-24 19:55:19 +00005702 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005703 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005704 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005705 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005706 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005707 if (ret) {
5708 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005709 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005710 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005711 }
5712
Chris Mason3de45862008-11-17 21:02:50 -05005713 return inode;
5714}
5715
Nick Pigginfe15ce42011-01-07 17:49:23 +11005716static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005717{
5718 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005719 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005720
Li Zefan848cce02012-02-21 17:04:28 +08005721 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005722 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005723
Li Zefan848cce02012-02-21 17:04:28 +08005724 if (inode) {
5725 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005726 if (btrfs_root_refs(&root->root_item) == 0)
5727 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005728
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005729 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005730 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005731 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005732 return 0;
5733}
5734
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005735static void btrfs_dentry_release(struct dentry *dentry)
5736{
Daeseok Youn944a4512014-04-14 15:37:02 +09005737 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005738}
5739
Chris Mason3de45862008-11-17 21:02:50 -05005740static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005741 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005742{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005743 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005744
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005745 inode = btrfs_lookup_dentry(dir, dentry);
5746 if (IS_ERR(inode)) {
5747 if (PTR_ERR(inode) == -ENOENT)
5748 inode = NULL;
5749 else
5750 return ERR_CAST(inode);
5751 }
5752
Al Viro41d28bc2014-10-12 22:24:21 -04005753 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005754}
5755
Miao Xie16cdcec2011-04-22 18:12:22 +08005756unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005757 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5758};
5759
Al Viro9cdda8d2013-05-22 16:48:09 -04005760static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005761{
Al Viro9cdda8d2013-05-22 16:48:09 -04005762 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005763 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005764 struct btrfs_root *root = BTRFS_I(inode)->root;
5765 struct btrfs_item *item;
5766 struct btrfs_dir_item *di;
5767 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005768 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005769 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005770 struct list_head ins_list;
5771 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005772 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005773 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005774 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005775 unsigned char d_type;
5776 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005777 char tmp_name[32];
5778 char *name_ptr;
5779 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005780 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005781 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005782
Al Viro9cdda8d2013-05-22 16:48:09 -04005783 if (!dir_emit_dots(file, ctx))
5784 return 0;
5785
David Woodhouse49593bf2008-08-17 17:08:36 +01005786 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005787 if (!path)
5788 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005789
David Sterbae4058b52015-11-27 16:31:35 +01005790 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005791
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005792 INIT_LIST_HEAD(&ins_list);
5793 INIT_LIST_HEAD(&del_list);
5794 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005795
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005796 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005797 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005798 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005799
Chris Mason39279cc2007-06-12 06:35:45 -04005800 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5801 if (ret < 0)
5802 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005803
5804 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005805 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005806 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005807 if (slot >= btrfs_header_nritems(leaf)) {
5808 ret = btrfs_next_leaf(root, path);
5809 if (ret < 0)
5810 goto err;
5811 else if (ret > 0)
5812 break;
5813 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005814 }
Chris Mason3de45862008-11-17 21:02:50 -05005815
Ross Kirkdd3cc162013-09-16 15:58:09 +01005816 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005817 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5818
5819 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005820 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005821 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005822 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005823 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005824 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005825 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005826 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005827
Al Viro9cdda8d2013-05-22 16:48:09 -04005828 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005829
Chris Mason39279cc2007-06-12 06:35:45 -04005830 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005831 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005832 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005833
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005834 name_len = btrfs_dir_name_len(leaf, di);
5835 if (name_len <= sizeof(tmp_name)) {
5836 name_ptr = tmp_name;
5837 } else {
5838 name_ptr = kmalloc(name_len, GFP_KERNEL);
5839 if (!name_ptr) {
5840 ret = -ENOMEM;
5841 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005842 }
Chris Mason39279cc2007-06-12 06:35:45 -04005843 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005844 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5845 name_len);
5846
5847 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5848 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5849
5850 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5851 d_type);
5852
5853 if (name_ptr != tmp_name)
5854 kfree(name_ptr);
5855
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005856 if (over)
5857 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005858 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005859next:
5860 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005861 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005862
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005863 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005864 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005865 goto nopos;
5866
Zach Browndb62efb2013-07-11 16:19:42 -07005867 /*
5868 * Stop new entries from being returned after we return the last
5869 * entry.
5870 *
5871 * New directory entries are assigned a strictly increasing
5872 * offset. This means that new entries created during readdir
5873 * are *guaranteed* to be seen in the future by that readdir.
5874 * This has broken buggy programs which operate on names as
5875 * they're returned by readdir. Until we re-use freed offsets
5876 * we have this hack to stop new entries from being returned
5877 * under the assumption that they'll never reach this huge
5878 * offset.
5879 *
5880 * This is being careful not to overflow 32bit loff_t unless the
5881 * last entry requires it because doing so has broken 32bit apps
5882 * in the past.
5883 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005884 if (ctx->pos >= INT_MAX)
5885 ctx->pos = LLONG_MAX;
5886 else
5887 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005888nopos:
5889 ret = 0;
5890err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005891 if (put)
5892 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005893 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005894 return ret;
5895}
5896
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005897int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005898{
5899 struct btrfs_root *root = BTRFS_I(inode)->root;
5900 struct btrfs_trans_handle *trans;
5901 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005902 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005903
Josef Bacik72ac3c02012-05-23 14:13:11 -04005904 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005905 return 0;
5906
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005907 if (btrfs_fs_closing(root->fs_info) &&
5908 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08005909 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005910
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005911 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005912 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005913 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005914 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005915 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005916 if (IS_ERR(trans))
5917 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005918 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005919 }
5920 return ret;
5921}
5922
5923/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005924 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005925 * inode changes. But, it is most likely to find the inode in cache.
5926 * FIXME, needs more benchmarking...there are no reasons other than performance
5927 * to keep or drop this code.
5928 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005929static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005930{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005931 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005932 struct btrfs_root *root = BTRFS_I(inode)->root;
5933 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005934 int ret;
5935
Josef Bacik72ac3c02012-05-23 14:13:11 -04005936 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005937 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005938
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005939 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005940 if (IS_ERR(trans))
5941 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005942
5943 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005944 if (ret && ret == -ENOSPC) {
5945 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005946 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005947 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005948 if (IS_ERR(trans))
5949 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005950
Chris Mason94b60442010-05-26 11:02:00 -04005951 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005952 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005953 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005954 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005955 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005956
5957 return ret;
5958}
5959
5960/*
5961 * This is a copy of file_update_time. We need this so we can return error on
5962 * ENOSPC for updating the inode in the case of file write and mmap writes.
5963 */
Josef Bacike41f9412012-03-26 09:46:47 -04005964static int btrfs_update_time(struct inode *inode, struct timespec *now,
5965 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005966{
Alexander Block2bc5565282012-06-15 09:49:33 +02005967 struct btrfs_root *root = BTRFS_I(inode)->root;
5968
5969 if (btrfs_root_readonly(root))
5970 return -EROFS;
5971
Josef Bacike41f9412012-03-26 09:46:47 -04005972 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005973 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005974 if (flags & S_CTIME)
5975 inode->i_ctime = *now;
5976 if (flags & S_MTIME)
5977 inode->i_mtime = *now;
5978 if (flags & S_ATIME)
5979 inode->i_atime = *now;
5980 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005981}
5982
Chris Masond352ac62008-09-29 15:18:18 -04005983/*
5984 * find the highest existing sequence number in a directory
5985 * and then set the in-memory index_cnt variable to reflect
5986 * free sequence numbers
5987 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005988static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005989{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005990 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005991 struct btrfs_key key, found_key;
5992 struct btrfs_path *path;
5993 struct extent_buffer *leaf;
5994 int ret;
5995
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005996 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005997 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005998 key.offset = (u64)-1;
5999
6000 path = btrfs_alloc_path();
6001 if (!path)
6002 return -ENOMEM;
6003
6004 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6005 if (ret < 0)
6006 goto out;
6007 /* FIXME: we should be able to handle this */
6008 if (ret == 0)
6009 goto out;
6010 ret = 0;
6011
6012 /*
6013 * MAGIC NUMBER EXPLANATION:
6014 * since we search a directory based on f_pos we have to start at 2
6015 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6016 * else has to start at 2
6017 */
6018 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006019 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006020 goto out;
6021 }
6022
6023 path->slots[0]--;
6024
6025 leaf = path->nodes[0];
6026 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6027
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006028 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006029 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006030 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006031 goto out;
6032 }
6033
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006034 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006035out:
6036 btrfs_free_path(path);
6037 return ret;
6038}
6039
Chris Masond352ac62008-09-29 15:18:18 -04006040/*
6041 * helper to find a free sequence number in a given directory. This current
6042 * code is very simple, later versions will do smarter things in the btree
6043 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006044int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006045{
6046 int ret = 0;
6047
Nikolay Borisov877574e2017-02-20 13:50:33 +02006048 if (dir->index_cnt == (u64)-1) {
6049 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006050 if (ret) {
Nikolay Borisov877574e2017-02-20 13:50:33 +02006051 ret = btrfs_set_inode_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006052 if (ret)
6053 return ret;
6054 }
Josef Bacikaec74772008-07-24 12:12:38 -04006055 }
6056
Nikolay Borisov877574e2017-02-20 13:50:33 +02006057 *index = dir->index_cnt;
6058 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006059
6060 return ret;
6061}
6062
Chris Masonb0d5d102014-09-08 13:08:51 -07006063static int btrfs_insert_inode_locked(struct inode *inode)
6064{
6065 struct btrfs_iget_args args;
6066 args.location = &BTRFS_I(inode)->location;
6067 args.root = BTRFS_I(inode)->root;
6068
6069 return insert_inode_locked4(inode,
6070 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6071 btrfs_find_actor, &args);
6072}
6073
Chris Mason39279cc2007-06-12 06:35:45 -04006074static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6075 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006076 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006077 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006078 u64 ref_objectid, u64 objectid,
6079 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006080{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006081 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006082 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006083 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006084 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006085 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006086 struct btrfs_inode_ref *ref;
6087 struct btrfs_key key[2];
6088 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006089 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006090 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006091 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006092
Chris Mason5f39d392007-10-15 16:14:19 -04006093 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006094 if (!path)
6095 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006096
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006097 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006098 if (!inode) {
6099 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006100 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006101 }
Chris Mason39279cc2007-06-12 06:35:45 -04006102
Li Zefan581bb052011-04-20 10:06:11 +08006103 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006104 * O_TMPFILE, set link count to 0, so that after this point,
6105 * we fill in an inode item with the correct link count.
6106 */
6107 if (!name)
6108 set_nlink(inode, 0);
6109
6110 /*
Li Zefan581bb052011-04-20 10:06:11 +08006111 * we have to initialize this early, so we can reclaim the inode
6112 * number if we fail afterwards in this function.
6113 */
6114 inode->i_ino = objectid;
6115
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006116 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006117 trace_btrfs_inode_request(dir);
6118
Nikolay Borisov877574e2017-02-20 13:50:33 +02006119 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006120 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006121 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006122 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006123 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006124 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006125 } else if (dir) {
6126 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006127 }
6128 /*
6129 * index_cnt is ignored for everything but a dir,
6130 * btrfs_get_inode_index_count has an explanation for the magic
6131 * number
6132 */
6133 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006134 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006135 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006136 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006137 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006138
Josef Bacik5dc562c2012-08-17 13:14:17 -04006139 /*
6140 * We could have gotten an inode number from somebody who was fsynced
6141 * and then removed in this same transaction, so let's just set full
6142 * sync since it will be a full sync anyway and this will blow away the
6143 * old info in the log.
6144 */
6145 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6146
Chris Mason9c583092008-01-29 15:15:18 -05006147 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006148 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006149 key[0].offset = 0;
6150
Chris Mason9c583092008-01-29 15:15:18 -05006151 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006152
6153 if (name) {
6154 /*
6155 * Start new inodes with an inode_ref. This is slightly more
6156 * efficient for small numbers of hard links since they will
6157 * be packed into one item. Extended refs will kick in if we
6158 * add more hard links than can fit in the ref item.
6159 */
6160 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006161 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006162 key[1].offset = ref_objectid;
6163
6164 sizes[1] = name_len + sizeof(*ref);
6165 }
Chris Mason9c583092008-01-29 15:15:18 -05006166
Chris Masonb0d5d102014-09-08 13:08:51 -07006167 location = &BTRFS_I(inode)->location;
6168 location->objectid = objectid;
6169 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006170 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006171
6172 ret = btrfs_insert_inode_locked(inode);
6173 if (ret < 0)
6174 goto fail;
6175
Chris Masonb9473432009-03-13 11:00:37 -04006176 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006177 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006178 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006179 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006180
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006181 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006182 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306183
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006184 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306185 inode->i_atime = inode->i_mtime;
6186 inode->i_ctime = inode->i_mtime;
6187 BTRFS_I(inode)->i_otime = inode->i_mtime;
6188
Chris Mason5f39d392007-10-15 16:14:19 -04006189 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6190 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006191 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006192 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006193 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006194
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006195 if (name) {
6196 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6197 struct btrfs_inode_ref);
6198 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6199 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6200 ptr = (unsigned long)(ref + 1);
6201 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6202 }
Chris Mason9c583092008-01-29 15:15:18 -05006203
Chris Mason5f39d392007-10-15 16:14:19 -04006204 btrfs_mark_buffer_dirty(path->nodes[0]);
6205 btrfs_free_path(path);
6206
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006207 btrfs_inherit_iflags(inode, dir);
6208
Al Viro569254b2011-07-24 17:08:40 -04006209 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006210 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006211 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006212 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006213 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6214 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006215 }
6216
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006217 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006218
6219 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006220 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006221
Alexander Block8ea05e32012-07-25 17:35:53 +02006222 btrfs_update_root_times(trans, root);
6223
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006224 ret = btrfs_inode_inherit_props(trans, inode, dir);
6225 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006226 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006227 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006228 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006229
Chris Mason39279cc2007-06-12 06:35:45 -04006230 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006231
6232fail_unlock:
6233 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006234fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006235 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006236 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006237 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006238 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006239 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006240}
6241
6242static inline u8 btrfs_inode_type(struct inode *inode)
6243{
6244 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6245}
6246
Chris Masond352ac62008-09-29 15:18:18 -04006247/*
6248 * utility function to add 'inode' into 'parent_inode' with
6249 * a give name and a given sequence number.
6250 * if 'add_backref' is true, also insert a backref from the
6251 * inode to the parent directory.
6252 */
Chris Masone02119d2008-09-05 16:13:11 -04006253int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006254 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006255 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006256{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006257 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006258 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006259 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006260 struct btrfs_root *root = parent_inode->root;
6261 u64 ino = btrfs_ino(inode);
6262 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006263
Li Zefan33345d012011-04-20 10:31:50 +08006264 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006265 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006266 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006267 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006268 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006269 key.offset = 0;
6270 }
Chris Mason39279cc2007-06-12 06:35:45 -04006271
Li Zefan33345d012011-04-20 10:31:50 +08006272 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006273 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6274 root->root_key.objectid, parent_ino,
6275 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006276 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006277 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6278 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006279 }
6280
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006281 /* Nothing to clean up yet */
6282 if (ret)
6283 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006284
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006285 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006286 parent_inode, &key,
6287 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006288 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006289 goto fail_dir_item;
6290 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006291 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006292 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006293 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006294
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006295 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006296 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006297 inode_inc_iversion(&parent_inode->vfs_inode);
6298 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6299 current_time(&parent_inode->vfs_inode);
6300 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006301 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006302 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006303 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006304
6305fail_dir_item:
6306 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6307 u64 local_index;
6308 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006309 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6310 root->root_key.objectid, parent_ino,
6311 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006312
6313 } else if (add_backref) {
6314 u64 local_index;
6315 int err;
6316
6317 err = btrfs_del_inode_ref(trans, root, name, name_len,
6318 ino, parent_ino, &local_index);
6319 }
6320 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006321}
6322
6323static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006324 struct btrfs_inode *dir, struct dentry *dentry,
6325 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006326{
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006327 int err = btrfs_add_link(trans, dir, inode,
Josef Bacika1b075d2010-11-19 20:36:11 +00006328 dentry->d_name.name, dentry->d_name.len,
6329 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006330 if (err > 0)
6331 err = -EEXIST;
6332 return err;
6333}
6334
Josef Bacik618e21d2007-07-11 10:18:17 -04006335static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006336 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006337{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006338 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006339 struct btrfs_trans_handle *trans;
6340 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006341 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006342 int err;
6343 int drop_inode = 0;
6344 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006345 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006346
Josef Bacik9ed74f22009-09-11 16:12:44 -04006347 /*
6348 * 2 for inode item and ref
6349 * 2 for dir items
6350 * 1 for xattr if selinux is on
6351 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006352 trans = btrfs_start_transaction(root, 5);
6353 if (IS_ERR(trans))
6354 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006355
Li Zefan581bb052011-04-20 10:06:11 +08006356 err = btrfs_find_free_ino(root, &objectid);
6357 if (err)
6358 goto out_unlock;
6359
Josef Bacikaec74772008-07-24 12:12:38 -04006360 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006361 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6362 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006363 if (IS_ERR(inode)) {
6364 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006365 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006366 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006367
Casey Schauflerad19db72011-12-15 10:09:07 -05006368 /*
6369 * If the active LSM wants to access the inode during
6370 * d_instantiate it needs these. Smack checks to see
6371 * if the filesystem supports xattrs by looking at the
6372 * ops vector.
6373 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006374 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006375 init_special_inode(inode, inode->i_mode, rdev);
6376
6377 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006378 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006379 goto out_unlock_inode;
6380
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006381 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6382 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006383 if (err) {
6384 goto out_unlock_inode;
6385 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006386 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006387 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006388 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006389 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006390
Josef Bacik618e21d2007-07-11 10:18:17 -04006391out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006392 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006393 btrfs_balance_delayed_items(fs_info);
6394 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006395 if (drop_inode) {
6396 inode_dec_link_count(inode);
6397 iput(inode);
6398 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006399 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006400
6401out_unlock_inode:
6402 drop_inode = 1;
6403 unlock_new_inode(inode);
6404 goto out_unlock;
6405
Josef Bacik618e21d2007-07-11 10:18:17 -04006406}
6407
Chris Mason39279cc2007-06-12 06:35:45 -04006408static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006409 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006410{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006411 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006412 struct btrfs_trans_handle *trans;
6413 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006414 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006415 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006416 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006417 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006418 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006419
Josef Bacik9ed74f22009-09-11 16:12:44 -04006420 /*
6421 * 2 for inode item and ref
6422 * 2 for dir items
6423 * 1 for xattr if selinux is on
6424 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006425 trans = btrfs_start_transaction(root, 5);
6426 if (IS_ERR(trans))
6427 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006428
Li Zefan581bb052011-04-20 10:06:11 +08006429 err = btrfs_find_free_ino(root, &objectid);
6430 if (err)
6431 goto out_unlock;
6432
Josef Bacikaec74772008-07-24 12:12:38 -04006433 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006434 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6435 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006436 if (IS_ERR(inode)) {
6437 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006438 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006439 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006440 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006441 /*
6442 * If the active LSM wants to access the inode during
6443 * d_instantiate it needs these. Smack checks to see
6444 * if the filesystem supports xattrs by looking at the
6445 * ops vector.
6446 */
6447 inode->i_fop = &btrfs_file_operations;
6448 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006449 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006450
6451 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6452 if (err)
6453 goto out_unlock_inode;
6454
6455 err = btrfs_update_inode(trans, root, inode);
6456 if (err)
6457 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006458
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006459 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6460 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006461 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006462 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006463
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006464 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006465 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006466 d_instantiate(dentry, inode);
6467
Chris Mason39279cc2007-06-12 06:35:45 -04006468out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006469 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006470 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006471 inode_dec_link_count(inode);
6472 iput(inode);
6473 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006474 btrfs_balance_delayed_items(fs_info);
6475 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006476 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006477
6478out_unlock_inode:
6479 unlock_new_inode(inode);
6480 goto out_unlock;
6481
Chris Mason39279cc2007-06-12 06:35:45 -04006482}
6483
6484static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6485 struct dentry *dentry)
6486{
Filipe Manana271dba42016-01-05 16:24:05 +00006487 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006488 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006489 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006490 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006491 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006492 int err;
6493 int drop_inode = 0;
6494
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006495 /* do not allow sys_link's with other subvols of the same device */
6496 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006497 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006498
Mark Fashehf1863732012-08-08 11:32:27 -07006499 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006500 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006501
Nikolay Borisov877574e2017-02-20 13:50:33 +02006502 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006503 if (err)
6504 goto fail;
6505
Yan, Zhenga22285a2010-05-16 10:48:46 -04006506 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006507 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006508 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006509 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006510 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006511 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006512 if (IS_ERR(trans)) {
6513 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006514 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006515 goto fail;
6516 }
Chris Mason5f39d392007-10-15 16:14:19 -04006517
Miao Xie67de1172013-12-26 13:07:06 +08006518 /* There are several dir indexes for this inode, clear the cache. */
6519 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006520 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006521 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006522 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006523 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006524 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006525
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006526 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6527 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006528
Yan, Zhenga5719522009-09-24 09:17:31 -04006529 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006530 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006531 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006532 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006533 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006534 if (err)
6535 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006536 if (inode->i_nlink == 1) {
6537 /*
6538 * If new hard link count is 1, it's a file created
6539 * with open(2) O_TMPFILE flag.
6540 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006541 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006542 if (err)
6543 goto fail;
6544 }
Al Viro08c422c2011-12-23 07:58:13 -05006545 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006546 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006547 }
Chris Mason39279cc2007-06-12 06:35:45 -04006548
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006549 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006550fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006551 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006552 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006553 if (drop_inode) {
6554 inode_dec_link_count(inode);
6555 iput(inode);
6556 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006557 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006558 return err;
6559}
6560
Al Viro18bb1db2011-07-26 01:41:39 -04006561static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006562{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006563 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006564 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006565 struct btrfs_trans_handle *trans;
6566 struct btrfs_root *root = BTRFS_I(dir)->root;
6567 int err = 0;
6568 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006569 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006570 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006571
Josef Bacik9ed74f22009-09-11 16:12:44 -04006572 /*
6573 * 2 items for inode and ref
6574 * 2 items for dir items
6575 * 1 for xattr if selinux is on
6576 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006577 trans = btrfs_start_transaction(root, 5);
6578 if (IS_ERR(trans))
6579 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006580
Li Zefan581bb052011-04-20 10:06:11 +08006581 err = btrfs_find_free_ino(root, &objectid);
6582 if (err)
6583 goto out_fail;
6584
Josef Bacikaec74772008-07-24 12:12:38 -04006585 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006586 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6587 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006588 if (IS_ERR(inode)) {
6589 err = PTR_ERR(inode);
6590 goto out_fail;
6591 }
Chris Mason5f39d392007-10-15 16:14:19 -04006592
Chris Mason39279cc2007-06-12 06:35:45 -04006593 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006594 /* these must be set before we unlock the inode */
6595 inode->i_op = &btrfs_dir_inode_operations;
6596 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006597
Eric Paris2a7dba32011-02-01 11:05:39 -05006598 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006599 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006600 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006601
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006602 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006603 err = btrfs_update_inode(trans, root, inode);
6604 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006605 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006606
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006607 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6608 dentry->d_name.name,
6609 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006610 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006611 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006612
Chris Mason39279cc2007-06-12 06:35:45 -04006613 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006614 /*
6615 * mkdir is special. We're unlocking after we call d_instantiate
6616 * to avoid a race with nfsd calling d_instantiate.
6617 */
6618 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006619 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006620
6621out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006622 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006623 if (drop_on_err) {
6624 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006625 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006626 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006627 btrfs_balance_delayed_items(fs_info);
6628 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006629 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006630
6631out_fail_inode:
6632 unlock_new_inode(inode);
6633 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006634}
6635
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006636/* Find next extent map of a given extent map, caller needs to ensure locks */
6637static struct extent_map *next_extent_map(struct extent_map *em)
6638{
6639 struct rb_node *next;
6640
6641 next = rb_next(&em->rb_node);
6642 if (!next)
6643 return NULL;
6644 return container_of(next, struct extent_map, rb_node);
6645}
6646
6647static struct extent_map *prev_extent_map(struct extent_map *em)
6648{
6649 struct rb_node *prev;
6650
6651 prev = rb_prev(&em->rb_node);
6652 if (!prev)
6653 return NULL;
6654 return container_of(prev, struct extent_map, rb_node);
6655}
6656
Chris Masond352ac62008-09-29 15:18:18 -04006657/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006658 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006659 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006660 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006661 */
Chris Mason3b951512008-04-17 11:29:12 -04006662static int merge_extent_mapping(struct extent_map_tree *em_tree,
6663 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006664 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006665 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006666{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006667 struct extent_map *prev;
6668 struct extent_map *next;
6669 u64 start;
6670 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006671 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006672
Chris Masone6dcd2d2008-07-17 12:53:50 -04006673 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006674
6675 if (existing->start > map_start) {
6676 next = existing;
6677 prev = prev_extent_map(next);
6678 } else {
6679 prev = existing;
6680 next = next_extent_map(prev);
6681 }
6682
6683 start = prev ? extent_map_end(prev) : em->start;
6684 start = max_t(u64, start, em->start);
6685 end = next ? next->start : extent_map_end(em);
6686 end = min_t(u64, end, extent_map_end(em));
6687 start_diff = start - em->start;
6688 em->start = start;
6689 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006690 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6691 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006692 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006693 em->block_len -= start_diff;
6694 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006695 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006696}
6697
Chris Masonc8b97812008-10-29 14:49:59 -04006698static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006699 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006700 size_t pg_offset, u64 extent_offset,
6701 struct btrfs_file_extent_item *item)
6702{
6703 int ret;
6704 struct extent_buffer *leaf = path->nodes[0];
6705 char *tmp;
6706 size_t max_size;
6707 unsigned long inline_size;
6708 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006709 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006710
6711 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006712 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006713 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6714 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006715 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006716 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006717 if (!tmp)
6718 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006719 ptr = btrfs_file_extent_inline_start(item);
6720
6721 read_extent_buffer(leaf, tmp, ptr, inline_size);
6722
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006723 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006724 ret = btrfs_decompress(compress_type, tmp, page,
6725 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006726 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006727 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006728}
6729
Chris Masond352ac62008-09-29 15:18:18 -04006730/*
6731 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006732 * the ugly parts come from merging extents from the disk with the in-ram
6733 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006734 * where the in-ram extents might be locked pending data=ordered completion.
6735 *
6736 * This also copies inline extents directly into the page.
6737 */
Chris Masond3977122009-01-05 21:25:51 -05006738
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006739struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6740 struct page *page,
6741 size_t pg_offset, u64 start, u64 len,
6742 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006743{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006744 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006745 int ret;
6746 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006747 u64 extent_start = 0;
6748 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006749 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006750 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006751 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006752 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006753 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006754 struct extent_buffer *leaf;
6755 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006756 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006757 struct extent_map_tree *em_tree = &inode->extent_tree;
6758 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006759 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006760 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006761
Chris Masona52d9a82007-08-27 16:49:44 -04006762again:
Chris Mason890871b2009-09-02 16:24:52 -04006763 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006764 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006765 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006766 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006767 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006768
Chris Masona52d9a82007-08-27 16:49:44 -04006769 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006770 if (em->start > start || em->start + em->len <= start)
6771 free_extent_map(em);
6772 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006773 free_extent_map(em);
6774 else
6775 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006776 }
David Sterba172ddd62011-04-21 00:48:27 +02006777 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006778 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006779 err = -ENOMEM;
6780 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006781 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006782 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006783 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006784 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006785 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006786 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006787
6788 if (!path) {
6789 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006790 if (!path) {
6791 err = -ENOMEM;
6792 goto out;
6793 }
6794 /*
6795 * Chances are we'll be called again, so go ahead and do
6796 * readahead
6797 */
David Sterbae4058b52015-11-27 16:31:35 +01006798 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006799 }
6800
Chris Mason179e29e2007-11-01 11:28:41 -04006801 ret = btrfs_lookup_file_extent(trans, root, path,
6802 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006803 if (ret < 0) {
6804 err = ret;
6805 goto out;
6806 }
6807
6808 if (ret != 0) {
6809 if (path->slots[0] == 0)
6810 goto not_found;
6811 path->slots[0]--;
6812 }
6813
Chris Mason5f39d392007-10-15 16:14:19 -04006814 leaf = path->nodes[0];
6815 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006816 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006817 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006818 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006819 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006820 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006821 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006822 /*
6823 * If we backup past the first extent we want to move forward
6824 * and see if there is an extent in front of us, otherwise we'll
6825 * say there is a hole for our whole search range which can
6826 * cause problems.
6827 */
6828 extent_end = start;
6829 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006830 }
6831
Chris Mason5f39d392007-10-15 16:14:19 -04006832 found_type = btrfs_file_extent_type(leaf, item);
6833 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006834 if (found_type == BTRFS_FILE_EXTENT_REG ||
6835 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006836 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006837 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006838 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6839 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006840 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006841 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006842 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006843 }
Josef Bacik25a50342013-10-14 12:08:38 -04006844next:
Yan Zheng9036c102008-10-30 14:19:41 -04006845 if (start >= extent_end) {
6846 path->slots[0]++;
6847 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6848 ret = btrfs_next_leaf(root, path);
6849 if (ret < 0) {
6850 err = ret;
6851 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006852 }
Yan Zheng9036c102008-10-30 14:19:41 -04006853 if (ret > 0)
6854 goto not_found;
6855 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006856 }
Yan Zheng9036c102008-10-30 14:19:41 -04006857 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6858 if (found_key.objectid != objectid ||
6859 found_key.type != BTRFS_EXTENT_DATA_KEY)
6860 goto not_found;
6861 if (start + len <= found_key.offset)
6862 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006863 if (start > found_key.offset)
6864 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006865 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006866 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006867 em->len = found_key.offset - start;
6868 goto not_found_em;
6869 }
6870
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006871 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02006872 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006873
Yan Zhengd899e052008-10-30 14:25:28 -04006874 if (found_type == BTRFS_FILE_EXTENT_REG ||
6875 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006876 goto insert;
6877 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006878 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006879 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006880 size_t size;
6881 size_t extent_offset;
6882 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006883
Filipe Manana7ffbb592014-06-09 03:48:05 +01006884 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006885 goto out;
Yan689f9342007-10-29 11:41:07 -04006886
Chris Mason514ac8a2014-01-03 21:07:00 -08006887 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006888 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006889 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6890 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006891 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006892 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006893 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006894 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006895 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006896 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006897 if (btrfs_file_extent_compression(leaf, item) !=
6898 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006899 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006900 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006901 if (ret) {
6902 err = ret;
6903 goto out;
6904 }
Chris Masonc8b97812008-10-29 14:49:59 -04006905 } else {
6906 map = kmap(page);
6907 read_extent_buffer(leaf, map + pg_offset, ptr,
6908 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006909 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006910 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006911 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006912 copy_size);
6913 }
Chris Masonc8b97812008-10-29 14:49:59 -04006914 kunmap(page);
6915 }
Chris Mason179e29e2007-11-01 11:28:41 -04006916 flush_dcache_page(page);
6917 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006918 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006919 if (!trans) {
6920 kunmap(page);
6921 free_extent_map(em);
6922 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006923
David Sterbab3b4aa72011-04-21 01:20:15 +02006924 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006925 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006926
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006927 if (IS_ERR(trans))
6928 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006929 goto again;
6930 }
Chris Masonc8b97812008-10-29 14:49:59 -04006931 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006932 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006933 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006934 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006935 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006936 }
Chris Masond1310b22008-01-24 16:13:08 -05006937 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006938 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006939 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006940 }
6941not_found:
6942 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006943 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006944 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006945not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006946 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006947 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006948insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006949 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006950 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006951 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006952 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6953 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006954 err = -EIO;
6955 goto out;
6956 }
Chris Masond1310b22008-01-24 16:13:08 -05006957
6958 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006959 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006960 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006961 /* it is possible that someone inserted the extent into the tree
6962 * while we had the lock dropped. It is also possible that
6963 * an overlapping map exists in the tree
6964 */
Chris Masona52d9a82007-08-27 16:49:44 -04006965 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006966 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006967
6968 ret = 0;
6969
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006970 existing = search_extent_mapping(em_tree, start, len);
6971 /*
6972 * existing will always be non-NULL, since there must be
6973 * extent causing the -EEXIST.
6974 */
Chris Mason8dff9c82015-09-19 11:28:25 -07006975 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006976 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07006977 em->block_start == existing->block_start) {
6978 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006979 * The existing extent map already encompasses the
6980 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07006981 */
6982 free_extent_map(em);
6983 em = existing;
6984 err = 0;
6985
6986 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006987 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006988 /*
6989 * The existing extent map is the one nearest to
6990 * the [start, start + len) range which overlaps
6991 */
6992 err = merge_extent_mapping(em_tree, existing,
6993 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006994 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006995 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006996 free_extent_map(em);
6997 em = NULL;
6998 }
6999 } else {
7000 free_extent_map(em);
7001 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007002 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007003 }
Chris Masona52d9a82007-08-27 16:49:44 -04007004 }
Chris Mason890871b2009-09-02 16:24:52 -04007005 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007006out:
liubo1abe9b82011-03-24 11:18:59 +00007007
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007008 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007009
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007010 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007011 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007012 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007013 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007014 err = ret;
7015 }
Chris Masona52d9a82007-08-27 16:49:44 -04007016 if (err) {
7017 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007018 return ERR_PTR(err);
7019 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007020 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007021 return em;
7022}
7023
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007024struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7025 struct page *page,
7026 size_t pg_offset, u64 start, u64 len,
7027 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007028{
7029 struct extent_map *em;
7030 struct extent_map *hole_em = NULL;
7031 u64 range_start = start;
7032 u64 end;
7033 u64 found;
7034 u64 found_end;
7035 int err = 0;
7036
7037 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7038 if (IS_ERR(em))
7039 return em;
7040 if (em) {
7041 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007042 * if our em maps to
7043 * - a hole or
7044 * - a pre-alloc extent,
7045 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007046 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007047 if (em->block_start != EXTENT_MAP_HOLE &&
7048 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007049 return em;
7050 else
7051 hole_em = em;
7052 }
7053
7054 /* check to see if we've wrapped (len == -1 or similar) */
7055 end = start + len;
7056 if (end < start)
7057 end = (u64)-1;
7058 else
7059 end -= 1;
7060
7061 em = NULL;
7062
7063 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007064 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007065 end, len, EXTENT_DELALLOC, 1);
7066 found_end = range_start + found;
7067 if (found_end < range_start)
7068 found_end = (u64)-1;
7069
7070 /*
7071 * we didn't find anything useful, return
7072 * the original results from get_extent()
7073 */
7074 if (range_start > end || found_end <= start) {
7075 em = hole_em;
7076 hole_em = NULL;
7077 goto out;
7078 }
7079
7080 /* adjust the range_start to make sure it doesn't
7081 * go backwards from the start they passed in
7082 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307083 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007084 found = found_end - range_start;
7085
7086 if (found > 0) {
7087 u64 hole_start = start;
7088 u64 hole_len = len;
7089
David Sterba172ddd62011-04-21 00:48:27 +02007090 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007091 if (!em) {
7092 err = -ENOMEM;
7093 goto out;
7094 }
7095 /*
7096 * when btrfs_get_extent can't find anything it
7097 * returns one huge hole
7098 *
7099 * make sure what it found really fits our range, and
7100 * adjust to make sure it is based on the start from
7101 * the caller
7102 */
7103 if (hole_em) {
7104 u64 calc_end = extent_map_end(hole_em);
7105
7106 if (calc_end <= start || (hole_em->start > end)) {
7107 free_extent_map(hole_em);
7108 hole_em = NULL;
7109 } else {
7110 hole_start = max(hole_em->start, start);
7111 hole_len = calc_end - hole_start;
7112 }
7113 }
7114 em->bdev = NULL;
7115 if (hole_em && range_start > hole_start) {
7116 /* our hole starts before our delalloc, so we
7117 * have to return just the parts of the hole
7118 * that go until the delalloc starts
7119 */
7120 em->len = min(hole_len,
7121 range_start - hole_start);
7122 em->start = hole_start;
7123 em->orig_start = hole_start;
7124 /*
7125 * don't adjust block start at all,
7126 * it is fixed at EXTENT_MAP_HOLE
7127 */
7128 em->block_start = hole_em->block_start;
7129 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007130 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7131 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007132 } else {
7133 em->start = range_start;
7134 em->len = found;
7135 em->orig_start = range_start;
7136 em->block_start = EXTENT_MAP_DELALLOC;
7137 em->block_len = found;
7138 }
7139 } else if (hole_em) {
7140 return hole_em;
7141 }
7142out:
7143
7144 free_extent_map(hole_em);
7145 if (err) {
7146 free_extent_map(em);
7147 return ERR_PTR(err);
7148 }
7149 return em;
7150}
7151
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007152static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7153 const u64 start,
7154 const u64 len,
7155 const u64 orig_start,
7156 const u64 block_start,
7157 const u64 block_len,
7158 const u64 orig_block_len,
7159 const u64 ram_bytes,
7160 const int type)
7161{
7162 struct extent_map *em = NULL;
7163 int ret;
7164
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007165 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007166 em = create_io_em(inode, start, len, orig_start,
7167 block_start, block_len, orig_block_len,
7168 ram_bytes,
7169 BTRFS_COMPRESS_NONE, /* compress_type */
7170 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007171 if (IS_ERR(em))
7172 goto out;
7173 }
7174 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7175 len, block_len, type);
7176 if (ret) {
7177 if (em) {
7178 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007179 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007180 start + len - 1, 0);
7181 }
7182 em = ERR_PTR(ret);
7183 }
7184 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007185
7186 return em;
7187}
7188
Josef Bacik4b46fce2010-05-23 11:00:55 -04007189static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7190 u64 start, u64 len)
7191{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007192 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007193 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007194 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007195 struct btrfs_key ins;
7196 u64 alloc_hint;
7197 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007198
Josef Bacik4b46fce2010-05-23 11:00:55 -04007199 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007200 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007201 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007202 if (ret)
7203 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007204
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007205 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7206 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007207 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007208 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007209 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007210 btrfs_free_reserved_extent(fs_info, ins.objectid,
7211 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007212
Josef Bacik4b46fce2010-05-23 11:00:55 -04007213 return em;
7214}
7215
Chris Mason46bfbb52010-05-26 11:04:10 -04007216/*
7217 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7218 * block must be cow'd
7219 */
Josef Bacik00361582013-08-14 14:02:47 -04007220noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007221 u64 *orig_start, u64 *orig_block_len,
7222 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007223{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007224 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007225 struct btrfs_path *path;
7226 int ret;
7227 struct extent_buffer *leaf;
7228 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007229 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007230 struct btrfs_file_extent_item *fi;
7231 struct btrfs_key key;
7232 u64 disk_bytenr;
7233 u64 backref_offset;
7234 u64 extent_end;
7235 u64 num_bytes;
7236 int slot;
7237 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007238 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007239
Chris Mason46bfbb52010-05-26 11:04:10 -04007240 path = btrfs_alloc_path();
7241 if (!path)
7242 return -ENOMEM;
7243
David Sterbaf85b7372017-01-20 14:54:07 +01007244 ret = btrfs_lookup_file_extent(NULL, root, path,
7245 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007246 if (ret < 0)
7247 goto out;
7248
7249 slot = path->slots[0];
7250 if (ret == 1) {
7251 if (slot == 0) {
7252 /* can't find the item, must cow */
7253 ret = 0;
7254 goto out;
7255 }
7256 slot--;
7257 }
7258 ret = 0;
7259 leaf = path->nodes[0];
7260 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007261 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007262 key.type != BTRFS_EXTENT_DATA_KEY) {
7263 /* not our file or wrong item type, must cow */
7264 goto out;
7265 }
7266
7267 if (key.offset > offset) {
7268 /* Wrong offset, must cow */
7269 goto out;
7270 }
7271
7272 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7273 found_type = btrfs_file_extent_type(leaf, fi);
7274 if (found_type != BTRFS_FILE_EXTENT_REG &&
7275 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7276 /* not a regular extent, must cow */
7277 goto out;
7278 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007279
7280 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7281 goto out;
7282
Miao Xiee77751a2013-12-27 21:11:50 +08007283 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7284 if (extent_end <= offset)
7285 goto out;
7286
Chris Mason46bfbb52010-05-26 11:04:10 -04007287 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007288 if (disk_bytenr == 0)
7289 goto out;
7290
7291 if (btrfs_file_extent_compression(leaf, fi) ||
7292 btrfs_file_extent_encryption(leaf, fi) ||
7293 btrfs_file_extent_other_encoding(leaf, fi))
7294 goto out;
7295
Chris Mason46bfbb52010-05-26 11:04:10 -04007296 backref_offset = btrfs_file_extent_offset(leaf, fi);
7297
Josef Bacik7ee9e442013-06-21 16:37:03 -04007298 if (orig_start) {
7299 *orig_start = key.offset - backref_offset;
7300 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7301 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7302 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007303
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007304 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007305 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007306
7307 num_bytes = min(offset + *len, extent_end) - offset;
7308 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7309 u64 range_end;
7310
Jeff Mahoneyda170662016-06-15 09:22:56 -04007311 range_end = round_up(offset + num_bytes,
7312 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007313 ret = test_range_bit(io_tree, offset, range_end,
7314 EXTENT_DELALLOC, 0, NULL);
7315 if (ret) {
7316 ret = -EAGAIN;
7317 goto out;
7318 }
7319 }
7320
Josef Bacik1bda19e2013-10-18 12:10:36 -04007321 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007322
7323 /*
7324 * look for other files referencing this extent, if we
7325 * find any we must cow
7326 */
Josef Bacik00361582013-08-14 14:02:47 -04007327
Liu Boe4c3b2d2017-01-30 12:25:28 -08007328 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007329 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007330 if (ret) {
7331 ret = 0;
7332 goto out;
7333 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007334
7335 /*
7336 * adjust disk_bytenr and num_bytes to cover just the bytes
7337 * in this extent we are about to write. If there
7338 * are any csums in that range we have to cow in order
7339 * to keep the csums correct
7340 */
7341 disk_bytenr += backref_offset;
7342 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007343 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7344 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007345 /*
7346 * all of the above have passed, it is safe to overwrite this extent
7347 * without cow
7348 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007349 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007350 ret = 1;
7351out:
7352 btrfs_free_path(path);
7353 return ret;
7354}
7355
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007356bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7357{
7358 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7359 int found = false;
7360 void **pagep = NULL;
7361 struct page *page = NULL;
7362 int start_idx;
7363 int end_idx;
7364
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007365 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007366
7367 /*
7368 * end is the last byte in the last page. end == start is legal
7369 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007370 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007371
7372 rcu_read_lock();
7373
7374 /* Most of the code in this while loop is lifted from
7375 * find_get_page. It's been modified to begin searching from a
7376 * page and return just the first page found in that range. If the
7377 * found idx is less than or equal to the end idx then we know that
7378 * a page exists. If no pages are found or if those pages are
7379 * outside of the range then we're fine (yay!) */
7380 while (page == NULL &&
7381 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7382 page = radix_tree_deref_slot(pagep);
7383 if (unlikely(!page))
7384 break;
7385
7386 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007387 if (radix_tree_deref_retry(page)) {
7388 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007389 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007390 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007391 /*
7392 * Otherwise, shmem/tmpfs must be storing a swap entry
7393 * here as an exceptional entry: so return it without
7394 * attempting to raise page count.
7395 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007396 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007397 break; /* TODO: Is this relevant for this use case? */
7398 }
7399
Filipe Manana91405152014-06-05 13:22:24 +01007400 if (!page_cache_get_speculative(page)) {
7401 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007402 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007403 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007404
7405 /*
7406 * Has the page moved?
7407 * This is part of the lockless pagecache protocol. See
7408 * include/linux/pagemap.h for details.
7409 */
7410 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007411 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007412 page = NULL;
7413 }
7414 }
7415
7416 if (page) {
7417 if (page->index <= end_idx)
7418 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007419 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007420 }
7421
7422 rcu_read_unlock();
7423 return found;
7424}
7425
Josef Bacikeb838e72012-07-31 16:28:48 -04007426static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7427 struct extent_state **cached_state, int writing)
7428{
7429 struct btrfs_ordered_extent *ordered;
7430 int ret = 0;
7431
7432 while (1) {
7433 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007434 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007435 /*
7436 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007437 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007438 * extents in this range.
7439 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007440 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007441 lockend - lockstart + 1);
7442
7443 /*
7444 * We need to make sure there are no buffered pages in this
7445 * range either, we could have raced between the invalidate in
7446 * generic_file_direct_write and locking the extent. The
7447 * invalidate needs to happen so that reads after a write do not
7448 * get stale data.
7449 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007450 if (!ordered &&
7451 (!writing ||
7452 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007453 break;
7454
7455 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7456 cached_state, GFP_NOFS);
7457
7458 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007459 /*
7460 * If we are doing a DIO read and the ordered extent we
7461 * found is for a buffered write, we can not wait for it
7462 * to complete and retry, because if we do so we can
7463 * deadlock with concurrent buffered writes on page
7464 * locks. This happens only if our DIO read covers more
7465 * than one extent map, if at this point has already
7466 * created an ordered extent for a previous extent map
7467 * and locked its range in the inode's io tree, and a
7468 * concurrent write against that previous extent map's
7469 * range and this range started (we unlock the ranges
7470 * in the io tree only when the bios complete and
7471 * buffered writes always lock pages before attempting
7472 * to lock range in the io tree).
7473 */
7474 if (writing ||
7475 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7476 btrfs_start_ordered_extent(inode, ordered, 1);
7477 else
7478 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007479 btrfs_put_ordered_extent(ordered);
7480 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007481 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007482 * We could trigger writeback for this range (and wait
7483 * for it to complete) and then invalidate the pages for
7484 * this range (through invalidate_inode_pages2_range()),
7485 * but that can lead us to a deadlock with a concurrent
7486 * call to readpages() (a buffered read or a defrag call
7487 * triggered a readahead) on a page lock due to an
7488 * ordered dio extent we created before but did not have
7489 * yet a corresponding bio submitted (whence it can not
7490 * complete), which makes readpages() wait for that
7491 * ordered extent to complete while holding a lock on
7492 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007493 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007494 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007495 }
7496
Filipe Mananaade77022016-02-18 14:28:55 +00007497 if (ret)
7498 break;
7499
Josef Bacikeb838e72012-07-31 16:28:48 -04007500 cond_resched();
7501 }
7502
7503 return ret;
7504}
7505
Liu Bo6f9994d2017-01-31 07:50:22 -08007506/* The callers of this must take lock_extent() */
7507static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7508 u64 orig_start, u64 block_start,
7509 u64 block_len, u64 orig_block_len,
7510 u64 ram_bytes, int compress_type,
7511 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007512{
7513 struct extent_map_tree *em_tree;
7514 struct extent_map *em;
7515 struct btrfs_root *root = BTRFS_I(inode)->root;
7516 int ret;
7517
Liu Bo6f9994d2017-01-31 07:50:22 -08007518 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7519 type == BTRFS_ORDERED_COMPRESSED ||
7520 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007521 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007522
Josef Bacik69ffb542012-09-11 15:40:07 -04007523 em_tree = &BTRFS_I(inode)->extent_tree;
7524 em = alloc_extent_map();
7525 if (!em)
7526 return ERR_PTR(-ENOMEM);
7527
7528 em->start = start;
7529 em->orig_start = orig_start;
7530 em->len = len;
7531 em->block_len = block_len;
7532 em->block_start = block_start;
7533 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007534 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007535 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007536 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007537 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007538 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007539 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007540 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007541 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7542 em->compress_type = compress_type;
7543 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007544
7545 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007546 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007547 em->start + em->len - 1, 0);
7548 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007549 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007550 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007551 /*
7552 * The caller has taken lock_extent(), who could race with us
7553 * to add em?
7554 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007555 } while (ret == -EEXIST);
7556
7557 if (ret) {
7558 free_extent_map(em);
7559 return ERR_PTR(ret);
7560 }
7561
Liu Bo6f9994d2017-01-31 07:50:22 -08007562 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007563 return em;
7564}
7565
Filipe Manana9c9464c2015-11-04 09:52:04 +00007566static void adjust_dio_outstanding_extents(struct inode *inode,
7567 struct btrfs_dio_data *dio_data,
7568 const u64 len)
7569{
David Sterba823bb202017-01-04 11:09:51 +01007570 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007571
Filipe Manana9c9464c2015-11-04 09:52:04 +00007572 /*
7573 * If we have an outstanding_extents count still set then we're
7574 * within our reservation, otherwise we need to adjust our inode
7575 * counter appropriately.
7576 */
Liu Boc2931662016-12-22 17:13:54 -08007577 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007578 dio_data->outstanding_extents -= num_extents;
7579 } else {
Liu Boc2931662016-12-22 17:13:54 -08007580 /*
7581 * If dio write length has been split due to no large enough
7582 * contiguous space, we need to compensate our inode counter
7583 * appropriately.
7584 */
7585 u64 num_needed = num_extents - dio_data->outstanding_extents;
7586
Filipe Manana9c9464c2015-11-04 09:52:04 +00007587 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007588 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007589 spin_unlock(&BTRFS_I(inode)->lock);
7590 }
7591}
7592
Josef Bacik4b46fce2010-05-23 11:00:55 -04007593static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7594 struct buffer_head *bh_result, int create)
7595{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007596 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007597 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007598 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307599 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007600 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007601 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007602 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007603 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007604 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007605
Miao Xie172a5042013-02-21 02:48:22 -07007606 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007607 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007608 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007609 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007610
Josef Bacikc3298612012-08-03 16:49:19 -04007611 lockstart = start;
7612 lockend = start + len - 1;
7613
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007614 if (current->journal_info) {
7615 /*
7616 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007617 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007618 * confused.
7619 */
chandan50745b02015-08-28 21:10:13 +05307620 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007621 current->journal_info = NULL;
7622 }
7623
Josef Bacikeb838e72012-07-31 16:28:48 -04007624 /*
7625 * If this errors out it's because we couldn't invalidate pagecache for
7626 * this range and we need to fallback to buffered.
7627 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007628 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7629 create)) {
7630 ret = -ENOTBLK;
7631 goto err;
7632 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007633
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007634 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007635 if (IS_ERR(em)) {
7636 ret = PTR_ERR(em);
7637 goto unlock_err;
7638 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007639
7640 /*
7641 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7642 * io. INLINE is special, and we could probably kludge it in here, but
7643 * it's still buffered so for safety lets just fall back to the generic
7644 * buffered path.
7645 *
7646 * For COMPRESSED we _have_ to read the entire extent in so we can
7647 * decompress it, so there will be buffering required no matter what we
7648 * do, so go ahead and fallback to buffered.
7649 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007650 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007651 * to buffered IO. Don't blame me, this is the price we pay for using
7652 * the generic code.
7653 */
7654 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7655 em->block_start == EXTENT_MAP_INLINE) {
7656 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007657 ret = -ENOTBLK;
7658 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007659 }
7660
7661 /* Just a good old fashioned hole, return */
7662 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7663 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7664 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007665 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007666 }
7667
7668 /*
7669 * We don't allocate a new extent in the following cases
7670 *
7671 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7672 * existing extent.
7673 * 2) The extent is marked as PREALLOC. We're good to go here and can
7674 * just use the extent.
7675 *
7676 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007677 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007678 len = min(len, em->len - (start - em->start));
7679 lockstart = start + len;
7680 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007681 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007682
7683 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7684 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7685 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007686 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007687 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007688
7689 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7690 type = BTRFS_ORDERED_PREALLOC;
7691 else
7692 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007693 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007694 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007695
Josef Bacik00361582013-08-14 14:02:47 -04007696 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007697 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007698 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007699 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007700
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007701 em2 = btrfs_create_dio_extent(inode, start, len,
7702 orig_start, block_start,
7703 len, orig_block_len,
7704 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007705 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007706 if (type == BTRFS_ORDERED_PREALLOC) {
7707 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007708 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007709 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007710 if (em2 && IS_ERR(em2)) {
7711 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007712 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007713 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007714 /*
7715 * For inode marked NODATACOW or extent marked PREALLOC,
7716 * use the existing or preallocated extent, so does not
7717 * need to adjust btrfs_space_info's bytes_may_use.
7718 */
7719 btrfs_free_reserved_data_space_noquota(inode,
7720 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007721 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007722 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007723 }
Josef Bacik00361582013-08-14 14:02:47 -04007724
Chris Mason46bfbb52010-05-26 11:04:10 -04007725 /*
7726 * this will cow the extent, reset the len in case we changed
7727 * it above
7728 */
7729 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007730 free_extent_map(em);
7731 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007732 if (IS_ERR(em)) {
7733 ret = PTR_ERR(em);
7734 goto unlock_err;
7735 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007736 len = min(len, em->len - (start - em->start));
7737unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007738 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7739 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007740 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007741 bh_result->b_bdev = em->bdev;
7742 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007743 if (create) {
7744 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7745 set_buffer_new(bh_result);
7746
7747 /*
7748 * Need to update the i_size under the extent lock so buffered
7749 * readers will get the updated i_size when we unlock.
7750 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007751 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007752 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007753
Filipe Manana9c9464c2015-11-04 09:52:04 +00007754 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307755 WARN_ON(dio_data->reserve < len);
7756 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007757 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307758 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007759 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007760
Josef Bacikeb838e72012-07-31 16:28:48 -04007761 /*
7762 * In the case of write we need to clear and unlock the entire range,
7763 * in the case of read we need to unlock only the end area that we
7764 * aren't using if there is any left over space.
7765 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007766 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007767 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7768 lockend, unlock_bits, 1, 0,
7769 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007770 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007771 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007772 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007773
Josef Bacik4b46fce2010-05-23 11:00:55 -04007774 free_extent_map(em);
7775
7776 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007777
7778unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007779 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7780 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007781err:
chandan50745b02015-08-28 21:10:13 +05307782 if (dio_data)
7783 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007784 /*
7785 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7786 * write less data then expected, so that we don't underflow our inode's
7787 * outstanding extents counter.
7788 */
7789 if (create && dio_data)
7790 adjust_dio_outstanding_extents(inode, dio_data, len);
7791
Josef Bacikeb838e72012-07-31 16:28:48 -04007792 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007793}
7794
Miao Xie8b110e32014-09-12 18:44:03 +08007795static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007796 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007797{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007798 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007799 int ret;
7800
Mike Christie37226b22016-06-05 14:31:52 -05007801 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007802
7803 bio_get(bio);
7804
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007805 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007806 if (ret)
7807 goto err;
7808
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007809 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007810err:
7811 bio_put(bio);
7812 return ret;
7813}
7814
7815static int btrfs_check_dio_repairable(struct inode *inode,
7816 struct bio *failed_bio,
7817 struct io_failure_record *failrec,
7818 int failed_mirror)
7819{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007820 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007821 int num_copies;
7822
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007823 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007824 if (num_copies == 1) {
7825 /*
7826 * we only have a single copy of the data, so don't bother with
7827 * all the retry and error correction code that follows. no
7828 * matter what the error is, it is very likely to persist.
7829 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007830 btrfs_debug(fs_info,
7831 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7832 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007833 return 0;
7834 }
7835
7836 failrec->failed_mirror = failed_mirror;
7837 failrec->this_mirror++;
7838 if (failrec->this_mirror == failed_mirror)
7839 failrec->this_mirror++;
7840
7841 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007842 btrfs_debug(fs_info,
7843 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7844 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007845 return 0;
7846 }
7847
7848 return 1;
7849}
7850
7851static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307852 struct page *page, unsigned int pgoff,
7853 u64 start, u64 end, int failed_mirror,
7854 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007855{
7856 struct io_failure_record *failrec;
7857 struct bio *bio;
7858 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007859 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007860 int ret;
7861
Mike Christie37226b22016-06-05 14:31:52 -05007862 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007863
7864 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7865 if (ret)
7866 return ret;
7867
7868 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7869 failed_mirror);
7870 if (!ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007871 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007872 return -EIO;
7873 }
7874
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307875 if ((failed_bio->bi_vcnt > 1)
7876 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007877 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007878 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007879
7880 isector = start - btrfs_io_bio(failed_bio)->logical;
7881 isector >>= inode->i_sb->s_blocksize_bits;
7882 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307883 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007884 if (!bio) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007885 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007886 return -EIO;
7887 }
Mike Christie37226b22016-06-05 14:31:52 -05007888 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007889
7890 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7891 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7892 read_mode, failrec->this_mirror, failrec->in_validation);
7893
Mike Christie81a75f672016-06-05 14:31:54 -05007894 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007895 if (ret) {
Nikolay Borisov4ac1f4a2017-02-20 13:50:52 +02007896 free_io_failure(BTRFS_I(inode), failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007897 bio_put(bio);
7898 }
7899
7900 return ret;
7901}
7902
7903struct btrfs_retry_complete {
7904 struct completion done;
7905 struct inode *inode;
7906 u64 start;
7907 int uptodate;
7908};
7909
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007910static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007911{
7912 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307913 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007914 struct bio_vec *bvec;
7915 int i;
7916
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007917 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007918 goto end;
7919
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307920 ASSERT(bio->bi_vcnt == 1);
7921 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007922 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307923
Miao Xie8b110e32014-09-12 18:44:03 +08007924 done->uptodate = 1;
7925 bio_for_each_segment_all(bvec, bio, i)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02007926 clean_io_failure(BTRFS_I(done->inode), done->start, bvec->bv_page, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007927end:
7928 complete(&done->done);
7929 bio_put(bio);
7930}
7931
7932static int __btrfs_correct_data_nocsum(struct inode *inode,
7933 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007934{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307935 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007936 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007937 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007938 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307939 unsigned int pgoff;
7940 u32 sectorsize;
7941 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007942 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007943 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007944
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307945 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007946 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307947
Miao Xiec1dc0892014-09-12 18:43:56 +08007948 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007949 done.inode = inode;
7950
7951 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307952 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7953 pgoff = bvec->bv_offset;
7954
7955next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007956 done.uptodate = 0;
7957 done.start = start;
7958 init_completion(&done.done);
7959
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307960 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7961 pgoff, start, start + sectorsize - 1,
7962 io_bio->mirror_num,
7963 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007964 if (ret)
7965 return ret;
7966
7967 wait_for_completion(&done.done);
7968
7969 if (!done.uptodate) {
7970 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307971 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007972 }
7973
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307974 start += sectorsize;
7975
7976 if (nr_sectors--) {
7977 pgoff += sectorsize;
7978 goto next_block_or_try_again;
7979 }
Miao Xie8b110e32014-09-12 18:44:03 +08007980 }
7981
7982 return 0;
7983}
7984
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007985static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007986{
7987 struct btrfs_retry_complete *done = bio->bi_private;
7988 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307989 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007990 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307991 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08007992 int uptodate;
7993 int ret;
7994 int i;
7995
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007996 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007997 goto end;
7998
7999 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308000
8001 start = done->start;
8002
8003 ASSERT(bio->bi_vcnt == 1);
8004 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008005 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308006
Miao Xie8b110e32014-09-12 18:44:03 +08008007 bio_for_each_segment_all(bvec, bio, i) {
8008 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308009 bvec->bv_page, bvec->bv_offset,
8010 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008011 if (!ret)
Nikolay Borisovb30cb442017-02-20 13:50:56 +02008012 clean_io_failure(BTRFS_I(done->inode), done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308013 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008014 else
8015 uptodate = 0;
8016 }
8017
8018 done->uptodate = uptodate;
8019end:
8020 complete(&done->done);
8021 bio_put(bio);
8022}
8023
8024static int __btrfs_subio_endio_read(struct inode *inode,
8025 struct btrfs_io_bio *io_bio, int err)
8026{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308027 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008028 struct bio_vec *bvec;
8029 struct btrfs_retry_complete done;
8030 u64 start;
8031 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308032 u32 sectorsize;
8033 int nr_sectors;
8034 unsigned int pgoff;
8035 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008036 int i;
8037 int ret;
8038
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308039 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008040 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308041
Miao Xie8b110e32014-09-12 18:44:03 +08008042 err = 0;
8043 start = io_bio->logical;
8044 done.inode = inode;
8045
Miao Xiec1dc0892014-09-12 18:43:56 +08008046 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308047 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8048
8049 pgoff = bvec->bv_offset;
8050next_block:
8051 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8052 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8053 bvec->bv_page, pgoff, start,
8054 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008055 if (likely(!ret))
8056 goto next;
8057try_again:
8058 done.uptodate = 0;
8059 done.start = start;
8060 init_completion(&done.done);
8061
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8063 pgoff, start, start + sectorsize - 1,
8064 io_bio->mirror_num,
8065 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008066 if (ret) {
8067 err = ret;
8068 goto next;
8069 }
8070
8071 wait_for_completion(&done.done);
8072
8073 if (!done.uptodate) {
8074 /* We might have another mirror, so try again */
8075 goto try_again;
8076 }
8077next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308078 offset += sectorsize;
8079 start += sectorsize;
8080
8081 ASSERT(nr_sectors);
8082
8083 if (--nr_sectors) {
8084 pgoff += sectorsize;
8085 goto next_block;
8086 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008087 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008088
8089 return err;
8090}
8091
Miao Xie8b110e32014-09-12 18:44:03 +08008092static int btrfs_subio_endio_read(struct inode *inode,
8093 struct btrfs_io_bio *io_bio, int err)
8094{
8095 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8096
8097 if (skip_csum) {
8098 if (unlikely(err))
8099 return __btrfs_correct_data_nocsum(inode, io_bio);
8100 else
8101 return 0;
8102 } else {
8103 return __btrfs_subio_endio_read(inode, io_bio, err);
8104 }
8105}
8106
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008107static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008108{
8109 struct btrfs_dio_private *dip = bio->bi_private;
8110 struct inode *inode = dip->inode;
8111 struct bio *dio_bio;
8112 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008113 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008114
Miao Xie8b110e32014-09-12 18:44:03 +08008115 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8116 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008117
Josef Bacik4b46fce2010-05-23 11:00:55 -04008118 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008119 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008120 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008121
Josef Bacik4b46fce2010-05-23 11:00:55 -04008122 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008123
Filipe Manana1636d1d2016-02-15 16:20:26 +00008124 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008125 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008126
8127 if (io_bio->end_io)
8128 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008129 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008130}
8131
Filipe Manana14543772015-11-24 16:23:54 +00008132static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8133 const u64 offset,
8134 const u64 bytes,
8135 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008136{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008137 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008138 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008139 u64 ordered_offset = offset;
8140 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008141 int ret;
8142
Chris Mason163cf092010-11-28 19:56:33 -05008143again:
8144 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8145 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008146 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008147 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008148 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008149 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008150
Liu Bo9e0af232014-08-15 23:36:53 +08008151 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8152 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008153 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008154out_test:
8155 /*
8156 * our bio might span multiple ordered extents. If we haven't
8157 * completed the accounting for the whole dio, go back and try again
8158 */
Filipe Manana14543772015-11-24 16:23:54 +00008159 if (ordered_offset < offset + bytes) {
8160 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008161 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008162 goto again;
8163 }
Filipe Manana14543772015-11-24 16:23:54 +00008164}
8165
8166static void btrfs_endio_direct_write(struct bio *bio)
8167{
8168 struct btrfs_dio_private *dip = bio->bi_private;
8169 struct bio *dio_bio = dip->dio_bio;
8170
8171 btrfs_endio_direct_write_update_ordered(dip->inode,
8172 dip->logical_offset,
8173 dip->bytes,
8174 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008175
Josef Bacik4b46fce2010-05-23 11:00:55 -04008176 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008177
Filipe Manana1636d1d2016-02-15 16:20:26 +00008178 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008179 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008180 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008181}
8182
Mike Christie81a75f672016-06-05 14:31:54 -05008183static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008184 struct bio *bio, int mirror_num,
8185 unsigned long bio_flags, u64 offset)
8186{
8187 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008188 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008189 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008190 return 0;
8191}
8192
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008193static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008194{
8195 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008196 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008197
Miao Xie8b110e32014-09-12 18:44:03 +08008198 if (err)
8199 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008200 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008201 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8202 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008203 (unsigned long long)bio->bi_iter.bi_sector,
8204 bio->bi_iter.bi_size, err);
8205
8206 if (dip->subio_endio)
8207 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008208
8209 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008210 dip->errors = 1;
8211
8212 /*
8213 * before atomic variable goto zero, we must make sure
8214 * dip->errors is perceived to be set.
8215 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008216 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008217 }
8218
8219 /* if there are more bios still pending for this dio, just exit */
8220 if (!atomic_dec_and_test(&dip->pending_bios))
8221 goto out;
8222
Chris Mason9be33952013-05-17 18:30:14 -04008223 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008224 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008225 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008226 dip->dio_bio->bi_error = 0;
8227 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008228 }
8229out:
8230 bio_put(bio);
8231}
8232
8233static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8234 u64 first_sector, gfp_t gfp_flags)
8235{
Chris Masonda2f0f72015-07-02 13:57:22 -07008236 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008237 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008238 if (bio)
8239 bio_associate_current(bio);
8240 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008241}
8242
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008243static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008244 struct btrfs_dio_private *dip,
8245 struct bio *bio,
8246 u64 file_offset)
8247{
8248 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8249 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8250 int ret;
8251
8252 /*
8253 * We load all the csum data we need when we submit
8254 * the first bio to reduce the csum tree search and
8255 * contention.
8256 */
8257 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008258 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008259 file_offset);
8260 if (ret)
8261 return ret;
8262 }
8263
8264 if (bio == dip->orig_bio)
8265 return 0;
8266
8267 file_offset -= dip->logical_offset;
8268 file_offset >>= inode->i_sb->s_blocksize_bits;
8269 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8270
8271 return 0;
8272}
8273
Miao Xiee65e1532010-11-22 03:04:43 +00008274static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008275 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008276 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008277{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008278 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008279 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008280 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008281 int ret;
8282
Josef Bacikb812ce22012-11-16 13:56:32 -05008283 if (async_submit)
8284 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8285
Miao Xiee65e1532010-11-22 03:04:43 +00008286 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008287
8288 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008289 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008290 if (ret)
8291 goto err;
8292 }
Miao Xiee65e1532010-11-22 03:04:43 +00008293
Josef Bacik1ae39932011-04-06 14:41:34 -04008294 if (skip_sum)
8295 goto map;
8296
8297 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008298 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8299 file_offset,
8300 __btrfs_submit_bio_start_direct_io,
8301 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008302 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008303 } else if (write) {
8304 /*
8305 * If we aren't doing async submit, calculate the csum of the
8306 * bio now.
8307 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008308 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008309 if (ret)
8310 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008311 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008312 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008313 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008314 if (ret)
8315 goto err;
8316 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008317map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008318 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008319err:
8320 bio_put(bio);
8321 return ret;
8322}
8323
Mike Christie81a75f672016-06-05 14:31:54 -05008324static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008325 int skip_sum)
8326{
8327 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008328 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008329 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008330 struct bio *bio;
8331 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008332 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008333 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008334 u64 file_offset = dip->logical_offset;
8335 u64 submit_len = 0;
8336 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008337 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008338 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308339 int nr_sectors;
8340 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008341 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008342
Kent Overstreet4f024f32013-10-11 15:44:27 -07008343 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008344 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8345 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008346 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008347 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008348
Kent Overstreet4f024f32013-10-11 15:44:27 -07008349 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008350 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008351 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008352 goto submit;
8353 }
8354
David Woodhouse53b381b2013-01-29 18:40:14 -05008355 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008356 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008357 async_submit = 0;
8358 else
8359 async_submit = 1;
8360
Josef Bacik02f57c72011-04-06 14:25:44 -04008361 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8362 if (!bio)
8363 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008364
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008365 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008366 bio->bi_private = dip;
8367 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008368 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008369 atomic_inc(&dip->pending_bios);
8370
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008371 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008372 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308373 i = 0;
8374next_block:
8375 if (unlikely(map_length < submit_len + blocksize ||
8376 bio_add_page(bio, bvec->bv_page, blocksize,
8377 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008378 /*
8379 * inc the count before we submit the bio so
8380 * we know the end IO handler won't happen before
8381 * we inc the count. Otherwise, the dip might get freed
8382 * before we're done setting it up
8383 */
8384 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008385 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008386 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008387 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008388 if (ret) {
8389 bio_put(bio);
8390 atomic_dec(&dip->pending_bios);
8391 goto out_err;
8392 }
8393
Miao Xiee65e1532010-11-22 03:04:43 +00008394 start_sector += submit_len >> 9;
8395 file_offset += submit_len;
8396
8397 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008398
8399 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8400 start_sector, GFP_NOFS);
8401 if (!bio)
8402 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008403 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008404 bio->bi_private = dip;
8405 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008406 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008407
Kent Overstreet4f024f32013-10-11 15:44:27 -07008408 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008409 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008410 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008411 &map_length, NULL, 0);
8412 if (ret) {
8413 bio_put(bio);
8414 goto out_err;
8415 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308416
8417 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008418 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308419 submit_len += blocksize;
8420 if (--nr_sectors) {
8421 i++;
8422 goto next_block;
8423 }
Miao Xiee65e1532010-11-22 03:04:43 +00008424 }
8425 }
8426
Josef Bacik02f57c72011-04-06 14:25:44 -04008427submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008428 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008429 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008430 if (!ret)
8431 return 0;
8432
8433 bio_put(bio);
8434out_err:
8435 dip->errors = 1;
8436 /*
8437 * before atomic variable goto zero, we must
8438 * make sure dip->errors is perceived to be set.
8439 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008440 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008441 if (atomic_dec_and_test(&dip->pending_bios))
8442 bio_io_error(dip->orig_bio);
8443
8444 /* bio_end_io() will handle error, so we needn't return it */
8445 return 0;
8446}
8447
Mike Christie8a4c1e42016-06-05 14:31:50 -05008448static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8449 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008450{
Filipe Manana61de7182015-07-01 12:13:10 +01008451 struct btrfs_dio_private *dip = NULL;
8452 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008453 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008454 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008455 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008456 int ret = 0;
8457
8458 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8459
Chris Mason9be33952013-05-17 18:30:14 -04008460 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008461 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008462 ret = -ENOMEM;
8463 goto free_ordered;
8464 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008465
Miao Xiec1dc0892014-09-12 18:43:56 +08008466 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008467 if (!dip) {
8468 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008469 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008470 }
8471
8472 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008473 dip->inode = inode;
8474 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008475 dip->bytes = dio_bio->bi_iter.bi_size;
8476 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008477 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008478 dip->orig_bio = io_bio;
8479 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008480 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008481 btrfs_bio = btrfs_io_bio(io_bio);
8482 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008483
Miao Xiec1dc0892014-09-12 18:43:56 +08008484 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008485 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008486 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008487 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008488 dip->subio_endio = btrfs_subio_endio_read;
8489 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008490
Filipe Mananaf28a4922015-12-08 19:23:20 +00008491 /*
8492 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8493 * even if we fail to submit a bio, because in such case we do the
8494 * corresponding error handling below and it must not be done a second
8495 * time by btrfs_direct_IO().
8496 */
8497 if (write) {
8498 struct btrfs_dio_data *dio_data = current->journal_info;
8499
8500 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8501 dip->bytes;
8502 dio_data->unsubmitted_oe_range_start =
8503 dio_data->unsubmitted_oe_range_end;
8504 }
8505
Mike Christie81a75f672016-06-05 14:31:54 -05008506 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008507 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008508 return;
Chris Mason9be33952013-05-17 18:30:14 -04008509
Miao Xie23ea8e52014-09-12 18:43:54 +08008510 if (btrfs_bio->end_io)
8511 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008512
Josef Bacik4b46fce2010-05-23 11:00:55 -04008513free_ordered:
8514 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008515 * If we arrived here it means either we failed to submit the dip
8516 * or we either failed to clone the dio_bio or failed to allocate the
8517 * dip. If we cloned the dio_bio and allocated the dip, we can just
8518 * call bio_endio against our io_bio so that we get proper resource
8519 * cleanup if we fail to submit the dip, otherwise, we must do the
8520 * same as btrfs_endio_direct_[write|read] because we can't call these
8521 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008522 */
Filipe Manana61de7182015-07-01 12:13:10 +01008523 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008524 io_bio->bi_error = -EIO;
8525 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008526 /*
8527 * The end io callbacks free our dip, do the final put on io_bio
8528 * and all the cleanup and final put for dio_bio (through
8529 * dio_end_io()).
8530 */
8531 dip = NULL;
8532 io_bio = NULL;
8533 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008534 if (write)
8535 btrfs_endio_direct_write_update_ordered(inode,
8536 file_offset,
8537 dio_bio->bi_iter.bi_size,
8538 0);
8539 else
Filipe Manana61de7182015-07-01 12:13:10 +01008540 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8541 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008542
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008543 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008544 /*
8545 * Releases and cleans up our dio_bio, no need to bio_put()
8546 * nor bio_endio()/bio_io_error() against dio_bio.
8547 */
8548 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008549 }
Filipe Manana61de7182015-07-01 12:13:10 +01008550 if (io_bio)
8551 bio_put(io_bio);
8552 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008553}
8554
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008555static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8556 struct kiocb *iocb,
8557 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008558{
8559 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008560 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008561 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008562 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008563
8564 if (offset & blocksize_mask)
8565 goto out;
8566
Al Viro28060d52014-03-22 05:15:17 -04008567 if (iov_iter_alignment(iter) & blocksize_mask)
8568 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008569
Al Viro28060d52014-03-22 05:15:17 -04008570 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008571 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008572 return 0;
8573 /*
8574 * Check to make sure we don't have duplicate iov_base's in this
8575 * iovec, if so return EINVAL, otherwise we'll get csum errors
8576 * when reading back.
8577 */
8578 for (seg = 0; seg < iter->nr_segs; seg++) {
8579 for (i = seg + 1; i < iter->nr_segs; i++) {
8580 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008581 goto out;
8582 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008583 }
8584 retval = 0;
8585out:
8586 return retval;
8587}
Josef Bacikeb838e72012-07-31 16:28:48 -04008588
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008589static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008590{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008591 struct file *file = iocb->ki_filp;
8592 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008593 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308594 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008595 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008596 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008597 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008598 bool wakeup = true;
8599 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008600 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008601
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008602 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008603 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008604
Jens Axboefe0f07d2015-04-15 17:05:48 -06008605 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008606 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008607
Josef Bacik0e267c42013-07-02 10:38:02 -04008608 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008609 * The generic stuff only does filemap_write_and_wait_range, which
8610 * isn't enough if we've written compressed pages to this area, so
8611 * we need to flush the dirty pages again to make absolutely sure
8612 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008613 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008614 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008615 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8616 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008617 filemap_fdatawrite_range(inode->i_mapping, offset,
8618 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008619
Omar Sandoval6f673762015-03-16 04:33:52 -07008620 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008621 /*
8622 * If the write DIO is beyond the EOF, we need update
8623 * the isize, but it is protected by i_mutex. So we can
8624 * not unlock the i_mutex at this case.
8625 */
8626 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008627 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008628 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008629 relock = true;
8630 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008631 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008632 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008633 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008634 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008635
8636 /*
8637 * We need to know how many extents we reserved so that we can
8638 * do the accounting properly if we go over the number we
8639 * originally calculated. Abuse current->journal_info for this.
8640 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008641 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008642 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008643 dio_data.unsubmitted_oe_range_start = (u64)offset;
8644 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308645 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308646 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008647 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8648 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008649 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008650 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8651 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008652 }
8653
Omar Sandoval17f8c842015-03-16 04:33:50 -07008654 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008655 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008656 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008657 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008658 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308659 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008660 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008661 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308662 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008663 btrfs_delalloc_release_space(inode, offset,
8664 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008665 /*
8666 * On error we might have left some ordered extents
8667 * without submitting corresponding bios for them, so
8668 * cleanup them up to avoid other tasks getting them
8669 * and waiting for them to complete forever.
8670 */
8671 if (dio_data.unsubmitted_oe_range_start <
8672 dio_data.unsubmitted_oe_range_end)
8673 btrfs_endio_direct_write_update_ordered(inode,
8674 dio_data.unsubmitted_oe_range_start,
8675 dio_data.unsubmitted_oe_range_end -
8676 dio_data.unsubmitted_oe_range_start,
8677 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008678 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008679 btrfs_delalloc_release_space(inode, offset,
8680 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008681 }
Miao Xie38851cc2013-02-08 07:04:11 +00008682out:
Miao Xie2e60a512013-02-08 07:01:08 +00008683 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008684 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008685 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008686 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008687
8688 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008689}
8690
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008691#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8692
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008693static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8694 __u64 start, __u64 len)
8695{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008696 int ret;
8697
8698 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8699 if (ret)
8700 return ret;
8701
Chris Masonec29ed52011-02-23 16:23:20 -05008702 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008703}
8704
Chris Mason9ebefb182007-06-15 13:50:00 -04008705int btrfs_readpage(struct file *file, struct page *page)
8706{
Chris Masond1310b22008-01-24 16:13:08 -05008707 struct extent_io_tree *tree;
8708 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008709 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008710}
Chris Mason1832a6d2007-12-21 16:27:21 -05008711
Chris Mason39279cc2007-06-12 06:35:45 -04008712static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8713{
Chris Masond1310b22008-01-24 16:13:08 -05008714 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008715 struct inode *inode = page->mapping->host;
8716 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008717
8718 if (current->flags & PF_MEMALLOC) {
8719 redirty_page_for_writepage(wbc, page);
8720 unlock_page(page);
8721 return 0;
8722 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008723
8724 /*
8725 * If we are under memory pressure we will call this directly from the
8726 * VM, we need to make sure we have the inode referenced for the ordered
8727 * extent. If not just return like we didn't do anything.
8728 */
8729 if (!igrab(inode)) {
8730 redirty_page_for_writepage(wbc, page);
8731 return AOP_WRITEPAGE_ACTIVATE;
8732 }
Chris Masond1310b22008-01-24 16:13:08 -05008733 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008734 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8735 btrfs_add_delayed_iput(inode);
8736 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008737}
Chris Mason39279cc2007-06-12 06:35:45 -04008738
Eric Sandeen48a3b632013-04-25 20:41:01 +00008739static int btrfs_writepages(struct address_space *mapping,
8740 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008741{
Chris Masond1310b22008-01-24 16:13:08 -05008742 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008743
Chris Masond1310b22008-01-24 16:13:08 -05008744 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008745 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8746}
8747
Chris Mason3ab2fb52007-11-08 10:59:22 -05008748static int
8749btrfs_readpages(struct file *file, struct address_space *mapping,
8750 struct list_head *pages, unsigned nr_pages)
8751{
Chris Masond1310b22008-01-24 16:13:08 -05008752 struct extent_io_tree *tree;
8753 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008754 return extent_readpages(tree, mapping, pages, nr_pages,
8755 btrfs_get_extent);
8756}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008757static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008758{
Chris Masond1310b22008-01-24 16:13:08 -05008759 struct extent_io_tree *tree;
8760 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008761 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008762
Chris Masond1310b22008-01-24 16:13:08 -05008763 tree = &BTRFS_I(page->mapping->host)->io_tree;
8764 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008765 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008766 if (ret == 1) {
8767 ClearPagePrivate(page);
8768 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008769 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008770 }
8771 return ret;
8772}
Chris Mason39279cc2007-06-12 06:35:45 -04008773
Chris Masone6dcd2d2008-07-17 12:53:50 -04008774static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8775{
Chris Mason98509cf2008-09-11 15:51:43 -04008776 if (PageWriteback(page) || PageDirty(page))
8777 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008778 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008779}
8780
Lukas Czernerd47992f2013-05-21 23:17:23 -04008781static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8782 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008783{
Josef Bacik5fd02042012-05-02 14:00:54 -04008784 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008785 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008786 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008787 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008788 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008789 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308790 u64 start;
8791 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008792 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008793
Chris Mason8b62b722009-09-02 16:53:46 -04008794 /*
8795 * we have the page locked, so new writeback can't start,
8796 * and the dirty bit won't be cleared while we are here.
8797 *
8798 * Wait for IO on this page so that we can safely clear
8799 * the PagePrivate2 bit and do ordered accounting
8800 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008801 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008802
Josef Bacik5fd02042012-05-02 14:00:54 -04008803 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008804 if (offset) {
8805 btrfs_releasepage(page, GFP_NOFS);
8806 return;
8807 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008808
8809 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008810 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308811again:
8812 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008813 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308814 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008815 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308816 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008817 /*
8818 * IO on this page will never be started, so we need
8819 * to account for any ordered extents now
8820 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008821 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308822 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008823 EXTENT_DIRTY | EXTENT_DELALLOC |
8824 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8825 EXTENT_DEFRAG, 1, 0, &cached_state,
8826 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008827 /*
8828 * whoever cleared the private bit is responsible
8829 * for the finish_ordered_io
8830 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008831 if (TestClearPagePrivate2(page)) {
8832 struct btrfs_ordered_inode_tree *tree;
8833 u64 new_len;
8834
8835 tree = &BTRFS_I(inode)->ordered_tree;
8836
8837 spin_lock_irq(&tree->lock);
8838 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308839 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008840 if (new_len < ordered->truncated_len)
8841 ordered->truncated_len = new_len;
8842 spin_unlock_irq(&tree->lock);
8843
8844 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308845 start,
8846 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008847 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008848 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008849 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008850 if (!inode_evicting) {
8851 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308852 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008853 &cached_state);
8854 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308855
8856 start = end + 1;
8857 if (start < page_end)
8858 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008859 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008860
Qu Wenruob9d0b382015-09-29 10:35:16 +08008861 /*
8862 * Qgroup reserved space handler
8863 * Page here will be either
8864 * 1) Already written to disk
8865 * In this case, its reserved space is released from data rsv map
8866 * and will be freed by delayed_ref handler finally.
8867 * So even we call qgroup_free_data(), it won't decrease reserved
8868 * space.
8869 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008870 * This means the reserved space should be freed here. However,
8871 * if a truncate invalidates the page (by clearing PageDirty)
8872 * and the page is accounted for while allocating extent
8873 * in btrfs_check_data_free_space() we let delayed_ref to
8874 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008875 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008876 if (PageDirty(page))
8877 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008878 if (!inode_evicting) {
8879 clear_extent_bit(tree, page_start, page_end,
8880 EXTENT_LOCKED | EXTENT_DIRTY |
8881 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8882 EXTENT_DEFRAG, 1, 1,
8883 &cached_state, GFP_NOFS);
8884
8885 __btrfs_releasepage(page, GFP_NOFS);
8886 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008887
Chris Mason4a096752008-07-21 10:29:44 -04008888 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008889 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008890 ClearPagePrivate(page);
8891 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008892 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008893 }
Chris Mason39279cc2007-06-12 06:35:45 -04008894}
8895
Chris Mason9ebefb182007-06-15 13:50:00 -04008896/*
8897 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8898 * called from a page fault handler when a page is first dirtied. Hence we must
8899 * be careful to check for EOF conditions here. We set the page up correctly
8900 * for a written page which means we get ENOSPC checking when writing into
8901 * holes and correct delalloc and unwritten extent mapping on filesystems that
8902 * support these features.
8903 *
8904 * We are not allowed to take the i_mutex here so we have to play games to
8905 * protect against truncate races as the page could now be beyond EOF. Because
8906 * vmtruncate() writes the inode size before removing pages, once we have the
8907 * page lock we can determine safely if the page is beyond EOF. If it is not
8908 * beyond EOF, then the page is guaranteed safe against truncation until we
8909 * unlock the page.
8910 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008911int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008912{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008913 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008914 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008915 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008916 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8917 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008918 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008919 char *kaddr;
8920 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008921 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008922 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008923 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308924 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008925 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008926 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308927 u64 end;
8928
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008929 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008930
Jan Karab2b5ef52012-06-12 16:20:45 +02008931 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008932 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008933 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308934 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008935
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308936 /*
8937 * Reserving delalloc space after obtaining the page lock can lead to
8938 * deadlock. For example, if a dirty page is locked by this function
8939 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8940 * dirty page write out, then the btrfs_writepage() function could
8941 * end up waiting indefinitely to get a lock on the page currently
8942 * being processed by btrfs_page_mkwrite() function.
8943 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008944 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308945 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008946 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008947 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008948 reserved = 1;
8949 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008950 if (ret) {
8951 if (ret == -ENOMEM)
8952 ret = VM_FAULT_OOM;
8953 else /* -ENOSPC, -EIO, etc */
8954 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008955 if (reserved)
8956 goto out;
8957 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008958 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008959
Nick Piggin56a76f82009-03-31 15:23:23 -07008960 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008961again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008962 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008963 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008964
Chris Mason9ebefb182007-06-15 13:50:00 -04008965 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008966 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008967 /* page got truncated out from underneath us */
8968 goto out_unlock;
8969 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008970 wait_on_page_writeback(page);
8971
David Sterbaff13db42015-12-03 14:30:40 +01008972 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008973 set_page_extent_mapped(page);
8974
Chris Masoneb84ae02008-07-17 13:53:27 -04008975 /*
8976 * we can't set the delalloc bits if there are pending ordered
8977 * extents. Drop our locks and wait for them to finish
8978 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008979 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8980 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008981 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008982 unlock_extent_cached(io_tree, page_start, page_end,
8983 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008984 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008985 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008986 btrfs_put_ordered_extent(ordered);
8987 goto again;
8988 }
8989
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008990 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008991 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008992 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008993 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308994 end = page_start + reserved_space - 1;
8995 spin_lock(&BTRFS_I(inode)->lock);
8996 BTRFS_I(inode)->outstanding_extents++;
8997 spin_unlock(&BTRFS_I(inode)->lock);
8998 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008999 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309000 }
9001 }
9002
Josef Bacikfbf19082009-10-01 17:10:23 -04009003 /*
Liu Bo54160342016-12-13 12:15:19 -08009004 * page_mkwrite gets called when the page is firstly dirtied after it's
9005 * faulted in, but write(2) could also dirty a page and set delalloc
9006 * bits, thus in this case for space account reason, we still need to
9007 * clear any delalloc bits within this page range since we have to
9008 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009009 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309010 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009011 EXTENT_DIRTY | EXTENT_DELALLOC |
9012 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009013 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009014
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309015 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009016 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009017 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009018 unlock_extent_cached(io_tree, page_start, page_end,
9019 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009020 ret = VM_FAULT_SIGBUS;
9021 goto out_unlock;
9022 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009023 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009024
9025 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009026 if (page_start + PAGE_SIZE > size)
9027 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009028 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009029 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009030
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009031 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009032 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009033 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009034 flush_dcache_page(page);
9035 kunmap(page);
9036 }
Chris Mason247e7432008-07-17 12:53:51 -04009037 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009038 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009039 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009040
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009041 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009042 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009043 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009044
Josef Bacik2ac55d42010-02-03 19:33:23 +00009045 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009046
9047out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009048 if (!ret) {
9049 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009050 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009051 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009052 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009053out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309054 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009055out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009056 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009057 return ret;
9058}
9059
Josef Bacika41ad392011-01-31 15:30:16 -05009060static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009061{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009062 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009063 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009064 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009065 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009066 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009067 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009068 u64 mask = fs_info->sectorsize - 1;
9069 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009070
Josef Bacik0ef8b722013-10-25 16:13:35 -04009071 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9072 (u64)-1);
9073 if (ret)
9074 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009075
Josef Bacikfcb80c22011-05-03 10:40:22 -04009076 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009077 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009078 * 3 things going on here
9079 *
9080 * 1) We need to reserve space for our orphan item and the space to
9081 * delete our orphan item. Lord knows we don't want to have a dangling
9082 * orphan item because we didn't reserve space to remove it.
9083 *
9084 * 2) We need to reserve space to update our inode.
9085 *
9086 * 3) We need to have something to cache all the space that is going to
9087 * be free'd up by the truncate operation, but also have some slack
9088 * space reserved in case it uses space during the truncate (thank you
9089 * very much snapshotting).
9090 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009091 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009092 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009093 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009094 * doesn't end up using space reserved for updating the inode or
9095 * removing the orphan item. We also need to be able to stop the
9096 * transaction and start a new one, which means we need to be able to
9097 * update the inode several times, and we have no idea of knowing how
9098 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009099 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009100 * Then there is the orphan item, which does indeed need to be held on
9101 * to for the whole operation, and we need nobody to touch this reserved
9102 * space except the orphan code.
9103 *
9104 * So that leaves us with
9105 *
9106 * 1) root->orphan_block_rsv - for the orphan deletion.
9107 * 2) rsv - for the truncate reservation, which we will steal from the
9108 * transaction reservation.
9109 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9110 * updating the inode.
9111 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009112 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009113 if (!rsv)
9114 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009115 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009116 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009117
Josef Bacik907cbce2011-08-08 13:46:15 -04009118 /*
Josef Bacik07127182011-08-19 10:29:59 -04009119 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009120 * 1 for updating the inode.
9121 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009122 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009123 if (IS_ERR(trans)) {
9124 err = PTR_ERR(trans);
9125 goto out;
9126 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009127
Josef Bacik907cbce2011-08-08 13:46:15 -04009128 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009129 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009130 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009131 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009132
Chris Mason5a3f23d2009-03-31 13:27:11 -04009133 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009134 * So if we truncate and then write and fsync we normally would just
9135 * write the extents that changed, which is a problem if we need to
9136 * first truncate that entire inode. So set this flag so we write out
9137 * all of the extents in the inode to the sync log so we're completely
9138 * safe.
9139 */
9140 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009141 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009142
Yan, Zheng80825102009-11-12 09:35:36 +00009143 while (1) {
9144 ret = btrfs_truncate_inode_items(trans, root, inode,
9145 inode->i_size,
9146 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009147 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009148 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009149 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009150 }
Chris Mason39279cc2007-06-12 06:35:45 -04009151
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009152 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009153 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009154 if (ret) {
9155 err = ret;
9156 break;
9157 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009158
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009159 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009160 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009161
9162 trans = btrfs_start_transaction(root, 2);
9163 if (IS_ERR(trans)) {
9164 ret = err = PTR_ERR(trans);
9165 trans = NULL;
9166 break;
9167 }
9168
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009169 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009170 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009171 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009172 BUG_ON(ret); /* shouldn't happen */
9173 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009174 }
9175
9176 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009177 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009178 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009179 if (ret)
9180 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009181 }
9182
Chris Mason917c16b2011-11-08 14:49:59 -05009183 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009184 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009185 ret = btrfs_update_inode(trans, root, inode);
9186 if (ret && !err)
9187 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009188
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009189 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009190 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009191 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009192out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009193 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009194
Josef Bacik3893e332011-01-31 16:03:11 -05009195 if (ret && !err)
9196 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009197
Josef Bacik3893e332011-01-31 16:03:11 -05009198 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009199}
9200
Sven Wegener3b963622008-06-09 21:57:42 -04009201/*
Chris Masond352ac62008-09-29 15:18:18 -04009202 * create a new subvolume directory/inode (helper for the ioctl).
9203 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009204int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009205 struct btrfs_root *new_root,
9206 struct btrfs_root *parent_root,
9207 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009208{
Chris Mason39279cc2007-06-12 06:35:45 -04009209 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009210 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009211 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009212
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009213 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9214 new_dirid, new_dirid,
9215 S_IFDIR | (~current_umask() & S_IRWXUGO),
9216 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009217 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009218 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009219 inode->i_op = &btrfs_dir_inode_operations;
9220 inode->i_fop = &btrfs_dir_file_operations;
9221
Miklos Szeredibfe86842011-10-28 14:13:29 +02009222 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009223 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009224 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009225
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009226 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9227 if (err)
9228 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009229 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009230 new_root->root_key.objectid, err);
9231
Yan, Zheng76dda932009-09-21 16:00:26 -04009232 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009233
Yan, Zheng76dda932009-09-21 16:00:26 -04009234 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009235 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009236}
9237
Chris Mason39279cc2007-06-12 06:35:45 -04009238struct inode *btrfs_alloc_inode(struct super_block *sb)
9239{
9240 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009241 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009242
9243 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9244 if (!ei)
9245 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009246
9247 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009248 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009249 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009250 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009251 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009252 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009253 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009254 ei->disk_i_size = 0;
9255 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009256 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009257 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009258 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009259 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009260 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009261 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009262
Josef Bacik9e0baf62011-07-15 15:16:44 +00009263 spin_lock_init(&ei->lock);
9264 ei->outstanding_extents = 0;
9265 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009266
Josef Bacik72ac3c02012-05-23 14:13:11 -04009267 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009268 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009269
Miao Xie16cdcec2011-04-22 18:12:22 +08009270 ei->delayed_node = NULL;
9271
chandan r9cc97d62012-07-04 12:48:07 +05309272 ei->i_otime.tv_sec = 0;
9273 ei->i_otime.tv_nsec = 0;
9274
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009275 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009276 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009277 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9278 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009279 ei->io_tree.track_uptodate = 1;
9280 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009281 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009282 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009283 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009284 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009285 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009286 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009287 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009288 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009289
9290 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009291}
9292
Josef Bacikaaedb552013-10-11 14:44:09 -04009293#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9294void btrfs_test_destroy_inode(struct inode *inode)
9295{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009296 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009297 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9298}
9299#endif
9300
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009301static void btrfs_i_callback(struct rcu_head *head)
9302{
9303 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009304 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9305}
9306
Chris Mason39279cc2007-06-12 06:35:45 -04009307void btrfs_destroy_inode(struct inode *inode)
9308{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009309 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009310 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009311 struct btrfs_root *root = BTRFS_I(inode)->root;
9312
Al Virob3d9b7a2012-06-09 13:51:19 -04009313 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009314 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009315 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9316 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009317 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9318 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009319 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009320
Chris Mason5a3f23d2009-03-31 13:27:11 -04009321 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009322 * This can happen where we create an inode, but somebody else also
9323 * created the same inode and we need to destroy the one we already
9324 * created.
9325 */
9326 if (!root)
9327 goto free;
9328
Josef Bacik8a35d952012-05-23 14:26:42 -04009329 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9330 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009331 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009332 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009333 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009334 }
Josef Bacik7b128762008-07-24 12:17:14 -04009335
Chris Masond3977122009-01-05 21:25:51 -05009336 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009337 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9338 if (!ordered)
9339 break;
9340 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009341 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009342 "found ordered extent %llu %llu on inode cleanup",
9343 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009344 btrfs_remove_ordered_extent(inode, ordered);
9345 btrfs_put_ordered_extent(ordered);
9346 btrfs_put_ordered_extent(ordered);
9347 }
9348 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009349 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009350 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009351 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009352free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009353 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009354}
9355
Al Viro45321ac2010-06-07 13:43:19 -04009356int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009357{
9358 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009359
Naohiro Aota6379ef92013-06-06 09:56:34 +00009360 if (root == NULL)
9361 return 1;
9362
Liu Bofa6ac872013-02-20 14:10:23 +00009363 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009364 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009365 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009366 else
Al Viro45321ac2010-06-07 13:43:19 -04009367 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009368}
9369
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009370static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009371{
9372 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9373
9374 inode_init_once(&ei->vfs_inode);
9375}
9376
9377void btrfs_destroy_cachep(void)
9378{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009379 /*
9380 * Make sure all delayed rcu free inodes are flushed before we
9381 * destroy cache.
9382 */
9383 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009384 kmem_cache_destroy(btrfs_inode_cachep);
9385 kmem_cache_destroy(btrfs_trans_handle_cachep);
9386 kmem_cache_destroy(btrfs_transaction_cachep);
9387 kmem_cache_destroy(btrfs_path_cachep);
9388 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009389}
9390
9391int btrfs_init_cachep(void)
9392{
David Sterba837e1972012-09-07 03:00:48 -06009393 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009394 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009395 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9396 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009397 if (!btrfs_inode_cachep)
9398 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009399
David Sterba837e1972012-09-07 03:00:48 -06009400 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009401 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009402 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009403 if (!btrfs_trans_handle_cachep)
9404 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009405
David Sterba837e1972012-09-07 03:00:48 -06009406 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009407 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009408 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009409 if (!btrfs_transaction_cachep)
9410 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009411
David Sterba837e1972012-09-07 03:00:48 -06009412 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009413 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009414 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009415 if (!btrfs_path_cachep)
9416 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009417
David Sterba837e1972012-09-07 03:00:48 -06009418 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009419 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009420 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009421 if (!btrfs_free_space_cachep)
9422 goto fail;
9423
Chris Mason39279cc2007-06-12 06:35:45 -04009424 return 0;
9425fail:
9426 btrfs_destroy_cachep();
9427 return -ENOMEM;
9428}
9429
9430static int btrfs_getattr(struct vfsmount *mnt,
9431 struct dentry *dentry, struct kstat *stat)
9432{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009433 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009434 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009435 u32 blocksize = inode->i_sb->s_blocksize;
9436
Chris Mason39279cc2007-06-12 06:35:45 -04009437 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009438 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009439
9440 spin_lock(&BTRFS_I(inode)->lock);
9441 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9442 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009443 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009444 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009445 return 0;
9446}
9447
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009448static int btrfs_rename_exchange(struct inode *old_dir,
9449 struct dentry *old_dentry,
9450 struct inode *new_dir,
9451 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009452{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009453 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009454 struct btrfs_trans_handle *trans;
9455 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009456 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009457 struct inode *new_inode = new_dentry->d_inode;
9458 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009459 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009460 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009461 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9462 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009463 u64 old_idx = 0;
9464 u64 new_idx = 0;
9465 u64 root_objectid;
9466 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009467 bool root_log_pinned = false;
9468 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009469
9470 /* we only allow rename subvolume link between subvolumes */
9471 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9472 return -EXDEV;
9473
9474 /* close the race window with snapshot create/destroy ioctl */
9475 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009476 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009477 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009478 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009479
9480 /*
9481 * We want to reserve the absolute worst case amount of items. So if
9482 * both inodes are subvols and we need to unlink them then that would
9483 * require 4 item modifications, but if they are both normal inodes it
9484 * would require 5 item modifications, so we'll assume their normal
9485 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9486 * should cover the worst case number of items we'll modify.
9487 */
9488 trans = btrfs_start_transaction(root, 12);
9489 if (IS_ERR(trans)) {
9490 ret = PTR_ERR(trans);
9491 goto out_notrans;
9492 }
9493
9494 /*
9495 * We need to find a free sequence number both in the source and
9496 * in the destination directory for the exchange.
9497 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009498 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009499 if (ret)
9500 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009501 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009502 if (ret)
9503 goto out_fail;
9504
9505 BTRFS_I(old_inode)->dir_index = 0ULL;
9506 BTRFS_I(new_inode)->dir_index = 0ULL;
9507
9508 /* Reference for the source. */
9509 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9510 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009511 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009512 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009513 btrfs_pin_log_trans(root);
9514 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009515 ret = btrfs_insert_inode_ref(trans, dest,
9516 new_dentry->d_name.name,
9517 new_dentry->d_name.len,
9518 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009519 btrfs_ino(BTRFS_I(new_dir)),
9520 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009521 if (ret)
9522 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009523 }
9524
9525 /* And now for the dest. */
9526 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9527 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009528 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009529 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009530 btrfs_pin_log_trans(dest);
9531 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009532 ret = btrfs_insert_inode_ref(trans, root,
9533 old_dentry->d_name.name,
9534 old_dentry->d_name.len,
9535 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009536 btrfs_ino(BTRFS_I(old_dir)),
9537 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009538 if (ret)
9539 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009540 }
9541
9542 /* Update inode version and ctime/mtime. */
9543 inode_inc_iversion(old_dir);
9544 inode_inc_iversion(new_dir);
9545 inode_inc_iversion(old_inode);
9546 inode_inc_iversion(new_inode);
9547 old_dir->i_ctime = old_dir->i_mtime = ctime;
9548 new_dir->i_ctime = new_dir->i_mtime = ctime;
9549 old_inode->i_ctime = ctime;
9550 new_inode->i_ctime = ctime;
9551
9552 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009553 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9554 BTRFS_I(old_inode), 1);
9555 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9556 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009557 }
9558
9559 /* src is a subvolume */
9560 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9561 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9562 ret = btrfs_unlink_subvol(trans, root, old_dir,
9563 root_objectid,
9564 old_dentry->d_name.name,
9565 old_dentry->d_name.len);
9566 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009567 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9568 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 old_dentry->d_name.name,
9570 old_dentry->d_name.len);
9571 if (!ret)
9572 ret = btrfs_update_inode(trans, root, old_inode);
9573 }
9574 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009575 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009576 goto out_fail;
9577 }
9578
9579 /* dest is a subvolume */
9580 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9581 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9582 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9583 root_objectid,
9584 new_dentry->d_name.name,
9585 new_dentry->d_name.len);
9586 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009587 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9588 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009589 new_dentry->d_name.name,
9590 new_dentry->d_name.len);
9591 if (!ret)
9592 ret = btrfs_update_inode(trans, dest, new_inode);
9593 }
9594 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009595 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009596 goto out_fail;
9597 }
9598
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009599 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009600 new_dentry->d_name.name,
9601 new_dentry->d_name.len, 0, old_idx);
9602 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009603 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009604 goto out_fail;
9605 }
9606
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009607 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009608 old_dentry->d_name.name,
9609 old_dentry->d_name.len, 0, new_idx);
9610 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009611 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009612 goto out_fail;
9613 }
9614
9615 if (old_inode->i_nlink == 1)
9616 BTRFS_I(old_inode)->dir_index = old_idx;
9617 if (new_inode->i_nlink == 1)
9618 BTRFS_I(new_inode)->dir_index = new_idx;
9619
Filipe Manana86e8aa02016-05-05 02:02:27 +01009620 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009621 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009622 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9623 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009624 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009625 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009626 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009627 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009628 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009629 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9630 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009631 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009632 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009633 }
9634out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009635 /*
9636 * If we have pinned a log and an error happened, we unpin tasks
9637 * trying to sync the log and force them to fallback to a transaction
9638 * commit if the log currently contains any of the inodes involved in
9639 * this rename operation (to ensure we do not persist a log with an
9640 * inconsistent state for any of these inodes or leading to any
9641 * inconsistencies when replayed). If the transaction was aborted, the
9642 * abortion reason is propagated to userspace when attempting to commit
9643 * the transaction. If the log does not contain any of these inodes, we
9644 * allow the tasks to sync it.
9645 */
9646 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009647 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9648 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9649 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009650 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009651 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009652 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009653
9654 if (root_log_pinned) {
9655 btrfs_end_log_trans(root);
9656 root_log_pinned = false;
9657 }
9658 if (dest_log_pinned) {
9659 btrfs_end_log_trans(dest);
9660 dest_log_pinned = false;
9661 }
9662 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009663 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664out_notrans:
9665 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009666 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009667 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009668 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009669
9670 return ret;
9671}
9672
9673static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9674 struct btrfs_root *root,
9675 struct inode *dir,
9676 struct dentry *dentry)
9677{
9678 int ret;
9679 struct inode *inode;
9680 u64 objectid;
9681 u64 index;
9682
9683 ret = btrfs_find_free_ino(root, &objectid);
9684 if (ret)
9685 return ret;
9686
9687 inode = btrfs_new_inode(trans, root, dir,
9688 dentry->d_name.name,
9689 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009690 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009691 objectid,
9692 S_IFCHR | WHITEOUT_MODE,
9693 &index);
9694
9695 if (IS_ERR(inode)) {
9696 ret = PTR_ERR(inode);
9697 return ret;
9698 }
9699
9700 inode->i_op = &btrfs_special_inode_operations;
9701 init_special_inode(inode, inode->i_mode,
9702 WHITEOUT_DEV);
9703
9704 ret = btrfs_init_inode_security(trans, inode, dir,
9705 &dentry->d_name);
9706 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009707 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009709 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9710 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009711 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009712 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713
9714 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009715out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009717 if (ret)
9718 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009719 iput(inode);
9720
Filipe Mananac9901612016-05-05 01:41:57 +01009721 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009722}
9723
Chris Mason39279cc2007-06-12 06:35:45 -04009724static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009725 struct inode *new_dir, struct dentry *new_dentry,
9726 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009727{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009728 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009729 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009730 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009731 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9732 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009733 struct inode *new_inode = d_inode(new_dentry);
9734 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009735 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009736 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009737 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009738 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009739 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009740
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009741 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009742 return -EPERM;
9743
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009744 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009745 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009746 return -EXDEV;
9747
Li Zefan33345d012011-04-20 10:31:50 +08009748 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009749 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009750 return -ENOTEMPTY;
9751
Chris Mason39279cc2007-06-12 06:35:45 -04009752 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009753 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009754 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009755
9756
9757 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009758 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009759 new_dentry->d_name.name,
9760 new_dentry->d_name.len);
9761
9762 if (ret) {
9763 if (ret == -EEXIST) {
9764 /* we shouldn't get
9765 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309766 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009767 return ret;
9768 }
9769 } else {
9770 /* maybe -EOVERFLOW */
9771 return ret;
9772 }
9773 }
9774 ret = 0;
9775
Chris Mason5a3f23d2009-03-31 13:27:11 -04009776 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009777 * we're using rename to replace one file with another. Start IO on it
9778 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009779 */
Chris Mason8d875f92014-08-12 10:47:42 -07009780 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009781 filemap_flush(old_inode->i_mapping);
9782
Yan, Zheng76dda932009-09-21 16:00:26 -04009783 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009784 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009785 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009786 /*
9787 * We want to reserve the absolute worst case amount of items. So if
9788 * both inodes are subvols and we need to unlink them then that would
9789 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009790 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009791 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9792 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009793 * If our rename has the whiteout flag, we need more 5 units for the
9794 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9795 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009796 */
Filipe Manana5062af32016-05-05 10:26:26 +01009797 trans_num_items = 11;
9798 if (flags & RENAME_WHITEOUT)
9799 trans_num_items += 5;
9800 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009801 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009802 ret = PTR_ERR(trans);
9803 goto out_notrans;
9804 }
Chris Mason5f39d392007-10-15 16:14:19 -04009805
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009806 if (dest != root)
9807 btrfs_record_root_in_trans(trans, dest);
9808
Nikolay Borisov877574e2017-02-20 13:50:33 +02009809 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009810 if (ret)
9811 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009812
Miao Xie67de1172013-12-26 13:07:06 +08009813 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009814 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009815 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009816 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009817 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009818 btrfs_pin_log_trans(root);
9819 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009820 ret = btrfs_insert_inode_ref(trans, dest,
9821 new_dentry->d_name.name,
9822 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009823 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009824 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009825 if (ret)
9826 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009827 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009828
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009829 inode_inc_iversion(old_dir);
9830 inode_inc_iversion(new_dir);
9831 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009832 old_dir->i_ctime = old_dir->i_mtime =
9833 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009834 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009835
Chris Mason12fcfd22009-03-24 10:24:20 -04009836 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009837 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9838 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009839
Li Zefan33345d012011-04-20 10:31:50 +08009840 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009841 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9842 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9843 old_dentry->d_name.name,
9844 old_dentry->d_name.len);
9845 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009846 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9847 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009848 old_dentry->d_name.name,
9849 old_dentry->d_name.len);
9850 if (!ret)
9851 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009852 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009853 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009854 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009855 goto out_fail;
9856 }
Chris Mason39279cc2007-06-12 06:35:45 -04009857
9858 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009859 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009860 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009861 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009862 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9863 root_objectid = BTRFS_I(new_inode)->location.objectid;
9864 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9865 root_objectid,
9866 new_dentry->d_name.name,
9867 new_dentry->d_name.len);
9868 BUG_ON(new_inode->i_nlink == 0);
9869 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009870 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9871 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009872 new_dentry->d_name.name,
9873 new_dentry->d_name.len);
9874 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009875 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009876 ret = btrfs_orphan_add(trans,
9877 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009878 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009879 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009880 goto out_fail;
9881 }
Chris Mason39279cc2007-06-12 06:35:45 -04009882 }
Josef Bacikaec74772008-07-24 12:12:38 -04009883
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009884 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009885 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009886 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009887 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009888 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009889 goto out_fail;
9890 }
Chris Mason39279cc2007-06-12 06:35:45 -04009891
Miao Xie67de1172013-12-26 13:07:06 +08009892 if (old_inode->i_nlink == 1)
9893 BTRFS_I(old_inode)->dir_index = index;
9894
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009895 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009896 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009897
David Sterbaf85b7372017-01-20 14:54:07 +01009898 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9899 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009900 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009901 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009902 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009903
9904 if (flags & RENAME_WHITEOUT) {
9905 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9906 old_dentry);
9907
9908 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009909 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009910 goto out_fail;
9911 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009912 }
Chris Mason39279cc2007-06-12 06:35:45 -04009913out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009914 /*
9915 * If we have pinned the log and an error happened, we unpin tasks
9916 * trying to sync the log and force them to fallback to a transaction
9917 * commit if the log currently contains any of the inodes involved in
9918 * this rename operation (to ensure we do not persist a log with an
9919 * inconsistent state for any of these inodes or leading to any
9920 * inconsistencies when replayed). If the transaction was aborted, the
9921 * abortion reason is propagated to userspace when attempting to commit
9922 * the transaction. If the log does not contain any of these inodes, we
9923 * allow the tasks to sync it.
9924 */
9925 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009926 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9927 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9928 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009929 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009930 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009931 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009932
9933 btrfs_end_log_trans(root);
9934 log_pinned = false;
9935 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009936 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009937out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009938 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009939 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009940
Chris Mason39279cc2007-06-12 06:35:45 -04009941 return ret;
9942}
9943
Miklos Szeredi80ace852014-07-23 15:15:32 +02009944static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9945 struct inode *new_dir, struct dentry *new_dentry,
9946 unsigned int flags)
9947{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009948 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009949 return -EINVAL;
9950
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009951 if (flags & RENAME_EXCHANGE)
9952 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9953 new_dentry);
9954
9955 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009956}
9957
Miao Xie8ccf6f192012-10-25 09:28:04 +00009958static void btrfs_run_delalloc_work(struct btrfs_work *work)
9959{
9960 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009961 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009962
9963 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9964 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009965 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009966 filemap_flush(inode->i_mapping);
9967 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9968 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009969 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009970
9971 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009972 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009973 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009974 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009975 complete(&delalloc_work->completion);
9976}
9977
9978struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009979 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009980{
9981 struct btrfs_delalloc_work *work;
9982
David Sterba100d5702015-12-08 14:39:32 +01009983 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009984 if (!work)
9985 return NULL;
9986
9987 init_completion(&work->completion);
9988 INIT_LIST_HEAD(&work->list);
9989 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009990 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009991 WARN_ON_ONCE(!inode);
9992 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9993 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009994
9995 return work;
9996}
9997
9998void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9999{
10000 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010001 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010002}
10003
Chris Masond352ac62008-09-29 15:18:18 -040010004/*
10005 * some fairly slow code that needs optimization. This walks the list
10006 * of all the inodes with pending delalloc and forces them to disk.
10007 */
Miao Xie6c255e62014-03-06 13:55:01 +080010008static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10009 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010010{
Chris Masonea8c2812008-08-04 23:17:27 -040010011 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010012 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010013 struct btrfs_delalloc_work *work, *next;
10014 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010015 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010016 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010017
Miao Xie8ccf6f192012-10-25 09:28:04 +000010018 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010019 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010020
Miao Xie573bfb72014-03-06 13:55:03 +080010021 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010022 spin_lock(&root->delalloc_lock);
10023 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010024 while (!list_empty(&splice)) {
10025 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010026 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010027
Miao Xieeb73c1b2013-05-15 07:48:22 +000010028 list_move_tail(&binode->delalloc_inodes,
10029 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010030 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010031 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010032 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010033 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010034 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010035 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010036
David Sterba651d4942015-11-27 19:24:16 +010010037 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010038 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010039 if (delay_iput)
10040 btrfs_add_delayed_iput(inode);
10041 else
10042 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010043 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010044 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010045 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010046 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010047 btrfs_queue_work(root->fs_info->flush_workers,
10048 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010049 ret++;
10050 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010051 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010052 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010053 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010054 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010055 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010056
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010057out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010058 list_for_each_entry_safe(work, next, &works, list) {
10059 list_del_init(&work->list);
10060 btrfs_wait_and_free_delalloc_work(work);
10061 }
10062
Miao Xieeb73c1b2013-05-15 07:48:22 +000010063 if (!list_empty_careful(&splice)) {
10064 spin_lock(&root->delalloc_lock);
10065 list_splice_tail(&splice, &root->delalloc_inodes);
10066 spin_unlock(&root->delalloc_lock);
10067 }
Miao Xie573bfb72014-03-06 13:55:03 +080010068 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010069 return ret;
10070}
10071
10072int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10073{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010074 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010075 int ret;
10076
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010077 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010078 return -EROFS;
10079
Miao Xie6c255e62014-03-06 13:55:01 +080010080 ret = __start_delalloc_inodes(root, delay_iput, -1);
10081 if (ret > 0)
10082 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010083 /*
10084 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010085 * we have to make sure the IO is actually started and that
10086 * ordered extents get created before we return
10087 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010088 atomic_inc(&fs_info->async_submit_draining);
10089 while (atomic_read(&fs_info->nr_async_submits) ||
10090 atomic_read(&fs_info->async_delalloc_pages)) {
10091 wait_event(fs_info->async_submit_wait,
10092 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10093 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010094 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010095 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010096 return ret;
10097}
10098
Miao Xie6c255e62014-03-06 13:55:01 +080010099int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10100 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010101{
10102 struct btrfs_root *root;
10103 struct list_head splice;
10104 int ret;
10105
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010106 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010107 return -EROFS;
10108
10109 INIT_LIST_HEAD(&splice);
10110
Miao Xie573bfb72014-03-06 13:55:03 +080010111 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010112 spin_lock(&fs_info->delalloc_root_lock);
10113 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010114 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010115 root = list_first_entry(&splice, struct btrfs_root,
10116 delalloc_root);
10117 root = btrfs_grab_fs_root(root);
10118 BUG_ON(!root);
10119 list_move_tail(&root->delalloc_root,
10120 &fs_info->delalloc_roots);
10121 spin_unlock(&fs_info->delalloc_root_lock);
10122
Miao Xie6c255e62014-03-06 13:55:01 +080010123 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010124 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010125 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010126 goto out;
10127
Miao Xie6c255e62014-03-06 13:55:01 +080010128 if (nr != -1) {
10129 nr -= ret;
10130 WARN_ON(nr < 0);
10131 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010132 spin_lock(&fs_info->delalloc_root_lock);
10133 }
10134 spin_unlock(&fs_info->delalloc_root_lock);
10135
Miao Xie6c255e62014-03-06 13:55:01 +080010136 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010137 atomic_inc(&fs_info->async_submit_draining);
10138 while (atomic_read(&fs_info->nr_async_submits) ||
10139 atomic_read(&fs_info->async_delalloc_pages)) {
10140 wait_event(fs_info->async_submit_wait,
10141 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10142 atomic_read(&fs_info->async_delalloc_pages) == 0));
10143 }
10144 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010145out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010146 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010147 spin_lock(&fs_info->delalloc_root_lock);
10148 list_splice_tail(&splice, &fs_info->delalloc_roots);
10149 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010150 }
Miao Xie573bfb72014-03-06 13:55:03 +080010151 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010152 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010153}
10154
Chris Mason39279cc2007-06-12 06:35:45 -040010155static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10156 const char *symname)
10157{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010158 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010159 struct btrfs_trans_handle *trans;
10160 struct btrfs_root *root = BTRFS_I(dir)->root;
10161 struct btrfs_path *path;
10162 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010163 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010164 int err;
10165 int drop_inode = 0;
10166 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010167 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010168 int name_len;
10169 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010170 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010171 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010172 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010173
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010174 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010175 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010176 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010177
Josef Bacik9ed74f22009-09-11 16:12:44 -040010178 /*
10179 * 2 items for inode item and ref
10180 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010181 * 1 item for updating parent inode item
10182 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010183 * 1 item for xattr if selinux is on
10184 */
Filipe Manana9269d122015-12-31 18:16:29 +000010185 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010186 if (IS_ERR(trans))
10187 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010188
Li Zefan581bb052011-04-20 10:06:11 +080010189 err = btrfs_find_free_ino(root, &objectid);
10190 if (err)
10191 goto out_unlock;
10192
Josef Bacikaec74772008-07-24 12:12:38 -040010193 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010194 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10195 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010196 if (IS_ERR(inode)) {
10197 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010198 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010199 }
Chris Mason39279cc2007-06-12 06:35:45 -040010200
Casey Schauflerad19db72011-12-15 10:09:07 -050010201 /*
10202 * If the active LSM wants to access the inode during
10203 * d_instantiate it needs these. Smack checks to see
10204 * if the filesystem supports xattrs by looking at the
10205 * ops vector.
10206 */
10207 inode->i_fop = &btrfs_file_operations;
10208 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010209 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010210 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10211
10212 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10213 if (err)
10214 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010215
Chris Mason39279cc2007-06-12 06:35:45 -040010216 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010217 if (!path) {
10218 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010219 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010220 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010221 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010222 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010223 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010224 datasize = btrfs_file_extent_calc_inline_size(name_len);
10225 err = btrfs_insert_empty_item(trans, root, path, &key,
10226 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010227 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010228 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010229 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010230 }
Chris Mason5f39d392007-10-15 16:14:19 -040010231 leaf = path->nodes[0];
10232 ei = btrfs_item_ptr(leaf, path->slots[0],
10233 struct btrfs_file_extent_item);
10234 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10235 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010236 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010237 btrfs_set_file_extent_encryption(leaf, ei, 0);
10238 btrfs_set_file_extent_compression(leaf, ei, 0);
10239 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10240 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10241
Chris Mason39279cc2007-06-12 06:35:45 -040010242 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010243 write_extent_buffer(leaf, symname, ptr, name_len);
10244 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010245 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010246
Chris Mason39279cc2007-06-12 06:35:45 -040010247 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010248 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010249 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010250 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010251 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010252 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010253 /*
10254 * Last step, add directory indexes for our symlink inode. This is the
10255 * last step to avoid extra cleanup of these indexes if an error happens
10256 * elsewhere above.
10257 */
10258 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010259 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10260 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010261 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010262 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010263 goto out_unlock_inode;
10264 }
10265
10266 unlock_new_inode(inode);
10267 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010268
10269out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010270 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010271 if (drop_inode) {
10272 inode_dec_link_count(inode);
10273 iput(inode);
10274 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010275 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010276 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010277
10278out_unlock_inode:
10279 drop_inode = 1;
10280 unlock_new_inode(inode);
10281 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010282}
Chris Mason16432982008-04-10 10:23:21 -040010283
Josef Bacik0af3d002010-06-21 14:48:16 -040010284static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10285 u64 start, u64 num_bytes, u64 min_size,
10286 loff_t actual_len, u64 *alloc_hint,
10287 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010288{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010289 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010290 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10291 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010292 struct btrfs_root *root = BTRFS_I(inode)->root;
10293 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010294 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010295 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010296 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010297 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010298 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010299 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010300 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010301
Josef Bacik0af3d002010-06-21 14:48:16 -040010302 if (trans)
10303 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010304 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010305 if (own_trans) {
10306 trans = btrfs_start_transaction(root, 3);
10307 if (IS_ERR(trans)) {
10308 ret = PTR_ERR(trans);
10309 break;
10310 }
Yan Zhengd899e052008-10-30 14:25:28 -040010311 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010312
Byongho Leeee221842015-12-15 01:42:10 +090010313 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010314 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010315 /*
10316 * If we are severely fragmented we could end up with really
10317 * small allocations, so if the allocator is returning small
10318 * chunks lets make its job easier by only searching for those
10319 * sized chunks.
10320 */
10321 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010322 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10323 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010324 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010325 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010326 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010327 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010328 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010329 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010330
Josef Bacik0b670dc2015-09-23 17:11:16 -040010331 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010332 ret = insert_reserved_file_extent(trans, inode,
10333 cur_offset, ins.objectid,
10334 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010335 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010336 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010337 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010338 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010339 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010340 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010341 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010342 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010343 break;
10344 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010345
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010346 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010347 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010348
Josef Bacik5dc562c2012-08-17 13:14:17 -040010349 em = alloc_extent_map();
10350 if (!em) {
10351 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10352 &BTRFS_I(inode)->runtime_flags);
10353 goto next;
10354 }
10355
10356 em->start = cur_offset;
10357 em->orig_start = cur_offset;
10358 em->len = ins.offset;
10359 em->block_start = ins.objectid;
10360 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010361 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010362 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010363 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010364 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10365 em->generation = trans->transid;
10366
10367 while (1) {
10368 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010369 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010370 write_unlock(&em_tree->lock);
10371 if (ret != -EEXIST)
10372 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010373 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010374 cur_offset + ins.offset - 1,
10375 0);
10376 }
10377 free_extent_map(em);
10378next:
Yan Zhengd899e052008-10-30 14:25:28 -040010379 num_bytes -= ins.offset;
10380 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010381 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010382
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010383 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010384 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010385 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010386 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010387 (actual_len > inode->i_size) &&
10388 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010389 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010390 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010391 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010392 i_size = cur_offset;
10393 i_size_write(inode, i_size);
10394 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010395 }
10396
Yan Zhengd899e052008-10-30 14:25:28 -040010397 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010398
10399 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010400 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010401 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010402 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010403 break;
10404 }
Yan Zhengd899e052008-10-30 14:25:28 -040010405
Josef Bacik0af3d002010-06-21 14:48:16 -040010406 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010407 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010408 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010409 if (cur_offset < end)
10410 btrfs_free_reserved_data_space(inode, cur_offset,
10411 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010412 return ret;
10413}
10414
Josef Bacik0af3d002010-06-21 14:48:16 -040010415int btrfs_prealloc_file_range(struct inode *inode, 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,
10421 NULL);
10422}
10423
10424int btrfs_prealloc_file_range_trans(struct inode *inode,
10425 struct btrfs_trans_handle *trans, int mode,
10426 u64 start, u64 num_bytes, u64 min_size,
10427 loff_t actual_len, u64 *alloc_hint)
10428{
10429 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10430 min_size, actual_len, alloc_hint, trans);
10431}
10432
Chris Masone6dcd2d2008-07-17 12:53:50 -040010433static int btrfs_set_page_dirty(struct page *page)
10434{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010435 return __set_page_dirty_nobuffers(page);
10436}
10437
Al Viro10556cb2011-06-20 19:28:19 -040010438static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010439{
Li Zefanb83cc962010-12-20 16:04:08 +080010440 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010441 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010442
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010443 if (mask & MAY_WRITE &&
10444 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10445 if (btrfs_root_readonly(root))
10446 return -EROFS;
10447 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10448 return -EACCES;
10449 }
Al Viro2830ba72011-06-20 19:16:29 -040010450 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010451}
Chris Mason39279cc2007-06-12 06:35:45 -040010452
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010453static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10454{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010455 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010456 struct btrfs_trans_handle *trans;
10457 struct btrfs_root *root = BTRFS_I(dir)->root;
10458 struct inode *inode = NULL;
10459 u64 objectid;
10460 u64 index;
10461 int ret = 0;
10462
10463 /*
10464 * 5 units required for adding orphan entry
10465 */
10466 trans = btrfs_start_transaction(root, 5);
10467 if (IS_ERR(trans))
10468 return PTR_ERR(trans);
10469
10470 ret = btrfs_find_free_ino(root, &objectid);
10471 if (ret)
10472 goto out;
10473
10474 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010475 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010476 if (IS_ERR(inode)) {
10477 ret = PTR_ERR(inode);
10478 inode = NULL;
10479 goto out;
10480 }
10481
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010482 inode->i_fop = &btrfs_file_operations;
10483 inode->i_op = &btrfs_file_inode_operations;
10484
10485 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010486 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10487
Chris Masonb0d5d102014-09-08 13:08:51 -070010488 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10489 if (ret)
10490 goto out_inode;
10491
10492 ret = btrfs_update_inode(trans, root, inode);
10493 if (ret)
10494 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010495 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010496 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010497 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010498
Filipe Manana5762b5c2014-08-01 00:10:32 +010010499 /*
10500 * We set number of links to 0 in btrfs_new_inode(), and here we set
10501 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10502 * through:
10503 *
10504 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10505 */
10506 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010507 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010508 d_tmpfile(dentry, inode);
10509 mark_inode_dirty(inode);
10510
10511out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010512 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010513 if (ret)
10514 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010515 btrfs_balance_delayed_items(fs_info);
10516 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010517 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010518
10519out_inode:
10520 unlock_new_inode(inode);
10521 goto out;
10522
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010523}
10524
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010525static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010526 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010527 .lookup = btrfs_lookup,
10528 .create = btrfs_create,
10529 .unlink = btrfs_unlink,
10530 .link = btrfs_link,
10531 .mkdir = btrfs_mkdir,
10532 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010533 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010534 .symlink = btrfs_symlink,
10535 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010536 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010537 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010538 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010539 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010540 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010541 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010542 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010543};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010544static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010545 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010546 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010547 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010548};
Yan, Zheng76dda932009-09-21 16:00:26 -040010549
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010550static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010551 .llseek = generic_file_llseek,
10552 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010553 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010554 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010555#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010556 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010557#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010558 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010559 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010560};
10561
David Sterba20e55062015-11-19 11:42:28 +010010562static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010563 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010564 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010565 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010566 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010567 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010568 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010569 .set_bit_hook = btrfs_set_bit_hook,
10570 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010571 .merge_extent_hook = btrfs_merge_extent_hook,
10572 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010573};
10574
Chris Mason35054392009-01-21 13:11:13 -050010575/*
10576 * btrfs doesn't support the bmap operation because swapfiles
10577 * use bmap to make a mapping of extents in the file. They assume
10578 * these extents won't change over the life of the file and they
10579 * use the bmap result to do IO directly to the drive.
10580 *
10581 * the btrfs bmap call would return logical addresses that aren't
10582 * suitable for IO and they also will change frequently as COW
10583 * operations happen. So, swapfile + btrfs == corruption.
10584 *
10585 * For now we're avoiding this by dropping bmap.
10586 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010587static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010588 .readpage = btrfs_readpage,
10589 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010590 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010591 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010592 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010593 .invalidatepage = btrfs_invalidatepage,
10594 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010595 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010596 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010597};
10598
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010599static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010600 .readpage = btrfs_readpage,
10601 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010602 .invalidatepage = btrfs_invalidatepage,
10603 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010604};
10605
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010606static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010607 .getattr = btrfs_getattr,
10608 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010609 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010610 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010611 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010612 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010613 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010614 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010615};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010616static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010617 .getattr = btrfs_getattr,
10618 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010619 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010620 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010621 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010622 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010623 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010624};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010625static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010626 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010627 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010628 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010629 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010630 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010631 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010632};
Yan, Zheng76dda932009-09-21 16:00:26 -040010633
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010634const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010635 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010636 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010637};