blob: ac5f8c4042683a7506620a78d6559cdcd70cee44 [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);
Josef Bacik70c8a912012-10-11 16:54:30 -0400112static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
113 u64 len, u64 orig_start,
114 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400115 u64 orig_block_len, u64 ram_bytes,
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);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400319 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400320 btrfs_drop_extent_cache(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
Anand Jain26d30f82016-12-19 19:09:06 +0800392static inline void inode_should_defrag(struct inode *inode,
393 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 &&
397 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
398 btrfs_add_inode_defrag(NULL, inode);
399}
400
Chris Masonc8b97812008-10-29 14:49:59 -0400401/*
Chris Mason771ed682008-11-06 22:02:51 -0500402 * we create compressed extents in two phases. The first
403 * phase compresses a range of pages that have already been
404 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400405 *
Chris Mason771ed682008-11-06 22:02:51 -0500406 * This is done inside an ordered work queue, and the compression
407 * is spread across many cpus. The actual IO submission is step
408 * two, and the ordered work queue takes care of making sure that
409 * happens in the same order things were put onto the queue by
410 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400411 *
Chris Mason771ed682008-11-06 22:02:51 -0500412 * If this code finds it can't get good compression, it puts an
413 * entry onto the work queue to write the uncompressed bytes. This
414 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300415 * are written in the same order that the flusher thread sent them
416 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400417 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100418static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500419 struct page *locked_page,
420 u64 start, u64 end,
421 struct async_cow *async_cow,
422 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400423{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db2b2007-08-27 16:49:44 -0400425 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400426 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400427 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400428 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500429 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400430 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400431 struct page **pages = NULL;
432 unsigned long nr_pages;
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 Masonb888db2b2007-08-27 16:49:44 -0400442
Anand Jain26d30f82016-12-19 19:09:06 +0800443 inode_should_defrag(inode, start, end, end - start + 1, SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400444
Chris Mason42dc7ba2008-12-15 11:44:56 -0500445 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400446again:
447 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300448 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
449 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400450
Chris Masonf03d9301f2009-02-04 09:31:06 -0500451 /*
452 * we don't want to send crud past the end of i_size through
453 * compression, that's just a waste of CPU time. So, if the
454 * end of the file is before the start of our current
455 * requested range of bytes, we bail out to the uncompressed
456 * cleanup code that can deal with all of this.
457 *
458 * It isn't really the fastest way to fix things, but this is a
459 * very uncommon corner.
460 */
461 if (actual_end <= start)
462 goto cleanup_and_bail_uncompressed;
463
Chris Masonc8b97812008-10-29 14:49:59 -0400464 total_compressed = actual_end - start;
465
Shilong Wang4bcbb332014-10-07 18:44:35 -0400466 /*
467 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400468 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400469 */
470 if (total_compressed <= blocksize &&
471 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
472 goto cleanup_and_bail_uncompressed;
473
Chris Masonc8b97812008-10-29 14:49:59 -0400474 /* we want to make sure that amount of ram required to uncompress
475 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500476 * of a compressed extent to 128k. This is a crucial number
477 * because it also controls how easily we can spread reads across
478 * cpus for decompression.
479 *
480 * We also want to make sure the amount of IO required to do
481 * a random read is reasonably small, so we limit the size of
482 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400483 */
484 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000485 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500486 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400487 total_in = 0;
488 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400489
Chris Mason771ed682008-11-06 22:02:51 -0500490 /*
491 * we do compression for mount -o compress and when the
492 * inode has not been flagged as nocompress. This flag can
493 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400494 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800495 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400496 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100497 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800498 if (!pages) {
499 /* just bail out to the uncompressed code */
500 goto cont;
501 }
Chris Mason179e29e2007-11-01 11:28:41 -0400502
Li Zefan261507a02010-12-17 14:21:50 +0800503 if (BTRFS_I(inode)->force_compress)
504 compress_type = BTRFS_I(inode)->force_compress;
505
Chris Mason4adaa612013-03-26 13:07:00 -0400506 /*
507 * we need to call clear_page_dirty_for_io on each
508 * page in the range. Otherwise applications with the file
509 * mmap'd can wander in and change the page contents while
510 * we are compressing them.
511 *
512 * If the compression fails for any reason, we set the pages
513 * dirty again later on.
514 */
515 extent_range_clear_dirty_for_io(inode, start, end);
516 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800517 ret = btrfs_compress_pages(compress_type,
518 inode->i_mapping, start,
519 total_compressed, pages,
520 nr_pages, &nr_pages_ret,
521 &total_in,
522 &total_compressed,
523 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400524
525 if (!ret) {
526 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300527 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 struct page *page = pages[nr_pages_ret - 1];
529 char *kaddr;
530
531 /* zero the tail end of the last page, we might be
532 * sending it down to disk
533 */
534 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800535 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400536 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300537 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800538 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400539 }
540 will_compress = 1;
541 }
542 }
Li Zefan560f7d72011-09-08 10:22:01 +0800543cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400544 if (start == 0) {
545 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500546 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400547 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500548 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400549 */
Josef Bacik00361582013-08-14 14:02:47 -0400550 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800551 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400552 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500553 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400554 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000555 total_compressed,
556 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400557 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100558 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400559 unsigned long clear_flags = EXTENT_DELALLOC |
560 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100561 unsigned long page_error_op;
562
Josef Bacik151a41b2013-07-29 13:22:24 -0400563 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100564 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400565
Chris Mason771ed682008-11-06 22:02:51 -0500566 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100567 * inline extent creation worked or returned error,
568 * we don't need to create any more async work items.
569 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500570 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800571 extent_clear_unlock_delalloc(inode, start, end, end,
572 NULL, clear_flags,
573 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400574 PAGE_CLEAR_DIRTY |
575 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100576 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400577 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800578 btrfs_free_reserved_data_space_noquota(inode, start,
579 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400580 goto free_pages_out;
581 }
582 }
583
584 if (will_compress) {
585 /*
586 * we aren't doing an inline extent round the compressed size
587 * up to a block size boundary so the allocator does sane
588 * things
589 */
Qu Wenruofda28322013-02-26 08:10:22 +0000590 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400591
592 /*
593 * one last check to make sure the compression is really a
594 * win, compare the page count read with the blocks on disk
595 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300596 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400597 if (total_compressed >= total_in) {
598 will_compress = 0;
599 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400600 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700601 *num_added += 1;
602
603 /*
604 * The async work queues will take care of doing actual
605 * allocation on disk for these compressed pages, and
606 * will submit them to the elevator.
607 */
608 add_async_extent(async_cow, start, num_bytes,
609 total_compressed, pages, nr_pages_ret,
610 compress_type);
611
612 if (start + num_bytes < end) {
613 start += num_bytes;
614 pages = NULL;
615 cond_resched();
616 goto again;
617 }
618 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400619 }
620 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700621 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400622 /*
623 * the compression code ran but failed to make things smaller,
624 * free any pages it allocated and our page pointer array
625 */
626 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400627 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300628 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400629 }
630 kfree(pages);
631 pages = NULL;
632 total_compressed = 0;
633 nr_pages_ret = 0;
634
635 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400636 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500637 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500638 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500639 }
Chris Masonc8b97812008-10-29 14:49:59 -0400640 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500641cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700642 /*
643 * No compression, but we still need to write the pages in the file
644 * we've been given so far. redirty the locked page if it corresponds
645 * to our extent and set things up for the async work queue to run
646 * cow_file_range to do the normal delalloc dance.
647 */
648 if (page_offset(locked_page) >= start &&
649 page_offset(locked_page) <= end)
650 __set_page_dirty_nobuffers(locked_page);
651 /* unlocked later on in the async handlers */
652
653 if (redirty)
654 extent_range_redirty_for_io(inode, start, end);
655 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
656 BTRFS_COMPRESS_NONE);
657 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500658
Filipe Mananac44f6492014-10-09 21:15:44 +0100659 return;
Chris Mason771ed682008-11-06 22:02:51 -0500660
661free_pages_out:
662 for (i = 0; i < nr_pages_ret; i++) {
663 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300664 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500665 }
Chris Masond3977122009-01-05 21:25:51 -0500666 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500667}
Chris Mason771ed682008-11-06 22:02:51 -0500668
Filipe Manana40ae8372014-10-06 22:14:24 +0100669static void free_async_extent_pages(struct async_extent *async_extent)
670{
671 int i;
672
673 if (!async_extent->pages)
674 return;
675
676 for (i = 0; i < async_extent->nr_pages; i++) {
677 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300678 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100679 }
680 kfree(async_extent->pages);
681 async_extent->nr_pages = 0;
682 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500683}
684
685/*
686 * phase two of compressed writeback. This is the ordered portion
687 * of the code, which only gets called in the order the work was
688 * queued. We walk all the async extents created by compress_file_range
689 * and send them down to the disk.
690 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100691static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500692 struct async_cow *async_cow)
693{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500695 struct async_extent *async_extent;
696 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500697 struct btrfs_key ins;
698 struct extent_map *em;
699 struct btrfs_root *root = BTRFS_I(inode)->root;
700 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
701 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 */
788 btrfs_drop_extent_cache(inode, async_extent->start,
789 async_extent->start +
790 async_extent->ram_size - 1, 0);
791
David Sterba172ddd62011-04-21 00:48:27 +0200792 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000793 if (!em) {
794 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500795 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000796 }
Chris Mason771ed682008-11-06 22:02:51 -0500797 em->start = async_extent->start;
798 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500799 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400800 em->mod_start = em->start;
801 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500802
803 em->block_start = ins.objectid;
804 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500805 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400806 em->ram_bytes = async_extent->ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400807 em->bdev = fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800808 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500809 set_bit(EXTENT_FLAG_PINNED, &em->flags);
810 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400811 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500812
Chris Masond3977122009-01-05 21:25:51 -0500813 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400814 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400815 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400816 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500817 if (ret != -EEXIST) {
818 free_extent_map(em);
819 break;
820 }
821 btrfs_drop_extent_cache(inode, async_extent->start,
822 async_extent->start +
823 async_extent->ram_size - 1, 0);
824 }
825
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826 if (ret)
827 goto out_free_reserve;
828
Li Zefan261507a02010-12-17 14:21:50 +0800829 ret = btrfs_add_ordered_extent_compress(inode,
830 async_extent->start,
831 ins.objectid,
832 async_extent->ram_size,
833 ins.offset,
834 BTRFS_ORDERED_COMPRESSED,
835 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100836 if (ret) {
837 btrfs_drop_extent_cache(inode, async_extent->start,
838 async_extent->start +
839 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500840 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100841 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400842 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500843
Chris Mason771ed682008-11-06 22:02:51 -0500844 /*
845 * clear dirty, set writeback and unlock the pages.
846 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400847 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400848 async_extent->start +
849 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800850 async_extent->start +
851 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400852 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
853 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400854 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500855 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500856 async_extent->start,
857 async_extent->ram_size,
858 ins.objectid,
859 ins.offset, async_extent->pages,
860 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 if (ret) {
862 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
863 struct page *p = async_extent->pages[0];
864 const u64 start = async_extent->start;
865 const u64 end = start + async_extent->ram_size - 1;
866
867 p->mapping = inode->i_mapping;
868 tree->ops->writepage_end_io_hook(p, start, end,
869 NULL, 0);
870 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800871 extent_clear_unlock_delalloc(inode, start, end, end,
872 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100873 PAGE_END_WRITEBACK |
874 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100875 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100876 }
Chris Mason771ed682008-11-06 22:02:51 -0500877 alloc_hint = ins.objectid + ins.offset;
878 kfree(async_extent);
879 cond_resched();
880 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100881 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500882out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400883 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400884 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400886 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887 async_extent->start +
888 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800889 async_extent->start +
890 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400891 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400892 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
893 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100894 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
895 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100896 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100897 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500898 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500899}
900
Josef Bacik4b46fce2010-05-23 11:00:55 -0400901static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
902 u64 num_bytes)
903{
904 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
905 struct extent_map *em;
906 u64 alloc_hint = 0;
907
908 read_lock(&em_tree->lock);
909 em = search_extent_mapping(em_tree, start, num_bytes);
910 if (em) {
911 /*
912 * if block start isn't an actual block number then find the
913 * first block in this inode and use that as a hint. If that
914 * block is also bogus then just don't worry about it.
915 */
916 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
917 free_extent_map(em);
918 em = search_extent_mapping(em_tree, 0, 0);
919 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
920 alloc_hint = em->block_start;
921 if (em)
922 free_extent_map(em);
923 } else {
924 alloc_hint = em->block_start;
925 free_extent_map(em);
926 }
927 }
928 read_unlock(&em_tree->lock);
929
930 return alloc_hint;
931}
932
Chris Mason771ed682008-11-06 22:02:51 -0500933/*
934 * when extent_io.c finds a delayed allocation range in the file,
935 * the call backs end up in this code. The basic idea is to
936 * allocate extents on disk for the range, and create ordered data structs
937 * in ram to track those extents.
938 *
939 * locked_page is the page that writepage had locked already. We use
940 * it to make sure we don't do extra locks or unlocks.
941 *
942 * *page_started is set to one if we unlock locked_page and do everything
943 * required to start IO on it. It may be clean and already done with
944 * IO when we return.
945 */
Josef Bacik00361582013-08-14 14:02:47 -0400946static noinline int cow_file_range(struct inode *inode,
947 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800948 u64 start, u64 end, u64 delalloc_end,
949 int *page_started, unsigned long *nr_written,
950 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500951{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400952 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400953 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500954 u64 alloc_hint = 0;
955 u64 num_bytes;
956 unsigned long ram_size;
957 u64 disk_num_bytes;
958 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400959 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500960 struct btrfs_key ins;
961 struct extent_map *em;
962 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
963 int ret = 0;
964
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400965 if (btrfs_is_free_space_inode(inode)) {
966 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500967 ret = -EINVAL;
968 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400969 }
Chris Mason771ed682008-11-06 22:02:51 -0500970
Qu Wenruofda28322013-02-26 08:10:22 +0000971 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500972 num_bytes = max(blocksize, num_bytes);
973 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500974
Anand Jain26d30f82016-12-19 19:09:06 +0800975 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400976
Chris Mason771ed682008-11-06 22:02:51 -0500977 if (start == 0) {
978 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800979 ret = cow_file_range_inline(root, inode, start, end, 0,
980 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500981 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800982 extent_clear_unlock_delalloc(inode, start, end,
983 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400984 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400985 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400986 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
987 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800988 btrfs_free_reserved_data_space_noquota(inode, start,
989 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500990 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300991 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500992 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500993 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100995 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500996 }
997 }
Chris Masonc8b97812008-10-29 14:49:59 -0400998
999 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001000 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001001
Josef Bacik4b46fce2010-05-23 11:00:55 -04001002 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001003 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001004
Chris Masond3977122009-01-05 21:25:51 -05001005 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -04001006 unsigned long op;
1007
Josef Bacik287a0ab2010-03-19 18:07:23 +00001008 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001009 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001010 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001011 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001012 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001013 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001014
David Sterba172ddd62011-04-21 00:48:27 +02001015 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +00001016 if (!em) {
1017 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001018 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001019 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001020 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001021 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001022 ram_size = ins.offset;
1023 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001024 em->mod_start = em->start;
1025 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001026
Chris Masone6dcd2d2008-07-17 12:53:50 -04001027 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001028 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001029 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001030 em->ram_bytes = ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001031 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001032 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001033 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001034
Chris Masond3977122009-01-05 21:25:51 -05001035 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001036 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001037 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001038 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001039 if (ret != -EEXIST) {
1040 free_extent_map(em);
1041 break;
1042 }
1043 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001044 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001045 }
Liu Boace68ba2013-04-22 10:53:47 +00001046 if (ret)
1047 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001048
Chris Mason98d20f62008-04-14 09:46:10 -04001049 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001050 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001051 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001052 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001053 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001054
Yan Zheng17d217f2008-12-12 10:03:38 -05001055 if (root->root_key.objectid ==
1056 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1057 ret = btrfs_reloc_clone_csums(inode, start,
1058 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001059 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001060 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001061 }
1062
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001063 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001064
Chris Masond3977122009-01-05 21:25:51 -05001065 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001066 break;
Chris Masond3977122009-01-05 21:25:51 -05001067
Chris Masonc8b97812008-10-29 14:49:59 -04001068 /* we're not doing compressed IO, don't unlock the first
1069 * page (which the caller expects to stay locked), don't
1070 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001071 *
1072 * Do set the Private2 bit so we know this page was properly
1073 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001074 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001075 op = unlock ? PAGE_UNLOCK : 0;
1076 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001077
Josef Bacikc2790a22013-07-29 11:20:47 -04001078 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001079 start + ram_size - 1,
1080 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001081 EXTENT_LOCKED | EXTENT_DELALLOC,
1082 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001083 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001084 num_bytes -= cur_alloc_size;
1085 alloc_hint = ins.objectid + ins.offset;
1086 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -04001087 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001088out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001089 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001090
Filipe Mananad9f85962014-08-25 10:43:00 +01001091out_drop_extent_cache:
1092 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001093out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001094 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001095 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001096out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001097 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1098 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001099 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1100 EXTENT_DELALLOC | EXTENT_DEFRAG,
1101 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1102 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001103 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001104}
Chris Masonc8b97812008-10-29 14:49:59 -04001105
Chris Mason771ed682008-11-06 22:02:51 -05001106/*
1107 * work queue call back to started compression on a file and pages
1108 */
1109static noinline void async_cow_start(struct btrfs_work *work)
1110{
1111 struct async_cow *async_cow;
1112 int num_added = 0;
1113 async_cow = container_of(work, struct async_cow, work);
1114
1115 compress_file_range(async_cow->inode, async_cow->locked_page,
1116 async_cow->start, async_cow->end, async_cow,
1117 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001118 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001119 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001120 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001121 }
Chris Mason771ed682008-11-06 22:02:51 -05001122}
1123
1124/*
1125 * work queue call back to submit previously compressed pages
1126 */
1127static noinline void async_cow_submit(struct btrfs_work *work)
1128{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001129 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001130 struct async_cow *async_cow;
1131 struct btrfs_root *root;
1132 unsigned long nr_pages;
1133
1134 async_cow = container_of(work, struct async_cow, work);
1135
1136 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001137 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001138 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1139 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001140
David Sterbaee863952015-02-16 19:41:40 +01001141 /*
1142 * atomic_sub_return implies a barrier for waitqueue_active
1143 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001144 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001145 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001146 waitqueue_active(&fs_info->async_submit_wait))
1147 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001148
Chris Masond3977122009-01-05 21:25:51 -05001149 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001150 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001151}
Chris Masonc8b97812008-10-29 14:49:59 -04001152
Chris Mason771ed682008-11-06 22:02:51 -05001153static noinline void async_cow_free(struct btrfs_work *work)
1154{
1155 struct async_cow *async_cow;
1156 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001157 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001158 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001159 kfree(async_cow);
1160}
1161
1162static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1163 u64 start, u64 end, int *page_started,
1164 unsigned long *nr_written)
1165{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001166 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001167 struct async_cow *async_cow;
1168 struct btrfs_root *root = BTRFS_I(inode)->root;
1169 unsigned long nr_pages;
1170 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001171
Chris Masona3429ab2009-10-08 12:30:20 -04001172 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1173 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001174 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001175 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001176 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001177 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001178 async_cow->root = root;
1179 async_cow->locked_page = locked_page;
1180 async_cow->start = start;
1181
Wang Shilongf79707b2014-07-17 11:44:09 +08001182 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001183 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001184 cur_end = end;
1185 else
Byongho Leeee221842015-12-15 01:42:10 +09001186 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001187
1188 async_cow->end = cur_end;
1189 INIT_LIST_HEAD(&async_cow->extents);
1190
Liu Bo9e0af232014-08-15 23:36:53 +08001191 btrfs_init_work(&async_cow->work,
1192 btrfs_delalloc_helper,
1193 async_cow_start, async_cow_submit,
1194 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001195
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001196 nr_pages = (cur_end - start + PAGE_SIZE) >>
1197 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001198 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001199
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001200 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001201
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001202 while (atomic_read(&fs_info->async_submit_draining) &&
1203 atomic_read(&fs_info->async_delalloc_pages)) {
1204 wait_event(fs_info->async_submit_wait,
1205 (atomic_read(&fs_info->async_delalloc_pages) ==
1206 0));
Chris Mason771ed682008-11-06 22:02:51 -05001207 }
1208
1209 *nr_written += nr_pages;
1210 start = cur_end + 1;
1211 }
1212 *page_started = 1;
1213 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001214}
1215
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001216static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001217 u64 bytenr, u64 num_bytes)
1218{
1219 int ret;
1220 struct btrfs_ordered_sum *sums;
1221 LIST_HEAD(list);
1222
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001223 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001224 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001225 if (ret == 0 && list_empty(&list))
1226 return 0;
1227
1228 while (!list_empty(&list)) {
1229 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1230 list_del(&sums->list);
1231 kfree(sums);
1232 }
1233 return 1;
1234}
1235
Chris Masond352ac62008-09-29 15:18:18 -04001236/*
1237 * when nowcow writeback call back. This checks for snapshots or COW copies
1238 * of the extents that exist in the file, and COWs the file as required.
1239 *
1240 * If no cow copies or snapshots exist, we write directly to the existing
1241 * blocks on disk
1242 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001243static noinline int run_delalloc_nocow(struct inode *inode,
1244 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001245 u64 start, u64 end, int *page_started, int force,
1246 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001249 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001250 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001251 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001252 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001254 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001255 u64 cow_start;
1256 u64 cur_offset;
1257 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001258 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 u64 disk_bytenr;
1260 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001261 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001262 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001264 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001265 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 int nocow;
1267 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001268 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001269 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001270
1271 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001272 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001273 extent_clear_unlock_delalloc(inode, start, end, end,
1274 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001275 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001276 EXTENT_DO_ACCOUNTING |
1277 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001278 PAGE_CLEAR_DIRTY |
1279 PAGE_SET_WRITEBACK |
1280 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001281 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001282 }
Li Zefan82d59022011-04-20 10:33:24 +08001283
Liu Bo83eea1f2012-07-10 05:28:39 -06001284 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001285
1286 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001287 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001288 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001289 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001290
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001291 if (IS_ERR(trans)) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001292 extent_clear_unlock_delalloc(inode, start, end, end,
1293 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001294 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001295 EXTENT_DO_ACCOUNTING |
1296 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001297 PAGE_CLEAR_DIRTY |
1298 PAGE_SET_WRITEBACK |
1299 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001300 btrfs_free_path(path);
1301 return PTR_ERR(trans);
1302 }
1303
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001304 trans->block_rsv = &fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001305
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 cow_start = (u64)-1;
1307 cur_offset = start;
1308 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001309 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001311 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001312 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1314 leaf = path->nodes[0];
1315 btrfs_item_key_to_cpu(leaf, &found_key,
1316 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001317 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 found_key.type == BTRFS_EXTENT_DATA_KEY)
1319 path->slots[0]--;
1320 }
1321 check_prev = 0;
1322next_slot:
1323 leaf = path->nodes[0];
1324 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1325 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001326 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001327 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001328 if (ret > 0)
1329 break;
1330 leaf = path->nodes[0];
1331 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001332
Yan Zheng80ff3852008-10-30 14:20:02 -04001333 nocow = 0;
1334 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001335 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001337
Filipe Manana1d512cb2015-11-09 00:33:58 +00001338 if (found_key.objectid > ino)
1339 break;
1340 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1341 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1342 path->slots[0]++;
1343 goto next_slot;
1344 }
1345 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 found_key.offset > end)
1347 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001348
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 if (found_key.offset > cur_offset) {
1350 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001351 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001352 goto out_check;
1353 }
Chris Masonc31f8832008-01-08 15:46:31 -05001354
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 fi = btrfs_item_ptr(leaf, path->slots[0],
1356 struct btrfs_file_extent_item);
1357 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001358
Josef Bacikcc95bef2013-04-04 14:31:27 -04001359 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001360 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1361 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001363 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 extent_end = found_key.offset +
1365 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001366 disk_num_bytes =
1367 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 if (extent_end <= start) {
1369 path->slots[0]++;
1370 goto next_slot;
1371 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001372 if (disk_bytenr == 0)
1373 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001374 if (btrfs_file_extent_compression(leaf, fi) ||
1375 btrfs_file_extent_encryption(leaf, fi) ||
1376 btrfs_file_extent_other_encoding(leaf, fi))
1377 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001378 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1379 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001380 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001382 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001383 found_key.offset -
1384 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001385 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001386 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001387 disk_bytenr += cur_offset - found_key.offset;
1388 num_bytes = min(end + 1, extent_end) - cur_offset;
1389 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001390 * if there are pending snapshots for this root,
1391 * we fall into common COW way.
1392 */
1393 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001394 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001395 if (!err)
1396 goto out_check;
1397 }
1398 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001399 * force cow if csum exists in the range.
1400 * this ensure that csum for a given extent are
1401 * either valid or do not exist.
1402 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001403 if (csum_exist_in_range(fs_info, disk_bytenr,
1404 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001405 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001406 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001407 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 nocow = 1;
1409 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1410 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001411 btrfs_file_extent_inline_len(leaf,
1412 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001413 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001414 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001415 } else {
1416 BUG_ON(1);
1417 }
1418out_check:
1419 if (extent_end <= start) {
1420 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001421 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001422 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001423 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001424 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 goto next_slot;
1426 }
1427 if (!nocow) {
1428 if (cow_start == (u64)-1)
1429 cow_start = cur_offset;
1430 cur_offset = extent_end;
1431 if (cur_offset > end)
1432 break;
1433 path->slots[0]++;
1434 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001435 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001436
David Sterbab3b4aa72011-04-21 01:20:15 +02001437 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001438 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001439 ret = cow_file_range(inode, locked_page,
1440 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001441 end, page_started, nr_written, 1,
1442 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001443 if (ret) {
1444 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001445 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001446 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001447 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001448 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001449 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001450 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001451 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001452 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001453
Yan Zhengd899e052008-10-30 14:25:28 -04001454 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1455 struct extent_map *em;
1456 struct extent_map_tree *em_tree;
1457 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001458 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001459 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001460 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001461 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001462 em->len = num_bytes;
1463 em->block_len = num_bytes;
1464 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001465 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001466 em->ram_bytes = ram_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001467 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001468 em->mod_start = em->start;
1469 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001470 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001471 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001472 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001473 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001474 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001475 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001476 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001477 if (ret != -EEXIST) {
1478 free_extent_map(em);
1479 break;
1480 }
1481 btrfs_drop_extent_cache(inode, em->start,
1482 em->start + em->len - 1, 0);
1483 }
1484 type = BTRFS_ORDERED_PREALLOC;
1485 } else {
1486 type = BTRFS_ORDERED_NOCOW;
1487 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001488
1489 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001490 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001491 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001492 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001493 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001494
Yan, Zhengefa56462010-05-16 10:49:59 -04001495 if (root->root_key.objectid ==
1496 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1497 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1498 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001499 if (ret) {
1500 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001501 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001502 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001503 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001504 }
1505
Josef Bacikc2790a22013-07-29 11:20:47 -04001506 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001507 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001508 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001509 EXTENT_DELALLOC |
1510 EXTENT_CLEAR_DATA_RESV,
1511 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1512
Wang Shilonge9894fd2014-03-27 11:12:25 +08001513 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001514 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001515 cur_offset = extent_end;
1516 if (cur_offset > end)
1517 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001518 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001519 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001520
Josef Bacik17ca04a2012-05-31 15:58:55 -04001521 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001522 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001523 cur_offset = end;
1524 }
1525
Yan Zheng80ff3852008-10-30 14:20:02 -04001526 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001527 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1528 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001529 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001530 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001531 }
1532
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001533error:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001534 err = btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001535 if (!ret)
1536 ret = err;
1537
Josef Bacik17ca04a2012-05-31 15:58:55 -04001538 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001539 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001540 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001541 EXTENT_DELALLOC | EXTENT_DEFRAG |
1542 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1543 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001544 PAGE_SET_WRITEBACK |
1545 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001546 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001547 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001548}
1549
Wang Shilong47059d92014-07-03 18:22:07 +08001550static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1551{
1552
1553 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1554 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1555 return 0;
1556
1557 /*
1558 * @defrag_bytes is a hint value, no spinlock held here,
1559 * if is not zero, it means the file is defragging.
1560 * Force cow if given extent needs to be defragged.
1561 */
1562 if (BTRFS_I(inode)->defrag_bytes &&
1563 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1564 EXTENT_DEFRAG, 0, NULL))
1565 return 1;
1566
1567 return 0;
1568}
1569
Chris Masond352ac62008-09-29 15:18:18 -04001570/*
1571 * extent_io.c call back to do delayed allocation processing
1572 */
Chris Masonc8b97812008-10-29 14:49:59 -04001573static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001574 u64 start, u64 end, int *page_started,
1575 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001576{
Chris Masonbe20aa92007-12-17 20:14:01 -05001577 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001578 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001579
Wang Shilong47059d92014-07-03 18:22:07 +08001580 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001581 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001582 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001583 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001584 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001585 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001586 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001587 ret = cow_file_range(inode, locked_page, start, end, end,
1588 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001589 } else {
1590 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1591 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001592 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001593 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001594 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001595 return ret;
1596}
1597
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001598static void btrfs_split_extent_hook(struct inode *inode,
1599 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001600{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001601 u64 size;
1602
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001603 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001604 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001605 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001606
Josef Bacikdcab6a32015-02-11 15:08:59 -05001607 size = orig->end - orig->start + 1;
1608 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001609 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001610 u64 new_size;
1611
1612 /*
Josef Bacikba117212015-03-13 15:01:24 -04001613 * See the explanation in btrfs_merge_extent_hook, the same
1614 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001615 */
1616 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001617 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001618 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001619 num_extents += count_max_extents(new_size);
1620 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 return;
1622 }
1623
Josef Bacik9e0baf62011-07-15 15:16:44 +00001624 spin_lock(&BTRFS_I(inode)->lock);
1625 BTRFS_I(inode)->outstanding_extents++;
1626 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001627}
1628
1629/*
1630 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1631 * extents so we can keep track of new extents that are just merged onto old
1632 * extents, such as when we are doing sequential writes, so we can properly
1633 * account for the metadata space we'll need.
1634 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001635static void btrfs_merge_extent_hook(struct inode *inode,
1636 struct extent_state *new,
1637 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001638{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001639 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001640 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641
Josef Bacik9ed74f22009-09-11 16:12:44 -04001642 /* not delalloc, ignore it */
1643 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001644 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001645
Josef Bacik8461a3d2015-03-13 15:12:08 -04001646 if (new->start > other->start)
1647 new_size = new->end - other->start + 1;
1648 else
1649 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001650
1651 /* we're not bigger than the max, unreserve the space and go */
1652 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1653 spin_lock(&BTRFS_I(inode)->lock);
1654 BTRFS_I(inode)->outstanding_extents--;
1655 spin_unlock(&BTRFS_I(inode)->lock);
1656 return;
1657 }
1658
1659 /*
Josef Bacikba117212015-03-13 15:01:24 -04001660 * We have to add up either side to figure out how many extents were
1661 * accounted for before we merged into one big extent. If the number of
1662 * extents we accounted for is <= the amount we need for the new range
1663 * then we can return, otherwise drop. Think of it like this
1664 *
1665 * [ 4k][MAX_SIZE]
1666 *
1667 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1668 * need 2 outstanding extents, on one side we have 1 and the other side
1669 * we have 1 so they are == and we can return. But in this case
1670 *
1671 * [MAX_SIZE+4k][MAX_SIZE+4k]
1672 *
1673 * Each range on their own accounts for 2 extents, but merged together
1674 * they are only 3 extents worth of accounting, so we need to drop in
1675 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001676 */
Josef Bacikba117212015-03-13 15:01:24 -04001677 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001678 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001679 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001680 num_extents += count_max_extents(old_size);
1681 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001682 return;
1683
Josef Bacik9e0baf62011-07-15 15:16:44 +00001684 spin_lock(&BTRFS_I(inode)->lock);
1685 BTRFS_I(inode)->outstanding_extents--;
1686 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001687}
1688
Miao Xieeb73c1b2013-05-15 07:48:22 +00001689static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1690 struct inode *inode)
1691{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001692 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1693
Miao Xieeb73c1b2013-05-15 07:48:22 +00001694 spin_lock(&root->delalloc_lock);
1695 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1696 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1697 &root->delalloc_inodes);
1698 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1699 &BTRFS_I(inode)->runtime_flags);
1700 root->nr_delalloc_inodes++;
1701 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001702 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001703 BUG_ON(!list_empty(&root->delalloc_root));
1704 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001705 &fs_info->delalloc_roots);
1706 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001707 }
1708 }
1709 spin_unlock(&root->delalloc_lock);
1710}
1711
1712static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1713 struct inode *inode)
1714{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001715 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1716
Miao Xieeb73c1b2013-05-15 07:48:22 +00001717 spin_lock(&root->delalloc_lock);
1718 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1719 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1720 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1721 &BTRFS_I(inode)->runtime_flags);
1722 root->nr_delalloc_inodes--;
1723 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001724 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001725 BUG_ON(list_empty(&root->delalloc_root));
1726 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001727 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001728 }
1729 }
1730 spin_unlock(&root->delalloc_lock);
1731}
1732
Chris Masond352ac62008-09-29 15:18:18 -04001733/*
1734 * extent_io.c set_bit_hook, used to track delayed allocation
1735 * bytes in this file, and to maintain the list of inodes that
1736 * have pending delalloc work to be done.
1737 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001738static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001739 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001740{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001741
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001742 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1743
Wang Shilong47059d92014-07-03 18:22:07 +08001744 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1745 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001746 /*
1747 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001748 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001749 * bit, which is only set or cleared with irqs on
1750 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001751 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001752 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001753 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001754 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001755
Josef Bacik9e0baf62011-07-15 15:16:44 +00001756 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001757 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001758 } else {
1759 spin_lock(&BTRFS_I(inode)->lock);
1760 BTRFS_I(inode)->outstanding_extents++;
1761 spin_unlock(&BTRFS_I(inode)->lock);
1762 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001763
Josef Bacik6a3891c2015-03-16 17:38:52 -04001764 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001765 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001766 return;
1767
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001768 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1769 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001770 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001771 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001772 if (*bits & EXTENT_DEFRAG)
1773 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001774 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001775 &BTRFS_I(inode)->runtime_flags))
1776 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001777 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001778 }
Chris Mason291d6732008-01-29 15:55:23 -05001779}
1780
Chris Masond352ac62008-09-29 15:18:18 -04001781/*
1782 * extent_io.c clear_bit_hook, see set_bit_hook for why
1783 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001784static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001785 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001786 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001787{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001788 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001789 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001790 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001791
1792 spin_lock(&BTRFS_I(inode)->lock);
1793 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1794 BTRFS_I(inode)->defrag_bytes -= len;
1795 spin_unlock(&BTRFS_I(inode)->lock);
1796
Chris Mason75eff682008-12-15 15:54:40 -05001797 /*
1798 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001799 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001800 * bit, which is only set or cleared with irqs on
1801 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001802 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001803 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001804 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001805
Josef Bacik9e0baf62011-07-15 15:16:44 +00001806 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001807 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001808 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1809 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001810 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001811 spin_unlock(&BTRFS_I(inode)->lock);
1812 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001813
Josef Bacikb6d08f02013-09-27 14:57:43 -04001814 /*
1815 * We don't reserve metadata space for space cache inodes so we
1816 * don't need to call dellalloc_release_metadata if there is an
1817 * error.
1818 */
1819 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001820 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001821 btrfs_delalloc_release_metadata(inode, len);
1822
Josef Bacik6a3891c2015-03-16 17:38:52 -04001823 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001824 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001825 return;
1826
Josef Bacik0cb59c92010-07-02 12:14:14 -04001827 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001828 && do_list && !(state->state & EXTENT_NORESERVE)
1829 && (*bits & (EXTENT_DO_ACCOUNTING |
1830 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001831 btrfs_free_reserved_data_space_noquota(inode,
1832 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001833
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001834 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1835 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001836 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001837 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001838 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001839 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001840 &BTRFS_I(inode)->runtime_flags))
1841 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001842 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001843 }
Chris Mason291d6732008-01-29 15:55:23 -05001844}
1845
Chris Masond352ac62008-09-29 15:18:18 -04001846/*
1847 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1848 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001849 *
1850 * return 1 if page cannot be merged to bio
1851 * return 0 if page can be merged to bio
1852 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001853 */
Mike Christie81a75f672016-06-05 14:31:54 -05001854int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001855 size_t size, struct bio *bio,
1856 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001857{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001858 struct inode *inode = page->mapping->host;
1859 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001860 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001861 u64 length = 0;
1862 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001863 int ret;
1864
Chris Mason771ed682008-11-06 22:02:51 -05001865 if (bio_flags & EXTENT_BIO_COMPRESSED)
1866 return 0;
1867
Kent Overstreet4f024f32013-10-11 15:44:27 -07001868 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001869 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001870 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1871 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001872 if (ret < 0)
1873 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001874 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001875 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001876 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001877}
1878
Chris Masond352ac62008-09-29 15:18:18 -04001879/*
1880 * in order to insert checksums into the metadata in large chunks,
1881 * we wait until bio submission time. All the pages in the bio are
1882 * checksummed and sums are attached onto the ordered extent record.
1883 *
1884 * At IO completion time the cums attached on the ordered extent record
1885 * are inserted into the btree
1886 */
Mike Christie81a75f672016-06-05 14:31:54 -05001887static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1888 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001889 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001890{
Chris Mason065631f2008-02-20 12:07:25 -05001891 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001892
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001893 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001894 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001895 return 0;
1896}
Chris Masone0156402008-04-16 11:15:20 -04001897
Chris Mason4a69a412008-11-06 22:03:00 -05001898/*
1899 * in order to insert checksums into the metadata in large chunks,
1900 * we wait until bio submission time. All the pages in the bio are
1901 * checksummed and sums are attached onto the ordered extent record.
1902 *
1903 * At IO completion time the cums attached on the ordered extent record
1904 * are inserted into the btree
1905 */
Mike Christie81a75f672016-06-05 14:31:54 -05001906static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001907 int mirror_num, unsigned long bio_flags,
1908 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001909{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001910 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001911 int ret;
1912
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001913 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001914 if (ret) {
1915 bio->bi_error = ret;
1916 bio_endio(bio);
1917 }
Stefan Behrens61891922012-11-05 18:51:52 +01001918 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001919}
1920
Chris Masond352ac62008-09-29 15:18:18 -04001921/*
Chris Masoncad321a2008-12-17 14:51:42 -05001922 * extent_io.c submission hook. This does the right thing for csum calculation
1923 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001924 */
Mike Christie81a75f672016-06-05 14:31:54 -05001925static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001926 int mirror_num, unsigned long bio_flags,
1927 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001928{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001929 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001930 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301931 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001932 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001933 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001934 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001935
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001936 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001937
Liu Bo83eea1f2012-07-10 05:28:39 -06001938 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301939 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001940
Mike Christie37226b22016-06-05 14:31:52 -05001941 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001942 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001943 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001944 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001945
Chris Masond20f7042008-12-08 16:58:54 -05001946 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001947 ret = btrfs_submit_compressed_read(inode, bio,
1948 mirror_num,
1949 bio_flags);
1950 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001951 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001952 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001953 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001954 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001955 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001956 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001957 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001958 /* csum items have already been cloned */
1959 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1960 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001961 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001962 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1963 bio_flags, bio_offset,
1964 __btrfs_submit_bio_start,
1965 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001966 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001967 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001968 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001969 if (ret)
1970 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001971 }
1972
Chris Mason0b86a832008-03-24 15:01:56 -04001973mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001974 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001975
1976out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001977 if (ret < 0) {
1978 bio->bi_error = ret;
1979 bio_endio(bio);
1980 }
Stefan Behrens61891922012-11-05 18:51:52 +01001981 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001982}
Chris Mason6885f302008-02-20 16:11:05 -05001983
Chris Masond352ac62008-09-29 15:18:18 -04001984/*
1985 * given a list of ordered sums record them in the inode. This happens
1986 * at IO completion time based on sums calculated at bio submission time.
1987 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001988static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001989 struct inode *inode, u64 file_offset,
1990 struct list_head *list)
1991{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001992 struct btrfs_ordered_sum *sum;
1993
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001994 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001995 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001996 btrfs_csum_file_blocks(trans,
1997 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001998 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001999 }
2000 return 0;
2001}
2002
Josef Bacik2ac55d42010-02-03 19:33:23 +00002003int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002004 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002005{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002006 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002007 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002008 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002009}
2010
Chris Masond352ac62008-09-29 15:18:18 -04002011/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002012struct btrfs_writepage_fixup {
2013 struct page *page;
2014 struct btrfs_work work;
2015};
2016
Christoph Hellwigb2950862008-12-02 09:54:17 -05002017static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002018{
2019 struct btrfs_writepage_fixup *fixup;
2020 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002021 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002022 struct page *page;
2023 struct inode *inode;
2024 u64 page_start;
2025 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002026 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002027
2028 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2029 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002030again:
Chris Mason247e7432008-07-17 12:53:51 -04002031 lock_page(page);
2032 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2033 ClearPageChecked(page);
2034 goto out_page;
2035 }
2036
2037 inode = page->mapping->host;
2038 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002039 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002040
David Sterbaff13db42015-12-03 14:30:40 +01002041 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002042 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002043
2044 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002045 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002046 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002047
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302048 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002049 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002050 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002051 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2052 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002053 unlock_page(page);
2054 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002055 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002056 goto again;
2057 }
Chris Mason247e7432008-07-17 12:53:51 -04002058
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002059 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002060 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002061 if (ret) {
2062 mapping_set_error(page->mapping, ret);
2063 end_extent_writepage(page, ret, page_start, page_end);
2064 ClearPageChecked(page);
2065 goto out;
2066 }
2067
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002068 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2069 0);
Chris Mason247e7432008-07-17 12:53:51 -04002070 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002071 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002072out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002073 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2074 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002075out_page:
2076 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002077 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002078 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002079}
2080
2081/*
2082 * There are a few paths in the higher layers of the kernel that directly
2083 * set the page dirty bit without asking the filesystem if it is a
2084 * good idea. This causes problems because we want to make sure COW
2085 * properly happens and the data=ordered rules are followed.
2086 *
Chris Masonc8b97812008-10-29 14:49:59 -04002087 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002088 * hasn't been properly setup for IO. We kick off an async process
2089 * to fix it up. The async helper will wait for ordered extents, set
2090 * the delalloc bit and make it safe to write the page.
2091 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002092static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002093{
2094 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002095 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002096 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002097
Chris Mason8b62b722009-09-02 16:53:46 -04002098 /* this page is properly in the ordered list */
2099 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002100 return 0;
2101
2102 if (PageChecked(page))
2103 return -EAGAIN;
2104
2105 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2106 if (!fixup)
2107 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002108
Chris Mason247e7432008-07-17 12:53:51 -04002109 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002110 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002111 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2112 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002113 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002114 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002115 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002116}
2117
Yan Zhengd899e052008-10-30 14:25:28 -04002118static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2119 struct inode *inode, u64 file_pos,
2120 u64 disk_bytenr, u64 disk_num_bytes,
2121 u64 num_bytes, u64 ram_bytes,
2122 u8 compression, u8 encryption,
2123 u16 other_encoding, int extent_type)
2124{
2125 struct btrfs_root *root = BTRFS_I(inode)->root;
2126 struct btrfs_file_extent_item *fi;
2127 struct btrfs_path *path;
2128 struct extent_buffer *leaf;
2129 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002130 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002131 int ret;
2132
2133 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002134 if (!path)
2135 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002136
Chris Masona1ed8352009-09-11 12:27:37 -04002137 /*
2138 * we may be replacing one extent in the tree with another.
2139 * The new extent is pinned in the extent map, and we don't want
2140 * to drop it from the cache until it is completely in the btree.
2141 *
2142 * So, tell btrfs_drop_extents to leave this extent in the cache.
2143 * the caller is expected to unpin it and allow it to be merged
2144 * with the others.
2145 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002146 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2147 file_pos + num_bytes, NULL, 0,
2148 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002149 if (ret)
2150 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002151
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002152 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002153 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002154 ins.offset = file_pos;
2155 ins.type = BTRFS_EXTENT_DATA_KEY;
2156
2157 path->leave_spinning = 1;
2158 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2159 sizeof(*fi));
2160 if (ret)
2161 goto out;
2162 }
Yan Zhengd899e052008-10-30 14:25:28 -04002163 leaf = path->nodes[0];
2164 fi = btrfs_item_ptr(leaf, path->slots[0],
2165 struct btrfs_file_extent_item);
2166 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2167 btrfs_set_file_extent_type(leaf, fi, extent_type);
2168 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2169 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2170 btrfs_set_file_extent_offset(leaf, fi, 0);
2171 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2172 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2173 btrfs_set_file_extent_compression(leaf, fi, compression);
2174 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2175 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002176
Yan Zhengd899e052008-10-30 14:25:28 -04002177 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002178 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002179
2180 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002181
2182 ins.objectid = disk_bytenr;
2183 ins.offset = disk_num_bytes;
2184 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002185 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002186 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002187 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002188 * Release the reserved range from inode dirty range map, as it is
2189 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002190 */
2191 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002192out:
Yan Zhengd899e052008-10-30 14:25:28 -04002193 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002194
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002195 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002196}
2197
Liu Bo38c227d2013-01-29 03:18:40 +00002198/* snapshot-aware defrag */
2199struct sa_defrag_extent_backref {
2200 struct rb_node node;
2201 struct old_sa_defrag_extent *old;
2202 u64 root_id;
2203 u64 inum;
2204 u64 file_pos;
2205 u64 extent_offset;
2206 u64 num_bytes;
2207 u64 generation;
2208};
2209
2210struct old_sa_defrag_extent {
2211 struct list_head list;
2212 struct new_sa_defrag_extent *new;
2213
2214 u64 extent_offset;
2215 u64 bytenr;
2216 u64 offset;
2217 u64 len;
2218 int count;
2219};
2220
2221struct new_sa_defrag_extent {
2222 struct rb_root root;
2223 struct list_head head;
2224 struct btrfs_path *path;
2225 struct inode *inode;
2226 u64 file_pos;
2227 u64 len;
2228 u64 bytenr;
2229 u64 disk_len;
2230 u8 compress_type;
2231};
2232
2233static int backref_comp(struct sa_defrag_extent_backref *b1,
2234 struct sa_defrag_extent_backref *b2)
2235{
2236 if (b1->root_id < b2->root_id)
2237 return -1;
2238 else if (b1->root_id > b2->root_id)
2239 return 1;
2240
2241 if (b1->inum < b2->inum)
2242 return -1;
2243 else if (b1->inum > b2->inum)
2244 return 1;
2245
2246 if (b1->file_pos < b2->file_pos)
2247 return -1;
2248 else if (b1->file_pos > b2->file_pos)
2249 return 1;
2250
2251 /*
2252 * [------------------------------] ===> (a range of space)
2253 * |<--->| |<---->| =============> (fs/file tree A)
2254 * |<---------------------------->| ===> (fs/file tree B)
2255 *
2256 * A range of space can refer to two file extents in one tree while
2257 * refer to only one file extent in another tree.
2258 *
2259 * So we may process a disk offset more than one time(two extents in A)
2260 * and locate at the same extent(one extent in B), then insert two same
2261 * backrefs(both refer to the extent in B).
2262 */
2263 return 0;
2264}
2265
2266static void backref_insert(struct rb_root *root,
2267 struct sa_defrag_extent_backref *backref)
2268{
2269 struct rb_node **p = &root->rb_node;
2270 struct rb_node *parent = NULL;
2271 struct sa_defrag_extent_backref *entry;
2272 int ret;
2273
2274 while (*p) {
2275 parent = *p;
2276 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2277
2278 ret = backref_comp(backref, entry);
2279 if (ret < 0)
2280 p = &(*p)->rb_left;
2281 else
2282 p = &(*p)->rb_right;
2283 }
2284
2285 rb_link_node(&backref->node, parent, p);
2286 rb_insert_color(&backref->node, root);
2287}
2288
2289/*
2290 * Note the backref might has changed, and in this case we just return 0.
2291 */
2292static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2293 void *ctx)
2294{
2295 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002296 struct old_sa_defrag_extent *old = ctx;
2297 struct new_sa_defrag_extent *new = old->new;
2298 struct btrfs_path *path = new->path;
2299 struct btrfs_key key;
2300 struct btrfs_root *root;
2301 struct sa_defrag_extent_backref *backref;
2302 struct extent_buffer *leaf;
2303 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002304 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002305 int slot;
2306 int ret;
2307 u64 extent_offset;
2308 u64 num_bytes;
2309
2310 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002311 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002312 return 0;
2313
2314 key.objectid = root_id;
2315 key.type = BTRFS_ROOT_ITEM_KEY;
2316 key.offset = (u64)-1;
2317
Liu Bo38c227d2013-01-29 03:18:40 +00002318 root = btrfs_read_fs_root_no_name(fs_info, &key);
2319 if (IS_ERR(root)) {
2320 if (PTR_ERR(root) == -ENOENT)
2321 return 0;
2322 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002323 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002324 inum, offset, root_id);
2325 return PTR_ERR(root);
2326 }
2327
2328 key.objectid = inum;
2329 key.type = BTRFS_EXTENT_DATA_KEY;
2330 if (offset > (u64)-1 << 32)
2331 key.offset = 0;
2332 else
2333 key.offset = offset;
2334
2335 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302336 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002337 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002338 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002339
2340 while (1) {
2341 cond_resched();
2342
2343 leaf = path->nodes[0];
2344 slot = path->slots[0];
2345
2346 if (slot >= btrfs_header_nritems(leaf)) {
2347 ret = btrfs_next_leaf(root, path);
2348 if (ret < 0) {
2349 goto out;
2350 } else if (ret > 0) {
2351 ret = 0;
2352 goto out;
2353 }
2354 continue;
2355 }
2356
2357 path->slots[0]++;
2358
2359 btrfs_item_key_to_cpu(leaf, &key, slot);
2360
2361 if (key.objectid > inum)
2362 goto out;
2363
2364 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2365 continue;
2366
2367 extent = btrfs_item_ptr(leaf, slot,
2368 struct btrfs_file_extent_item);
2369
2370 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2371 continue;
2372
Liu Boe68afa42013-07-01 22:13:26 +08002373 /*
2374 * 'offset' refers to the exact key.offset,
2375 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2376 * (key.offset - extent_offset).
2377 */
2378 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002379 continue;
2380
Liu Boe68afa42013-07-01 22:13:26 +08002381 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002382 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002383
Liu Bo38c227d2013-01-29 03:18:40 +00002384 if (extent_offset >= old->extent_offset + old->offset +
2385 old->len || extent_offset + num_bytes <=
2386 old->extent_offset + old->offset)
2387 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002388 break;
2389 }
2390
2391 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2392 if (!backref) {
2393 ret = -ENOENT;
2394 goto out;
2395 }
2396
2397 backref->root_id = root_id;
2398 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002399 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002400 backref->num_bytes = num_bytes;
2401 backref->extent_offset = extent_offset;
2402 backref->generation = btrfs_file_extent_generation(leaf, extent);
2403 backref->old = old;
2404 backref_insert(&new->root, backref);
2405 old->count++;
2406out:
2407 btrfs_release_path(path);
2408 WARN_ON(ret);
2409 return ret;
2410}
2411
2412static noinline bool record_extent_backrefs(struct btrfs_path *path,
2413 struct new_sa_defrag_extent *new)
2414{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002415 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002416 struct old_sa_defrag_extent *old, *tmp;
2417 int ret;
2418
2419 new->path = path;
2420
2421 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002422 ret = iterate_inodes_from_logical(old->bytenr +
2423 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002424 path, record_one_backref,
2425 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002426 if (ret < 0 && ret != -ENOENT)
2427 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002428
2429 /* no backref to be processed for this extent */
2430 if (!old->count) {
2431 list_del(&old->list);
2432 kfree(old);
2433 }
2434 }
2435
2436 if (list_empty(&new->head))
2437 return false;
2438
2439 return true;
2440}
2441
2442static int relink_is_mergable(struct extent_buffer *leaf,
2443 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002444 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002445{
Liu Bo116e0022013-08-02 16:30:40 +08002446 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002447 return 0;
2448
2449 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2450 return 0;
2451
Liu Bo116e0022013-08-02 16:30:40 +08002452 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2453 return 0;
2454
2455 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002456 btrfs_file_extent_other_encoding(leaf, fi))
2457 return 0;
2458
2459 return 1;
2460}
2461
2462/*
2463 * Note the backref might has changed, and in this case we just return 0.
2464 */
2465static noinline int relink_extent_backref(struct btrfs_path *path,
2466 struct sa_defrag_extent_backref *prev,
2467 struct sa_defrag_extent_backref *backref)
2468{
2469 struct btrfs_file_extent_item *extent;
2470 struct btrfs_file_extent_item *item;
2471 struct btrfs_ordered_extent *ordered;
2472 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002473 struct btrfs_root *root;
2474 struct btrfs_key key;
2475 struct extent_buffer *leaf;
2476 struct old_sa_defrag_extent *old = backref->old;
2477 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002478 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002479 struct inode *inode;
2480 struct extent_state *cached = NULL;
2481 int ret = 0;
2482 u64 start;
2483 u64 len;
2484 u64 lock_start;
2485 u64 lock_end;
2486 bool merge = false;
2487 int index;
2488
2489 if (prev && prev->root_id == backref->root_id &&
2490 prev->inum == backref->inum &&
2491 prev->file_pos + prev->num_bytes == backref->file_pos)
2492 merge = true;
2493
2494 /* step 1: get root */
2495 key.objectid = backref->root_id;
2496 key.type = BTRFS_ROOT_ITEM_KEY;
2497 key.offset = (u64)-1;
2498
Liu Bo38c227d2013-01-29 03:18:40 +00002499 index = srcu_read_lock(&fs_info->subvol_srcu);
2500
2501 root = btrfs_read_fs_root_no_name(fs_info, &key);
2502 if (IS_ERR(root)) {
2503 srcu_read_unlock(&fs_info->subvol_srcu, index);
2504 if (PTR_ERR(root) == -ENOENT)
2505 return 0;
2506 return PTR_ERR(root);
2507 }
Liu Bo38c227d2013-01-29 03:18:40 +00002508
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002509 if (btrfs_root_readonly(root)) {
2510 srcu_read_unlock(&fs_info->subvol_srcu, index);
2511 return 0;
2512 }
2513
Liu Bo38c227d2013-01-29 03:18:40 +00002514 /* step 2: get inode */
2515 key.objectid = backref->inum;
2516 key.type = BTRFS_INODE_ITEM_KEY;
2517 key.offset = 0;
2518
2519 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2520 if (IS_ERR(inode)) {
2521 srcu_read_unlock(&fs_info->subvol_srcu, index);
2522 return 0;
2523 }
2524
2525 srcu_read_unlock(&fs_info->subvol_srcu, index);
2526
2527 /* step 3: relink backref */
2528 lock_start = backref->file_pos;
2529 lock_end = backref->file_pos + backref->num_bytes - 1;
2530 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002531 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002532
2533 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2534 if (ordered) {
2535 btrfs_put_ordered_extent(ordered);
2536 goto out_unlock;
2537 }
2538
2539 trans = btrfs_join_transaction(root);
2540 if (IS_ERR(trans)) {
2541 ret = PTR_ERR(trans);
2542 goto out_unlock;
2543 }
2544
2545 key.objectid = backref->inum;
2546 key.type = BTRFS_EXTENT_DATA_KEY;
2547 key.offset = backref->file_pos;
2548
2549 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2550 if (ret < 0) {
2551 goto out_free_path;
2552 } else if (ret > 0) {
2553 ret = 0;
2554 goto out_free_path;
2555 }
2556
2557 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2558 struct btrfs_file_extent_item);
2559
2560 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2561 backref->generation)
2562 goto out_free_path;
2563
2564 btrfs_release_path(path);
2565
2566 start = backref->file_pos;
2567 if (backref->extent_offset < old->extent_offset + old->offset)
2568 start += old->extent_offset + old->offset -
2569 backref->extent_offset;
2570
2571 len = min(backref->extent_offset + backref->num_bytes,
2572 old->extent_offset + old->offset + old->len);
2573 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2574
2575 ret = btrfs_drop_extents(trans, root, inode, start,
2576 start + len, 1);
2577 if (ret)
2578 goto out_free_path;
2579again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002580 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002581 key.type = BTRFS_EXTENT_DATA_KEY;
2582 key.offset = start;
2583
Liu Boa09a0a72013-03-11 09:20:58 +00002584 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002585 if (merge) {
2586 struct btrfs_file_extent_item *fi;
2587 u64 extent_len;
2588 struct btrfs_key found_key;
2589
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002590 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002591 if (ret < 0)
2592 goto out_free_path;
2593
2594 path->slots[0]--;
2595 leaf = path->nodes[0];
2596 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2597
2598 fi = btrfs_item_ptr(leaf, path->slots[0],
2599 struct btrfs_file_extent_item);
2600 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2601
Liu Bo116e0022013-08-02 16:30:40 +08002602 if (extent_len + found_key.offset == start &&
2603 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002604 btrfs_set_file_extent_num_bytes(leaf, fi,
2605 extent_len + len);
2606 btrfs_mark_buffer_dirty(leaf);
2607 inode_add_bytes(inode, len);
2608
2609 ret = 1;
2610 goto out_free_path;
2611 } else {
2612 merge = false;
2613 btrfs_release_path(path);
2614 goto again;
2615 }
2616 }
2617
2618 ret = btrfs_insert_empty_item(trans, root, path, &key,
2619 sizeof(*extent));
2620 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002621 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002622 goto out_free_path;
2623 }
2624
2625 leaf = path->nodes[0];
2626 item = btrfs_item_ptr(leaf, path->slots[0],
2627 struct btrfs_file_extent_item);
2628 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2629 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2630 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2631 btrfs_set_file_extent_num_bytes(leaf, item, len);
2632 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2633 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2634 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2635 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2636 btrfs_set_file_extent_encryption(leaf, item, 0);
2637 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2638
2639 btrfs_mark_buffer_dirty(leaf);
2640 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002641 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002642
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002643 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002644 new->disk_len, 0,
2645 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002646 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002647 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002648 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002649 goto out_free_path;
2650 }
2651
2652 ret = 1;
2653out_free_path:
2654 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002655 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002656 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002657out_unlock:
2658 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2659 &cached, GFP_NOFS);
2660 iput(inode);
2661 return ret;
2662}
2663
Liu Bo6f519562013-10-29 10:45:05 +08002664static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2665{
2666 struct old_sa_defrag_extent *old, *tmp;
2667
2668 if (!new)
2669 return;
2670
2671 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002672 kfree(old);
2673 }
2674 kfree(new);
2675}
2676
Liu Bo38c227d2013-01-29 03:18:40 +00002677static void relink_file_extents(struct new_sa_defrag_extent *new)
2678{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002679 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002680 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002681 struct sa_defrag_extent_backref *backref;
2682 struct sa_defrag_extent_backref *prev = NULL;
2683 struct inode *inode;
2684 struct btrfs_root *root;
2685 struct rb_node *node;
2686 int ret;
2687
2688 inode = new->inode;
2689 root = BTRFS_I(inode)->root;
2690
2691 path = btrfs_alloc_path();
2692 if (!path)
2693 return;
2694
2695 if (!record_extent_backrefs(path, new)) {
2696 btrfs_free_path(path);
2697 goto out;
2698 }
2699 btrfs_release_path(path);
2700
2701 while (1) {
2702 node = rb_first(&new->root);
2703 if (!node)
2704 break;
2705 rb_erase(node, &new->root);
2706
2707 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2708
2709 ret = relink_extent_backref(path, prev, backref);
2710 WARN_ON(ret < 0);
2711
2712 kfree(prev);
2713
2714 if (ret == 1)
2715 prev = backref;
2716 else
2717 prev = NULL;
2718 cond_resched();
2719 }
2720 kfree(prev);
2721
2722 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002723out:
Liu Bo6f519562013-10-29 10:45:05 +08002724 free_sa_defrag_extent(new);
2725
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002726 atomic_dec(&fs_info->defrag_running);
2727 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002728}
2729
2730static struct new_sa_defrag_extent *
2731record_old_file_extents(struct inode *inode,
2732 struct btrfs_ordered_extent *ordered)
2733{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002734 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002735 struct btrfs_root *root = BTRFS_I(inode)->root;
2736 struct btrfs_path *path;
2737 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002738 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002739 struct new_sa_defrag_extent *new;
2740 int ret;
2741
2742 new = kmalloc(sizeof(*new), GFP_NOFS);
2743 if (!new)
2744 return NULL;
2745
2746 new->inode = inode;
2747 new->file_pos = ordered->file_offset;
2748 new->len = ordered->len;
2749 new->bytenr = ordered->start;
2750 new->disk_len = ordered->disk_len;
2751 new->compress_type = ordered->compress_type;
2752 new->root = RB_ROOT;
2753 INIT_LIST_HEAD(&new->head);
2754
2755 path = btrfs_alloc_path();
2756 if (!path)
2757 goto out_kfree;
2758
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002759 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002760 key.type = BTRFS_EXTENT_DATA_KEY;
2761 key.offset = new->file_pos;
2762
2763 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2764 if (ret < 0)
2765 goto out_free_path;
2766 if (ret > 0 && path->slots[0] > 0)
2767 path->slots[0]--;
2768
2769 /* find out all the old extents for the file range */
2770 while (1) {
2771 struct btrfs_file_extent_item *extent;
2772 struct extent_buffer *l;
2773 int slot;
2774 u64 num_bytes;
2775 u64 offset;
2776 u64 end;
2777 u64 disk_bytenr;
2778 u64 extent_offset;
2779
2780 l = path->nodes[0];
2781 slot = path->slots[0];
2782
2783 if (slot >= btrfs_header_nritems(l)) {
2784 ret = btrfs_next_leaf(root, path);
2785 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002786 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002787 else if (ret > 0)
2788 break;
2789 continue;
2790 }
2791
2792 btrfs_item_key_to_cpu(l, &key, slot);
2793
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002794 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002795 break;
2796 if (key.type != BTRFS_EXTENT_DATA_KEY)
2797 break;
2798 if (key.offset >= new->file_pos + new->len)
2799 break;
2800
2801 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2802
2803 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2804 if (key.offset + num_bytes < new->file_pos)
2805 goto next;
2806
2807 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2808 if (!disk_bytenr)
2809 goto next;
2810
2811 extent_offset = btrfs_file_extent_offset(l, extent);
2812
2813 old = kmalloc(sizeof(*old), GFP_NOFS);
2814 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002815 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002816
2817 offset = max(new->file_pos, key.offset);
2818 end = min(new->file_pos + new->len, key.offset + num_bytes);
2819
2820 old->bytenr = disk_bytenr;
2821 old->extent_offset = extent_offset;
2822 old->offset = offset - key.offset;
2823 old->len = end - offset;
2824 old->new = new;
2825 old->count = 0;
2826 list_add_tail(&old->list, &new->head);
2827next:
2828 path->slots[0]++;
2829 cond_resched();
2830 }
2831
2832 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002833 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002834
2835 return new;
2836
Liu Bo38c227d2013-01-29 03:18:40 +00002837out_free_path:
2838 btrfs_free_path(path);
2839out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002840 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002841 return NULL;
2842}
2843
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002844static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002845 u64 start, u64 len)
2846{
2847 struct btrfs_block_group_cache *cache;
2848
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002849 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002850 ASSERT(cache);
2851
2852 spin_lock(&cache->lock);
2853 cache->delalloc_bytes -= len;
2854 spin_unlock(&cache->lock);
2855
2856 btrfs_put_block_group(cache);
2857}
2858
Chris Masond352ac62008-09-29 15:18:18 -04002859/* as ordered data IO finishes, this gets called so we can finish
2860 * an ordered extent if the range of bytes in the file it covers are
2861 * fully written.
2862 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002863static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002864{
Josef Bacik5fd02042012-05-02 14:00:54 -04002865 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002866 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002867 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002868 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002869 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002870 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002871 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002872 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002873 int ret = 0;
2874 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002875 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002876 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002877
Liu Bo83eea1f2012-07-10 05:28:39 -06002878 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002879
Josef Bacik5fd02042012-05-02 14:00:54 -04002880 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2881 ret = -EIO;
2882 goto out;
2883 }
2884
Miao Xief6124962014-09-12 18:44:04 +08002885 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2886 ordered_extent->file_offset +
2887 ordered_extent->len - 1);
2888
Josef Bacik77cef2e2013-08-29 13:57:21 -04002889 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2890 truncated = true;
2891 logical_len = ordered_extent->truncated_len;
2892 /* Truncated the entire extent, don't bother adding */
2893 if (!logical_len)
2894 goto out;
2895 }
2896
Yan, Zhengc2167752009-11-12 09:34:21 +00002897 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002898 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002899
2900 /*
2901 * For mwrite(mmap + memset to write) case, we still reserve
2902 * space for NOCOW range.
2903 * As NOCOW won't cause a new delayed ref, just free the space
2904 */
2905 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2906 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002907 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2908 if (nolock)
2909 trans = btrfs_join_transaction_nolock(root);
2910 else
2911 trans = btrfs_join_transaction(root);
2912 if (IS_ERR(trans)) {
2913 ret = PTR_ERR(trans);
2914 trans = NULL;
2915 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002916 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002917 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002918 ret = btrfs_update_inode_fallback(trans, root, inode);
2919 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002920 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002921 goto out;
2922 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002923
Josef Bacik2ac55d42010-02-03 19:33:23 +00002924 lock_extent_bits(io_tree, ordered_extent->file_offset,
2925 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002926 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002927
Liu Bo38c227d2013-01-29 03:18:40 +00002928 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2929 ordered_extent->file_offset + ordered_extent->len - 1,
2930 EXTENT_DEFRAG, 1, cached_state);
2931 if (ret) {
2932 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002933 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002934 /* the inode is shared */
2935 new = record_old_file_extents(inode, ordered_extent);
2936
2937 clear_extent_bit(io_tree, ordered_extent->file_offset,
2938 ordered_extent->file_offset + ordered_extent->len - 1,
2939 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2940 }
2941
Josef Bacik0cb59c92010-07-02 12:14:14 -04002942 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002943 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002944 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002945 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002946 if (IS_ERR(trans)) {
2947 ret = PTR_ERR(trans);
2948 trans = NULL;
2949 goto out_unlock;
2950 }
Chris Masona79b7d42014-05-22 16:18:52 -07002951
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002952 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002953
Chris Masonc8b97812008-10-29 14:49:59 -04002954 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002955 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002956 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002957 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002958 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002959 ordered_extent->file_offset,
2960 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002961 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002962 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002963 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002964 ret = insert_reserved_file_extent(trans, inode,
2965 ordered_extent->file_offset,
2966 ordered_extent->start,
2967 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002968 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002969 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002970 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002971 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002972 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002973 ordered_extent->start,
2974 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002975 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002976 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2977 ordered_extent->file_offset, ordered_extent->len,
2978 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002979 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002980 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002981 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002982 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002983
Chris Masone6dcd2d2008-07-17 12:53:50 -04002984 add_pending_csums(trans, inode, ordered_extent->file_offset,
2985 &ordered_extent->list);
2986
Josef Bacik6c760c02012-11-09 10:53:21 -05002987 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2988 ret = btrfs_update_inode_fallback(trans, root, inode);
2989 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002990 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002991 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002992 }
2993 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002994out_unlock:
2995 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2996 ordered_extent->file_offset +
2997 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002998out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002999 if (root != fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003000 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003001 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003002 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003003
Josef Bacik77cef2e2013-08-29 13:57:21 -04003004 if (ret || truncated) {
3005 u64 start, end;
3006
3007 if (truncated)
3008 start = ordered_extent->file_offset + logical_len;
3009 else
3010 start = ordered_extent->file_offset;
3011 end = ordered_extent->file_offset + ordered_extent->len - 1;
3012 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3013
3014 /* Drop the cache for the part of the extent we didn't write. */
3015 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003016
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003017 /*
3018 * If the ordered extent had an IOERR or something else went
3019 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003020 * back to the allocator. We only free the extent in the
3021 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003022 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003023 if ((ret || !logical_len) &&
3024 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003025 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003026 btrfs_free_reserved_extent(fs_info,
3027 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003028 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003029 }
3030
3031
Josef Bacik5fd02042012-05-02 14:00:54 -04003032 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003033 * This needs to be done to make sure anybody waiting knows we are done
3034 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003035 */
3036 btrfs_remove_ordered_extent(inode, ordered_extent);
3037
Liu Bo38c227d2013-01-29 03:18:40 +00003038 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003039 if (new) {
3040 if (ret) {
3041 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003042 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003043 } else {
3044 relink_file_extents(new);
3045 }
3046 }
Liu Bo38c227d2013-01-29 03:18:40 +00003047
Chris Masone6dcd2d2008-07-17 12:53:50 -04003048 /* once for us */
3049 btrfs_put_ordered_extent(ordered_extent);
3050 /* once for the tree */
3051 btrfs_put_ordered_extent(ordered_extent);
3052
Josef Bacik5fd02042012-05-02 14:00:54 -04003053 return ret;
3054}
3055
3056static void finish_ordered_fn(struct btrfs_work *work)
3057{
3058 struct btrfs_ordered_extent *ordered_extent;
3059 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3060 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003061}
3062
Christoph Hellwigb2950862008-12-02 09:54:17 -05003063static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003064 struct extent_state *state, int uptodate)
3065{
Josef Bacik5fd02042012-05-02 14:00:54 -04003066 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003067 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003068 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003069 struct btrfs_workqueue *wq;
3070 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003071
liubo1abe9b82011-03-24 11:18:59 +00003072 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3073
Chris Mason8b62b722009-09-02 16:53:46 -04003074 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003075 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3076 end - start + 1, uptodate))
3077 return 0;
3078
Liu Bo9e0af232014-08-15 23:36:53 +08003079 if (btrfs_is_free_space_inode(inode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003080 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003081 func = btrfs_freespace_write_helper;
3082 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003083 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003084 func = btrfs_endio_write_helper;
3085 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003086
Liu Bo9e0af232014-08-15 23:36:53 +08003087 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3088 NULL);
3089 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003090
3091 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003092}
3093
Miao Xiedc380ae2014-09-12 18:43:55 +08003094static int __readpage_endio_check(struct inode *inode,
3095 struct btrfs_io_bio *io_bio,
3096 int icsum, struct page *page,
3097 int pgoff, u64 start, size_t len)
3098{
3099 char *kaddr;
3100 u32 csum_expected;
3101 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003102
3103 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3104
3105 kaddr = kmap_atomic(page);
3106 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003107 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003108 if (csum != csum_expected)
3109 goto zeroit;
3110
3111 kunmap_atomic(kaddr);
3112 return 0;
3113zeroit:
David Sterba94647322015-10-08 11:01:36 +02003114 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3115 "csum failed ino %llu off %llu csum %u expected csum %u",
David Sterbaf85b7372017-01-20 14:54:07 +01003116 btrfs_ino(BTRFS_I(inode)), start, csum, csum_expected);
Miao Xiedc380ae2014-09-12 18:43:55 +08003117 memset(kaddr + pgoff, 1, len);
3118 flush_dcache_page(page);
3119 kunmap_atomic(kaddr);
3120 if (csum_expected == 0)
3121 return 0;
3122 return -EIO;
3123}
3124
Chris Masond352ac62008-09-29 15:18:18 -04003125/*
Chris Masond352ac62008-09-29 15:18:18 -04003126 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003127 * if there's a match, we allow the bio to finish. If not, the code in
3128 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003129 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003130static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3131 u64 phy_offset, struct page *page,
3132 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003133{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003134 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003135 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003136 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003137 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003138
Chris Masond20f7042008-12-08 16:58:54 -05003139 if (PageChecked(page)) {
3140 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003141 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003142 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003143
3144 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003145 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003146
Yan Zheng17d217f2008-12-12 10:03:38 -05003147 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003148 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003149 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003150 return 0;
3151 }
3152
Miao Xiefacc8a222013-07-25 19:22:34 +08003153 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003154 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3155 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003156}
Chris Masonb888db2b2007-08-27 16:49:44 -04003157
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003158void btrfs_add_delayed_iput(struct inode *inode)
3159{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003160 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003161 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003162
3163 if (atomic_add_unless(&inode->i_count, -1, 1))
3164 return;
3165
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003166 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003167 if (binode->delayed_iput_count == 0) {
3168 ASSERT(list_empty(&binode->delayed_iput));
3169 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3170 } else {
3171 binode->delayed_iput_count++;
3172 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003173 spin_unlock(&fs_info->delayed_iput_lock);
3174}
3175
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003176void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003177{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003178
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003179 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003180 while (!list_empty(&fs_info->delayed_iputs)) {
3181 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003182
David Sterba8089fe62015-11-19 14:15:51 +01003183 inode = list_first_entry(&fs_info->delayed_iputs,
3184 struct btrfs_inode, delayed_iput);
3185 if (inode->delayed_iput_count) {
3186 inode->delayed_iput_count--;
3187 list_move_tail(&inode->delayed_iput,
3188 &fs_info->delayed_iputs);
3189 } else {
3190 list_del_init(&inode->delayed_iput);
3191 }
3192 spin_unlock(&fs_info->delayed_iput_lock);
3193 iput(&inode->vfs_inode);
3194 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003195 }
David Sterba8089fe62015-11-19 14:15:51 +01003196 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003197}
3198
Yan, Zhengd68fc572010-05-16 10:49:58 -04003199/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003200 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003201 * files in the subvolume, it removes orphan item and frees block_rsv
3202 * structure.
3203 */
3204void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3205 struct btrfs_root *root)
3206{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003207 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003208 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003209 int ret;
3210
Josef Bacik8a35d952012-05-23 14:26:42 -04003211 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003212 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3213 return;
3214
Josef Bacik90290e12011-12-02 15:44:12 -05003215 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003216 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003217 spin_unlock(&root->orphan_lock);
3218 return;
3219 }
3220
3221 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3222 spin_unlock(&root->orphan_lock);
3223 return;
3224 }
3225
3226 block_rsv = root->orphan_block_rsv;
3227 root->orphan_block_rsv = NULL;
3228 spin_unlock(&root->orphan_lock);
3229
Miao Xie27cdeb72014-04-02 19:51:05 +08003230 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003231 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003232 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003233 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003234 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003235 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003236 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003237 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3238 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003239 }
3240
Josef Bacik90290e12011-12-02 15:44:12 -05003241 if (block_rsv) {
3242 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003243 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003244 }
3245}
3246
3247/*
Josef Bacik7b128762008-07-24 12:17:14 -04003248 * This creates an orphan entry for the given inode in case something goes
3249 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 *
3251 * NOTE: caller of this function should reserve 5 units of metadata for
3252 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003253 */
3254int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3255{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003256 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003257 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003258 struct btrfs_block_rsv *block_rsv = NULL;
3259 int reserve = 0;
3260 int insert = 0;
3261 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003262
Yan, Zhengd68fc572010-05-16 10:49:58 -04003263 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003264 block_rsv = btrfs_alloc_block_rsv(fs_info,
3265 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003266 if (!block_rsv)
3267 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003268 }
3269
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 spin_lock(&root->orphan_lock);
3271 if (!root->orphan_block_rsv) {
3272 root->orphan_block_rsv = block_rsv;
3273 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003274 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 block_rsv = NULL;
3276 }
Josef Bacik7b128762008-07-24 12:17:14 -04003277
Josef Bacik8a35d952012-05-23 14:26:42 -04003278 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3279 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003280#if 0
3281 /*
3282 * For proper ENOSPC handling, we should do orphan
3283 * cleanup when mounting. But this introduces backward
3284 * compatibility issue.
3285 */
3286 if (!xchg(&root->orphan_item_inserted, 1))
3287 insert = 2;
3288 else
3289 insert = 1;
3290#endif
3291 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003292 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003293 }
Josef Bacik7b128762008-07-24 12:17:14 -04003294
Josef Bacik72ac3c02012-05-23 14:13:11 -04003295 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3296 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003297 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003298 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003299
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 /* grab metadata reservation from transaction handle */
3301 if (reserve) {
3302 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003303 ASSERT(!ret);
3304 if (ret) {
3305 atomic_dec(&root->orphan_inodes);
3306 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3307 &BTRFS_I(inode)->runtime_flags);
3308 if (insert)
3309 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3310 &BTRFS_I(inode)->runtime_flags);
3311 return ret;
3312 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 }
3314
3315 /* insert an orphan item to track this unlinked/truncated file */
3316 if (insert >= 1) {
David Sterbaf85b7372017-01-20 14:54:07 +01003317 ret = btrfs_insert_orphan_item(trans, root,
3318 btrfs_ino(BTRFS_I(inode)));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003319 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003320 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003321 if (reserve) {
3322 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3323 &BTRFS_I(inode)->runtime_flags);
3324 btrfs_orphan_release_metadata(inode);
3325 }
3326 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003327 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3328 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003329 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003330 return ret;
3331 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003332 }
3333 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 }
3335
3336 /* insert an orphan item to track subvolume contains orphan files */
3337 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003338 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003339 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003340 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003341 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003342 return ret;
3343 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003344 }
3345 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003346}
3347
3348/*
3349 * We have done the truncate/delete so we can go ahead and remove the orphan
3350 * item for this particular inode.
3351 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003352static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3353 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003354{
3355 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003356 int delete_item = 0;
3357 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003358 int ret = 0;
3359
Yan, Zhengd68fc572010-05-16 10:49:58 -04003360 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003361 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3362 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003363 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003364
Josef Bacik72ac3c02012-05-23 14:13:11 -04003365 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3366 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003367 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003368 spin_unlock(&root->orphan_lock);
3369
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003370 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003371 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003372 if (trans)
3373 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003374 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04003375 }
Josef Bacik7b128762008-07-24 12:17:14 -04003376
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003377 if (release_rsv)
3378 btrfs_orphan_release_metadata(inode);
3379
Josef Bacik4ef31a42013-08-13 14:10:08 -04003380 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003381}
3382
3383/*
3384 * this cleans up any orphans that may be left on the list from the last use
3385 * of this root.
3386 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003387int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003388{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003389 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003390 struct btrfs_path *path;
3391 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003392 struct btrfs_key key, found_key;
3393 struct btrfs_trans_handle *trans;
3394 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003395 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003396 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3397
Yan, Zhengd68fc572010-05-16 10:49:58 -04003398 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003399 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003400
3401 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003402 if (!path) {
3403 ret = -ENOMEM;
3404 goto out;
3405 }
David Sterbae4058b52015-11-27 16:31:35 +01003406 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003407
3408 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003409 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003410 key.offset = (u64)-1;
3411
Josef Bacik7b128762008-07-24 12:17:14 -04003412 while (1) {
3413 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003414 if (ret < 0)
3415 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003416
3417 /*
3418 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003419 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003420 * find the key and see if we have stuff that matches
3421 */
3422 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003423 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003424 if (path->slots[0] == 0)
3425 break;
3426 path->slots[0]--;
3427 }
3428
3429 /* pull out the item */
3430 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003431 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3432
3433 /* make sure the item matches what we want */
3434 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3435 break;
David Sterba962a2982014-06-04 18:41:45 +02003436 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003437 break;
3438
3439 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003440 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003441
3442 /*
3443 * this is where we are basically btrfs_lookup, without the
3444 * crossing root thing. we store the inode number in the
3445 * offset of the orphan item.
3446 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003447
3448 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003449 btrfs_err(fs_info,
3450 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003451 ret = -EINVAL;
3452 goto out;
3453 }
3454
3455 last_objectid = found_key.offset;
3456
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003457 found_key.objectid = found_key.offset;
3458 found_key.type = BTRFS_INODE_ITEM_KEY;
3459 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003460 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303461 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003462 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003463 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003464
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003465 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003466 struct btrfs_root *dead_root;
3467 struct btrfs_fs_info *fs_info = root->fs_info;
3468 int is_dead_root = 0;
3469
3470 /*
3471 * this is an orphan in the tree root. Currently these
3472 * could come from 2 sources:
3473 * a) a snapshot deletion in progress
3474 * b) a free space cache inode
3475 * We need to distinguish those two, as the snapshot
3476 * orphan must not get deleted.
3477 * find_dead_roots already ran before us, so if this
3478 * is a snapshot deletion, we should find the root
3479 * in the dead_roots list
3480 */
3481 spin_lock(&fs_info->trans_lock);
3482 list_for_each_entry(dead_root, &fs_info->dead_roots,
3483 root_list) {
3484 if (dead_root->root_key.objectid ==
3485 found_key.objectid) {
3486 is_dead_root = 1;
3487 break;
3488 }
3489 }
3490 spin_unlock(&fs_info->trans_lock);
3491 if (is_dead_root) {
3492 /* prevent this orphan from being found again */
3493 key.offset = found_key.objectid - 1;
3494 continue;
3495 }
3496 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003497 /*
3498 * Inode is already gone but the orphan item is still there,
3499 * kill the orphan item.
3500 */
Filipe Manana67710892016-06-06 11:51:25 +01003501 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003502 trans = btrfs_start_transaction(root, 1);
3503 if (IS_ERR(trans)) {
3504 ret = PTR_ERR(trans);
3505 goto out;
3506 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003507 btrfs_debug(fs_info, "auto deleting %Lu",
3508 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003509 ret = btrfs_del_orphan_item(trans, root,
3510 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003511 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003512 if (ret)
3513 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003514 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003515 }
Josef Bacik7b128762008-07-24 12:17:14 -04003516
Josef Bacik7b128762008-07-24 12:17:14 -04003517 /*
3518 * add this inode to the orphan list so btrfs_orphan_del does
3519 * the proper thing when we hit it
3520 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003521 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3522 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003523 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003524
Josef Bacik7b128762008-07-24 12:17:14 -04003525 /* if we have links, this was a truncate, lets do that */
3526 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303527 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003528 iput(inode);
3529 continue;
3530 }
Josef Bacik7b128762008-07-24 12:17:14 -04003531 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003532
3533 /* 1 for the orphan item deletion. */
3534 trans = btrfs_start_transaction(root, 1);
3535 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003536 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003537 ret = PTR_ERR(trans);
3538 goto out;
3539 }
3540 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003541 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003542 if (ret) {
3543 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003544 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003545 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003546
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003547 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003548 if (ret)
3549 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003550 } else {
3551 nr_unlink++;
3552 }
3553
3554 /* this will do delete_inode and everything for us */
3555 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003556 if (ret)
3557 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003558 }
Miao Xie3254c872011-11-10 20:45:05 -05003559 /* release the path since we're done with it */
3560 btrfs_release_path(path);
3561
Yan, Zhengd68fc572010-05-16 10:49:58 -04003562 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3563
3564 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003565 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003566 (u64)-1);
3567
Miao Xie27cdeb72014-04-02 19:51:05 +08003568 if (root->orphan_block_rsv ||
3569 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003570 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003571 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003572 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003573 }
Josef Bacik7b128762008-07-24 12:17:14 -04003574
3575 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003576 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003577 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003578 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003579
3580out:
3581 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003582 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003583 btrfs_free_path(path);
3584 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003585}
3586
Chris Masond352ac62008-09-29 15:18:18 -04003587/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003588 * very simple check to peek ahead in the leaf looking for xattrs. If we
3589 * don't find any xattrs, we know there can't be any acls.
3590 *
3591 * slot is the slot the inode is in, objectid is the objectid of the inode
3592 */
3593static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003594 int slot, u64 objectid,
3595 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003596{
3597 u32 nritems = btrfs_header_nritems(leaf);
3598 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003599 static u64 xattr_access = 0;
3600 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003601 int scanned = 0;
3602
Josef Bacikf23b5a52013-06-19 10:16:26 -04003603 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003604 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3605 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3606 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3607 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003608 }
3609
Chris Mason46a53cc2009-04-27 11:47:50 -04003610 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003611 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003612 while (slot < nritems) {
3613 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3614
3615 /* we found a different objectid, there must not be acls */
3616 if (found_key.objectid != objectid)
3617 return 0;
3618
3619 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003620 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003621 if (*first_xattr_slot == -1)
3622 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003623 if (found_key.offset == xattr_access ||
3624 found_key.offset == xattr_default)
3625 return 1;
3626 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003627
3628 /*
3629 * we found a key greater than an xattr key, there can't
3630 * be any acls later on
3631 */
3632 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3633 return 0;
3634
3635 slot++;
3636 scanned++;
3637
3638 /*
3639 * it goes inode, inode backrefs, xattrs, extents,
3640 * so if there are a ton of hard links to an inode there can
3641 * be a lot of backrefs. Don't waste time searching too hard,
3642 * this is just an optimization
3643 */
3644 if (scanned >= 8)
3645 break;
3646 }
3647 /* we hit the end of the leaf before we found an xattr or
3648 * something larger than an xattr. We have to assume the inode
3649 * has acls
3650 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003651 if (*first_xattr_slot == -1)
3652 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003653 return 1;
3654}
3655
3656/*
Chris Masond352ac62008-09-29 15:18:18 -04003657 * read an inode from the btree into the in-memory inode
3658 */
Filipe Manana67710892016-06-06 11:51:25 +01003659static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003660{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003661 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003662 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003663 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003664 struct btrfs_inode_item *inode_item;
3665 struct btrfs_root *root = BTRFS_I(inode)->root;
3666 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003667 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003668 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003669 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003670 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003671 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003672 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003673
3674 ret = btrfs_fill_inode(inode, &rdev);
3675 if (!ret)
3676 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003677
3678 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003679 if (!path) {
3680 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003681 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003682 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003683
Chris Mason39279cc2007-06-12 06:35:45 -04003684 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003685
Chris Mason39279cc2007-06-12 06:35:45 -04003686 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003687 if (ret) {
3688 if (ret > 0)
3689 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003690 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003691 }
Chris Mason39279cc2007-06-12 06:35:45 -04003692
Chris Mason5f39d392007-10-15 16:14:19 -04003693 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003694
3695 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003696 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003697
Chris Mason5f39d392007-10-15 16:14:19 -04003698 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3699 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003700 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003701 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003702 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3703 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003704 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003705
David Sterbaa937b972014-12-12 17:39:12 +01003706 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3707 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003708
David Sterbaa937b972014-12-12 17:39:12 +01003709 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3710 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003711
David Sterbaa937b972014-12-12 17:39:12 +01003712 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3713 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003714
chandan r9cc97d62012-07-04 12:48:07 +05303715 BTRFS_I(inode)->i_otime.tv_sec =
3716 btrfs_timespec_sec(leaf, &inode_item->otime);
3717 BTRFS_I(inode)->i_otime.tv_nsec =
3718 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003719
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003720 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003721 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003722 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3723
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003724 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003725 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003726 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003727 rdev = btrfs_inode_rdev(leaf, inode_item);
3728
Josef Bacikaec74772008-07-24 12:12:38 -04003729 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003730 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003731
3732cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003733 /*
3734 * If we were modified in the current generation and evicted from memory
3735 * and then re-read we need to do a full sync since we don't have any
3736 * idea about which extents were modified before we were evicted from
3737 * cache.
3738 *
3739 * This is required for both inode re-read from disk and delayed inode
3740 * in delayed_nodes_tree.
3741 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003742 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003743 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3744 &BTRFS_I(inode)->runtime_flags);
3745
Filipe Mananabde6c242015-07-24 00:00:19 +01003746 /*
3747 * We don't persist the id of the transaction where an unlink operation
3748 * against the inode was last made. So here we assume the inode might
3749 * have been evicted, and therefore the exact value of last_unlink_trans
3750 * lost, and set it to last_trans to avoid metadata inconsistencies
3751 * between the inode and its parent if the inode is fsync'ed and the log
3752 * replayed. For example, in the scenario:
3753 *
3754 * touch mydir/foo
3755 * ln mydir/foo mydir/bar
3756 * sync
3757 * unlink mydir/bar
3758 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3759 * xfs_io -c fsync mydir/foo
3760 * <power failure>
3761 * mount fs, triggers fsync log replay
3762 *
3763 * We must make sure that when we fsync our inode foo we also log its
3764 * parent inode, otherwise after log replay the parent still has the
3765 * dentry with the "bar" name but our inode foo has a link count of 1
3766 * and doesn't have an inode ref with the name "bar" anymore.
3767 *
3768 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003769 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003770 * transaction commits on fsync if our inode is a directory, or if our
3771 * inode is not a directory, logging its parent unnecessarily.
3772 */
3773 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3774
Miao Xie67de1172013-12-26 13:07:06 +08003775 path->slots[0]++;
3776 if (inode->i_nlink != 1 ||
3777 path->slots[0] >= btrfs_header_nritems(leaf))
3778 goto cache_acl;
3779
3780 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003781 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003782 goto cache_acl;
3783
3784 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3785 if (location.type == BTRFS_INODE_REF_KEY) {
3786 struct btrfs_inode_ref *ref;
3787
3788 ref = (struct btrfs_inode_ref *)ptr;
3789 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3790 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3791 struct btrfs_inode_extref *extref;
3792
3793 extref = (struct btrfs_inode_extref *)ptr;
3794 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3795 extref);
3796 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003797cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003798 /*
3799 * try to precache a NULL acl entry for files that don't have
3800 * any xattrs or acls
3801 */
Li Zefan33345d012011-04-20 10:31:50 +08003802 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003803 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003804 if (first_xattr_slot != -1) {
3805 path->slots[0] = first_xattr_slot;
3806 ret = btrfs_load_inode_props(inode, path);
3807 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003808 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003809 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003810 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003811 root->root_key.objectid, ret);
3812 }
3813 btrfs_free_path(path);
3814
Al Viro72c04902009-06-24 16:58:48 -04003815 if (!maybe_acls)
3816 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003817
Chris Mason39279cc2007-06-12 06:35:45 -04003818 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003819 case S_IFREG:
3820 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003821 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003822 inode->i_fop = &btrfs_file_operations;
3823 inode->i_op = &btrfs_file_inode_operations;
3824 break;
3825 case S_IFDIR:
3826 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003827 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003828 break;
3829 case S_IFLNK:
3830 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003831 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003832 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3833 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003834 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003835 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003836 init_special_inode(inode, inode->i_mode, rdev);
3837 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003838 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003839
3840 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003841 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003842
3843make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003844 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003845 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003846 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003847}
3848
Chris Masond352ac62008-09-29 15:18:18 -04003849/*
3850 * given a leaf and an inode, copy the inode fields into the leaf
3851 */
Chris Masone02119d2008-09-05 16:13:11 -04003852static void fill_inode_item(struct btrfs_trans_handle *trans,
3853 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003854 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003855 struct inode *inode)
3856{
Liu Bo51fab692012-12-27 09:01:21 +00003857 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003858
Liu Bo51fab692012-12-27 09:01:21 +00003859 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003860
Liu Bo51fab692012-12-27 09:01:21 +00003861 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3862 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3863 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3864 &token);
3865 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3866 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003867
David Sterbaa937b972014-12-12 17:39:12 +01003868 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003869 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003870 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003871 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003872
David Sterbaa937b972014-12-12 17:39:12 +01003873 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003874 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003875 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003876 inode->i_mtime.tv_nsec, &token);
3877
David Sterbaa937b972014-12-12 17:39:12 +01003878 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003879 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003880 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003881 inode->i_ctime.tv_nsec, &token);
3882
chandan r9cc97d62012-07-04 12:48:07 +05303883 btrfs_set_token_timespec_sec(leaf, &item->otime,
3884 BTRFS_I(inode)->i_otime.tv_sec, &token);
3885 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3886 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3887
Liu Bo51fab692012-12-27 09:01:21 +00003888 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3889 &token);
3890 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3891 &token);
3892 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3893 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3894 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3895 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3896 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003897}
3898
Chris Masond352ac62008-09-29 15:18:18 -04003899/*
3900 * copy everything in the in-memory inode into the btree.
3901 */
Chris Mason21151332011-11-10 20:39:08 -05003902static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003903 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003904{
3905 struct btrfs_inode_item *inode_item;
3906 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003907 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 int ret;
3909
3910 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003911 if (!path)
3912 return -ENOMEM;
3913
Chris Masonb9473432009-03-13 11:00:37 -04003914 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003915 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3916 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003917 if (ret) {
3918 if (ret > 0)
3919 ret = -ENOENT;
3920 goto failed;
3921 }
3922
Chris Mason5f39d392007-10-15 16:14:19 -04003923 leaf = path->nodes[0];
3924 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003925 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003926
Chris Masone02119d2008-09-05 16:13:11 -04003927 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003928 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003929 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003930 ret = 0;
3931failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003932 btrfs_free_path(path);
3933 return ret;
3934}
3935
Chris Masond352ac62008-09-29 15:18:18 -04003936/*
Chris Mason21151332011-11-10 20:39:08 -05003937 * copy everything in the in-memory inode into the btree.
3938 */
3939noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3940 struct btrfs_root *root, struct inode *inode)
3941{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003942 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003943 int ret;
3944
3945 /*
3946 * If the inode is a free space inode, we can deadlock during commit
3947 * if we put it into the delayed code.
3948 *
3949 * The data relocation inode should also be directly updated
3950 * without delay
3951 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003952 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003953 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003954 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003955 btrfs_update_root_times(trans, root);
3956
Chris Mason21151332011-11-10 20:39:08 -05003957 ret = btrfs_delayed_update_inode(trans, root, inode);
3958 if (!ret)
3959 btrfs_set_inode_last_trans(trans, inode);
3960 return ret;
3961 }
3962
3963 return btrfs_update_inode_item(trans, root, inode);
3964}
3965
Josef Bacikbe6aef62012-10-22 15:43:12 -04003966noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3967 struct btrfs_root *root,
3968 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003969{
3970 int ret;
3971
3972 ret = btrfs_update_inode(trans, root, inode);
3973 if (ret == -ENOSPC)
3974 return btrfs_update_inode_item(trans, root, inode);
3975 return ret;
3976}
3977
3978/*
Chris Masond352ac62008-09-29 15:18:18 -04003979 * unlink helper that gets used here in inode.c and in the tree logging
3980 * recovery code. It remove a link in a directory with a given name, and
3981 * also drops the back refs in the inode to the directory
3982 */
Al Viro92986792011-03-04 17:14:37 +00003983static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3984 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003985 struct btrfs_inode *dir,
3986 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003987 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003988{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003989 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003990 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003991 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003992 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003993 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003994 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003995 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003996 u64 ino = btrfs_ino(inode);
3997 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003998
3999 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004000 if (!path) {
4001 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004002 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004003 }
4004
Chris Masonb9473432009-03-13 11:00:37 -04004005 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004006 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004007 name, name_len, -1);
4008 if (IS_ERR(di)) {
4009 ret = PTR_ERR(di);
4010 goto err;
4011 }
4012 if (!di) {
4013 ret = -ENOENT;
4014 goto err;
4015 }
Chris Mason5f39d392007-10-15 16:14:19 -04004016 leaf = path->nodes[0];
4017 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004018 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004019 if (ret)
4020 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004021 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004022
Miao Xie67de1172013-12-26 13:07:06 +08004023 /*
4024 * If we don't have dir index, we have to get it by looking up
4025 * the inode ref, since we get the inode ref, remove it directly,
4026 * it is unnecessary to do delayed deletion.
4027 *
4028 * But if we have dir index, needn't search inode ref to get it.
4029 * Since the inode ref is close to the inode item, it is better
4030 * that we delay to delete it, and just do this deletion when
4031 * we update the inode item.
4032 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004033 if (inode->dir_index) {
4034 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08004035 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004036 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004037 goto skip_backref;
4038 }
4039 }
4040
Li Zefan33345d012011-04-20 10:31:50 +08004041 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4042 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004043 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004044 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004045 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004046 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004047 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004048 goto err;
4049 }
Miao Xie67de1172013-12-26 13:07:06 +08004050skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004051 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004052 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004053 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004054 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004055 }
Chris Mason39279cc2007-06-12 06:35:45 -04004056
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004057 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4058 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004059 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004060 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004061 goto err;
4062 }
Chris Masone02119d2008-09-05 16:13:11 -04004063
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004064 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4065 index);
Chris Mason6418c962010-10-30 07:34:24 -04004066 if (ret == -ENOENT)
4067 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004068 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004069 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004070err:
4071 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004072 if (ret)
4073 goto out;
4074
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004075 btrfs_i_size_write(&dir->vfs_inode,
4076 dir->vfs_inode.i_size - name_len * 2);
4077 inode_inc_iversion(&inode->vfs_inode);
4078 inode_inc_iversion(&dir->vfs_inode);
4079 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4080 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4081 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004082out:
Chris Mason39279cc2007-06-12 06:35:45 -04004083 return ret;
4084}
4085
Al Viro92986792011-03-04 17:14:37 +00004086int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4087 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004088 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004089 const char *name, int name_len)
4090{
4091 int ret;
4092 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4093 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004094 drop_nlink(&inode->vfs_inode);
4095 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004096 }
4097 return ret;
4098}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004099
4100/*
4101 * helper to start transaction for unlink and rmdir.
4102 *
Josef Bacikd52be812013-05-29 14:54:47 -04004103 * unlink and rmdir are special in btrfs, they do not always free space, so
4104 * if we cannot make our reservations the normal way try and see if there is
4105 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4106 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004107 */
Josef Bacikd52be812013-05-29 14:54:47 -04004108static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004109{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004110 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004111
Josef Bacike70bea52011-10-11 14:18:24 -04004112 /*
4113 * 1 for the possible orphan item
4114 * 1 for the dir item
4115 * 1 for the dir index
4116 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004117 * 1 for the inode
4118 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004119 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004120}
4121
Chris Mason39279cc2007-06-12 06:35:45 -04004122static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4123{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004124 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004125 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004126 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004127 int ret;
4128
Josef Bacikd52be812013-05-29 14:54:47 -04004129 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004130 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004131 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004132
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004133 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4134 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004135
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004136 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4137 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4138 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004139 if (ret)
4140 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004141
Yan, Zhenga22285a2010-05-16 10:48:46 -04004142 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004143 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004144 if (ret)
4145 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004146 }
Josef Bacik7b128762008-07-24 12:17:14 -04004147
Tsutomu Itohb5324022011-07-19 07:27:20 +00004148out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004149 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004150 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004151 return ret;
4152}
4153
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004154int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4155 struct btrfs_root *root,
4156 struct inode *dir, u64 objectid,
4157 const char *name, int name_len)
4158{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004159 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004160 struct btrfs_path *path;
4161 struct extent_buffer *leaf;
4162 struct btrfs_dir_item *di;
4163 struct btrfs_key key;
4164 u64 index;
4165 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004166 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004167
4168 path = btrfs_alloc_path();
4169 if (!path)
4170 return -ENOMEM;
4171
Li Zefan33345d012011-04-20 10:31:50 +08004172 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004173 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004174 if (IS_ERR_OR_NULL(di)) {
4175 if (!di)
4176 ret = -ENOENT;
4177 else
4178 ret = PTR_ERR(di);
4179 goto out;
4180 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004181
4182 leaf = path->nodes[0];
4183 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4184 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4185 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004186 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004187 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004188 goto out;
4189 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004190 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004191
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004192 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4193 root->root_key.objectid, dir_ino,
4194 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004195 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004196 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004197 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004198 goto out;
4199 }
Li Zefan33345d012011-04-20 10:31:50 +08004200 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004201 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004202 if (IS_ERR_OR_NULL(di)) {
4203 if (!di)
4204 ret = -ENOENT;
4205 else
4206 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004207 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004208 goto out;
4209 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004210
4211 leaf = path->nodes[0];
4212 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004213 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004214 index = key.offset;
4215 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004216 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004217
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004218 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004219 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004220 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004221 goto out;
4222 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004223
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004224 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004225 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004226 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004227 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004228 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004229 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004230out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004231 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004232 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004233}
4234
Chris Mason39279cc2007-06-12 06:35:45 -04004235static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4236{
David Howells2b0143b2015-03-17 22:25:59 +00004237 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004238 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004239 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004240 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004241 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004242
David Sterbab3ae2442012-09-13 16:04:34 -06004243 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004244 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004245 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004246 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004247
Josef Bacikd52be812013-05-29 14:54:47 -04004248 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004249 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004250 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004251
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004252 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004253 err = btrfs_unlink_subvol(trans, root, dir,
4254 BTRFS_I(inode)->location.objectid,
4255 dentry->d_name.name,
4256 dentry->d_name.len);
4257 goto out;
4258 }
4259
Josef Bacik7b128762008-07-24 12:17:14 -04004260 err = btrfs_orphan_add(trans, inode);
4261 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004262 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004263
Filipe Manana44f714d2016-06-06 16:11:13 +01004264 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4265
Chris Mason39279cc2007-06-12 06:35:45 -04004266 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004267 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4268 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4269 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004270 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004271 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004272 /*
4273 * Propagate the last_unlink_trans value of the deleted dir to
4274 * its parent directory. This is to prevent an unrecoverable
4275 * log tree in the case we do something like this:
4276 * 1) create dir foo
4277 * 2) create snapshot under dir foo
4278 * 3) delete the snapshot
4279 * 4) rmdir foo
4280 * 5) mkdir foo
4281 * 6) fsync foo or some file inside foo
4282 */
4283 if (last_unlink_trans >= trans->transid)
4284 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4285 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004286out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004287 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004288 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004289
Chris Mason39279cc2007-06-12 06:35:45 -04004290 return err;
4291}
4292
Chris Mason28f75a02015-02-04 06:59:29 -08004293static int truncate_space_check(struct btrfs_trans_handle *trans,
4294 struct btrfs_root *root,
4295 u64 bytes_deleted)
4296{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004297 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004298 int ret;
4299
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004300 /*
4301 * This is only used to apply pressure to the enospc system, we don't
4302 * intend to use this reservation at all.
4303 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004304 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004305 bytes_deleted *= fs_info->nodesize;
4306 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004307 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004308 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004309 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004310 trans->transid,
4311 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004312 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004313 }
Chris Mason28f75a02015-02-04 06:59:29 -08004314 return ret;
4315
4316}
4317
Filipe Manana0305cd52015-10-16 12:34:25 +01004318static int truncate_inline_extent(struct inode *inode,
4319 struct btrfs_path *path,
4320 struct btrfs_key *found_key,
4321 const u64 item_end,
4322 const u64 new_size)
4323{
4324 struct extent_buffer *leaf = path->nodes[0];
4325 int slot = path->slots[0];
4326 struct btrfs_file_extent_item *fi;
4327 u32 size = (u32)(new_size - found_key->offset);
4328 struct btrfs_root *root = BTRFS_I(inode)->root;
4329
4330 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4331
4332 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4333 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004334 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004335
4336 /*
4337 * Zero out the remaining of the last page of our inline extent,
4338 * instead of directly truncating our inline extent here - that
4339 * would be much more complex (decompressing all the data, then
4340 * compressing the truncated data, which might be bigger than
4341 * the size of the inline extent, resize the extent, etc).
4342 * We release the path because to get the page we might need to
4343 * read the extent item from disk (data not in the page cache).
4344 */
4345 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304346 return btrfs_truncate_block(inode, offset, page_end - offset,
4347 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004348 }
4349
4350 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4351 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004352 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004353
4354 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4355 inode_sub_bytes(inode, item_end + 1 - new_size);
4356
4357 return 0;
4358}
4359
Chris Mason323ac952008-10-01 19:05:46 -04004360/*
Chris Mason39279cc2007-06-12 06:35:45 -04004361 * this can truncate away extent items, csum items and directory items.
4362 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004363 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004364 *
4365 * csum items that cross the new i_size are truncated to the new size
4366 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004367 *
4368 * min_type is the minimum key type to truncate down to. If set to 0, this
4369 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004370 */
Yan, Zheng80825102009-11-12 09:35:36 +00004371int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4372 struct btrfs_root *root,
4373 struct inode *inode,
4374 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004375{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004376 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004377 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004378 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004379 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004380 struct btrfs_key key;
4381 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004382 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004383 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004384 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004385 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004386 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004387 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004388 int found_extent;
4389 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004390 int pending_del_nr = 0;
4391 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004392 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004393 int ret;
4394 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004395 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004396 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004397 bool be_nice = 0;
4398 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004399 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004400
4401 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004402
Chris Mason28ed1342014-12-17 09:41:04 -08004403 /*
4404 * for non-free space inodes and ref cows, we want to back off from
4405 * time to time
4406 */
4407 if (!btrfs_is_free_space_inode(inode) &&
4408 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4409 be_nice = 1;
4410
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004411 path = btrfs_alloc_path();
4412 if (!path)
4413 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004414 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004415
Josef Bacik5dc562c2012-08-17 13:14:17 -04004416 /*
4417 * We want to drop from the next block forward in case this new size is
4418 * not block aligned since we will be keeping the last block of the
4419 * extent just the way it is.
4420 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004421 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004422 root == fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004423 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004424 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004425 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004426
Miao Xie16cdcec2011-04-22 18:12:22 +08004427 /*
4428 * This function is also used to drop the items in the log tree before
4429 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4430 * it is used to drop the loged items. So we shouldn't kill the delayed
4431 * items.
4432 */
4433 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004434 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004435
Li Zefan33345d012011-04-20 10:31:50 +08004436 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004437 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004438 key.type = (u8)-1;
4439
Chris Mason85e21ba2008-01-29 15:11:36 -05004440search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004441 /*
4442 * with a 16K leaf size and 128MB extents, you can actually queue
4443 * up a huge file in a single leaf. Most of the time that
4444 * bytes_deleted is > 0, it will be huge by the time we get here
4445 */
Byongho Leeee221842015-12-15 01:42:10 +09004446 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004447 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004448 err = -EAGAIN;
4449 goto error;
4450 }
4451 }
4452
4453
Chris Masonb9473432009-03-13 11:00:37 -04004454 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004455 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004456 if (ret < 0) {
4457 err = ret;
4458 goto out;
4459 }
Chris Masond3977122009-01-05 21:25:51 -05004460
Chris Mason85e21ba2008-01-29 15:11:36 -05004461 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004462 /* there are no items in the tree for us to truncate, we're
4463 * done
4464 */
Yan, Zheng80825102009-11-12 09:35:36 +00004465 if (path->slots[0] == 0)
4466 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004467 path->slots[0]--;
4468 }
4469
Chris Masond3977122009-01-05 21:25:51 -05004470 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004471 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004472 leaf = path->nodes[0];
4473 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004474 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004475
Li Zefan33345d012011-04-20 10:31:50 +08004476 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004477 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004478
Chris Mason85e21ba2008-01-29 15:11:36 -05004479 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004480 break;
4481
Chris Mason5f39d392007-10-15 16:14:19 -04004482 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004483 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004484 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004485 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004486 extent_type = btrfs_file_extent_type(leaf, fi);
4487 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004488 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004489 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004490 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004491 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004492 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004493 }
Yan008630c2007-11-07 13:31:09 -05004494 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004495 }
Yan, Zheng80825102009-11-12 09:35:36 +00004496 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004497 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004498 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004499 if (item_end < new_size) {
4500 /*
4501 * With NO_HOLES mode, for the following mapping
4502 *
4503 * [0-4k][hole][8k-12k]
4504 *
4505 * if truncating isize down to 6k, it ends up
4506 * isize being 8k.
4507 */
4508 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4509 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004510 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004511 }
Yan, Zheng80825102009-11-12 09:35:36 +00004512 if (found_key.offset >= new_size)
4513 del_item = 1;
4514 else
4515 del_item = 0;
4516 }
Chris Mason39279cc2007-06-12 06:35:45 -04004517 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004518 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004519 if (found_type != BTRFS_EXTENT_DATA_KEY)
4520 goto delete;
4521
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004522 if (del_item)
4523 last_size = found_key.offset;
4524 else
4525 last_size = new_size;
4526
Chris Mason179e29e2007-11-01 11:28:41 -04004527 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004528 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004529 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004530 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004531 u64 orig_num_bytes =
4532 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004533 extent_num_bytes = ALIGN(new_size -
4534 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004535 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004536 btrfs_set_file_extent_num_bytes(leaf, fi,
4537 extent_num_bytes);
4538 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004539 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004540 if (test_bit(BTRFS_ROOT_REF_COWS,
4541 &root->state) &&
4542 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004543 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004544 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004545 } else {
Chris Masondb945352007-10-15 16:15:53 -04004546 extent_num_bytes =
4547 btrfs_file_extent_disk_num_bytes(leaf,
4548 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004549 extent_offset = found_key.offset -
4550 btrfs_file_extent_offset(leaf, fi);
4551
Chris Mason39279cc2007-06-12 06:35:45 -04004552 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004553 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004554 if (extent_start != 0) {
4555 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004556 if (test_bit(BTRFS_ROOT_REF_COWS,
4557 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004558 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004559 }
4560 }
Chris Mason90692182008-02-08 13:49:28 -05004561 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004562 /*
4563 * we can't truncate inline items that have had
4564 * special encodings
4565 */
4566 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004567 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4568 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004569
4570 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004571 * Need to release path in order to truncate a
4572 * compressed extent. So delete any accumulated
4573 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004574 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004575 if (btrfs_file_extent_compression(leaf, fi) !=
4576 BTRFS_COMPRESS_NONE && pending_del_nr) {
4577 err = btrfs_del_items(trans, root, path,
4578 pending_del_slot,
4579 pending_del_nr);
4580 if (err) {
4581 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004582 err);
4583 goto error;
4584 }
4585 pending_del_nr = 0;
4586 }
4587
4588 err = truncate_inline_extent(inode, path,
4589 &found_key,
4590 item_end,
4591 new_size);
4592 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004593 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004594 goto error;
4595 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004596 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4597 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004598 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004599 }
Chris Mason39279cc2007-06-12 06:35:45 -04004600 }
Chris Mason179e29e2007-11-01 11:28:41 -04004601delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004602 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004603 if (!pending_del_nr) {
4604 /* no pending yet, add ourselves */
4605 pending_del_slot = path->slots[0];
4606 pending_del_nr = 1;
4607 } else if (pending_del_nr &&
4608 path->slots[0] + 1 == pending_del_slot) {
4609 /* hop on the pending chunk */
4610 pending_del_nr++;
4611 pending_del_slot = path->slots[0];
4612 } else {
Chris Masond3977122009-01-05 21:25:51 -05004613 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004614 }
Chris Mason39279cc2007-06-12 06:35:45 -04004615 } else {
4616 break;
4617 }
Chris Mason28f75a02015-02-04 06:59:29 -08004618 should_throttle = 0;
4619
Miao Xie27cdeb72014-04-02 19:51:05 +08004620 if (found_extent &&
4621 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004622 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004623 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004624 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004625 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004626 extent_num_bytes, 0,
4627 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004628 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004629 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004630 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4631 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004632 trans->delayed_ref_updates * 2,
4633 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004634 if (be_nice) {
4635 if (truncate_space_check(trans, root,
4636 extent_num_bytes)) {
4637 should_end = 1;
4638 }
4639 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004640 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004641 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004642 }
Chris Mason39279cc2007-06-12 06:35:45 -04004643 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004644
Yan, Zheng80825102009-11-12 09:35:36 +00004645 if (found_type == BTRFS_INODE_ITEM_KEY)
4646 break;
4647
4648 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004649 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004650 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004651 if (pending_del_nr) {
4652 ret = btrfs_del_items(trans, root, path,
4653 pending_del_slot,
4654 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004655 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004656 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004657 goto error;
4658 }
Yan, Zheng80825102009-11-12 09:35:36 +00004659 pending_del_nr = 0;
4660 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004661 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004662 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004663 unsigned long updates = trans->delayed_ref_updates;
4664 if (updates) {
4665 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004666 ret = btrfs_run_delayed_refs(trans,
4667 fs_info,
4668 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004669 if (ret && !err)
4670 err = ret;
4671 }
4672 }
Chris Mason28f75a02015-02-04 06:59:29 -08004673 /*
4674 * if we failed to refill our space rsv, bail out
4675 * and let the transaction restart
4676 */
4677 if (should_end) {
4678 err = -EAGAIN;
4679 goto error;
4680 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004681 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004682 } else {
4683 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004684 }
Chris Mason39279cc2007-06-12 06:35:45 -04004685 }
Yan, Zheng80825102009-11-12 09:35:36 +00004686out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004687 if (pending_del_nr) {
4688 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4689 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004690 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004691 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004692 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004693error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004694 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004695 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004696
Chris Mason39279cc2007-06-12 06:35:45 -04004697 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004698
Liu Bo19fd2df2016-12-01 13:01:02 -08004699 if (err == 0) {
4700 /* only inline file may have last_size != new_size */
4701 if (new_size >= fs_info->sectorsize ||
4702 new_size > fs_info->max_inline)
4703 ASSERT(last_size == new_size);
4704 }
4705
Byongho Leeee221842015-12-15 01:42:10 +09004706 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004707 unsigned long updates = trans->delayed_ref_updates;
4708 if (updates) {
4709 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004710 ret = btrfs_run_delayed_refs(trans, fs_info,
4711 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004712 if (ret && !err)
4713 err = ret;
4714 }
4715 }
Yan, Zheng80825102009-11-12 09:35:36 +00004716 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004717}
4718
Chris Masona52d9a82007-08-27 16:49:44 -04004719/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304720 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004721 * @inode - inode that we're zeroing
4722 * @from - the offset to start zeroing
4723 * @len - the length to zero, 0 to zero the entire range respective to the
4724 * offset
4725 * @front - zero up to the offset instead of from the offset on
4726 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304727 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004728 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004729 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304730int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004731 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004732{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004734 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004735 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4736 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004737 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004738 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004739 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004740 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304741 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004742 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004743 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004744 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304745 u64 block_start;
4746 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004747
Josef Bacik2aaa6652012-08-29 14:27:18 -04004748 if ((offset & (blocksize - 1)) == 0 &&
4749 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004750 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304751
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004752 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304753 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004754 if (ret)
4755 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004756
Chris Mason211c17f2008-05-15 09:13:45 -04004757again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004758 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004759 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004760 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304761 round_down(from, blocksize),
4762 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004763 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004764 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004765 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004766
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304767 block_start = round_down(from, blocksize);
4768 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004769
Chris Masona52d9a82007-08-27 16:49:44 -04004770 if (!PageUptodate(page)) {
4771 ret = btrfs_readpage(NULL, page);
4772 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004773 if (page->mapping != mapping) {
4774 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004775 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004776 goto again;
4777 }
Chris Masona52d9a82007-08-27 16:49:44 -04004778 if (!PageUptodate(page)) {
4779 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004780 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004781 }
4782 }
Chris Mason211c17f2008-05-15 09:13:45 -04004783 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004784
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304785 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004786 set_page_extent_mapped(page);
4787
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304788 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004789 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304790 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004791 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004792 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004793 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004794 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004795 btrfs_put_ordered_extent(ordered);
4796 goto again;
4797 }
4798
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304799 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004800 EXTENT_DIRTY | EXTENT_DELALLOC |
4801 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004802 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004803
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304804 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004805 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004806 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304807 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004808 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004809 goto out_unlock;
4810 }
4811
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304812 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004813 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304814 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004815 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004816 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304817 memset(kaddr + (block_start - page_offset(page)),
4818 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004819 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304820 memset(kaddr + (block_start - page_offset(page)) + offset,
4821 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004822 flush_dcache_page(page);
4823 kunmap(page);
4824 }
Chris Mason247e7432008-07-17 12:53:51 -04004825 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004826 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304827 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004828 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004829
Chris Mason89642222008-07-24 09:41:53 -04004830out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004831 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304832 btrfs_delalloc_release_space(inode, block_start,
4833 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004834 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004835 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004836out:
4837 return ret;
4838}
4839
Josef Bacik16e75492013-10-22 12:18:51 -04004840static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4841 u64 offset, u64 len)
4842{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004843 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004844 struct btrfs_trans_handle *trans;
4845 int ret;
4846
4847 /*
4848 * Still need to make sure the inode looks like it's been updated so
4849 * that any holes get logged if we fsync.
4850 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004851 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4852 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004853 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4854 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4855 return 0;
4856 }
4857
4858 /*
4859 * 1 - for the one we're dropping
4860 * 1 - for the one we're adding
4861 * 1 - for updating the inode.
4862 */
4863 trans = btrfs_start_transaction(root, 3);
4864 if (IS_ERR(trans))
4865 return PTR_ERR(trans);
4866
4867 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4868 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004869 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004870 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004871 return ret;
4872 }
4873
David Sterbaf85b7372017-01-20 14:54:07 +01004874 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4875 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004876 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004877 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004878 else
4879 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004880 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004881 return ret;
4882}
4883
Josef Bacik695a0d02011-03-04 15:46:53 -05004884/*
4885 * This function puts in dummy file extents for the area we're creating a hole
4886 * for. So if we are truncating this file to a larger size we need to insert
4887 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4888 * the range between oldsize and size
4889 */
Josef Bacika41ad392011-01-31 15:30:16 -05004890int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004891{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004892 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004893 struct btrfs_root *root = BTRFS_I(inode)->root;
4894 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004895 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004896 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004897 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004898 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4899 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004900 u64 last_byte;
4901 u64 cur_offset;
4902 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004903 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004904
Josef Bacika71754f2013-06-17 17:14:39 -04004905 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304906 * If our size started in the middle of a block we need to zero out the
4907 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004908 * expose stale data.
4909 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304910 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004911 if (err)
4912 return err;
4913
Yan Zheng9036c102008-10-30 14:19:41 -04004914 if (size <= hole_start)
4915 return 0;
4916
Yan Zheng9036c102008-10-30 14:19:41 -04004917 while (1) {
4918 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004919
David Sterbaff13db42015-12-03 14:30:40 +01004920 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004921 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004922 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4923 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004924 if (!ordered)
4925 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004926 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4927 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004928 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004929 btrfs_put_ordered_extent(ordered);
4930 }
4931
Yan Zheng9036c102008-10-30 14:19:41 -04004932 cur_offset = hole_start;
4933 while (1) {
4934 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4935 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004936 if (IS_ERR(em)) {
4937 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004938 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004939 break;
4940 }
Yan Zheng9036c102008-10-30 14:19:41 -04004941 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004942 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004943 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004944 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004945 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004946
Josef Bacik16e75492013-10-22 12:18:51 -04004947 err = maybe_insert_hole(root, inode, cur_offset,
4948 hole_size);
4949 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004950 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004951 btrfs_drop_extent_cache(inode, cur_offset,
4952 cur_offset + hole_size - 1, 0);
4953 hole_em = alloc_extent_map();
4954 if (!hole_em) {
4955 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4956 &BTRFS_I(inode)->runtime_flags);
4957 goto next;
4958 }
4959 hole_em->start = cur_offset;
4960 hole_em->len = hole_size;
4961 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004962
Josef Bacik5dc562c2012-08-17 13:14:17 -04004963 hole_em->block_start = EXTENT_MAP_HOLE;
4964 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004965 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004966 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004967 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004968 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004969 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004970
4971 while (1) {
4972 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004973 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004974 write_unlock(&em_tree->lock);
4975 if (err != -EEXIST)
4976 break;
4977 btrfs_drop_extent_cache(inode, cur_offset,
4978 cur_offset +
4979 hole_size - 1, 0);
4980 }
4981 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004982 }
Josef Bacik16e75492013-10-22 12:18:51 -04004983next:
Yan Zheng9036c102008-10-30 14:19:41 -04004984 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004985 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004986 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004987 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004988 break;
4989 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004990 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004991 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4992 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004993 return err;
4994}
4995
Eric Sandeen3972f262013-01-12 02:57:22 +00004996static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004997{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004998 struct btrfs_root *root = BTRFS_I(inode)->root;
4999 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005000 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005001 loff_t newsize = attr->ia_size;
5002 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005003 int ret;
5004
Eric Sandeen3972f262013-01-12 02:57:22 +00005005 /*
5006 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5007 * special case where we need to update the times despite not having
5008 * these flags set. For all other operations the VFS set these flags
5009 * explicitly if it wants a timestamp update.
5010 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005011 if (newsize != oldsize) {
5012 inode_inc_iversion(inode);
5013 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5014 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005015 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005016 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005017
Josef Bacika41ad392011-01-31 15:30:16 -05005018 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005019 /*
5020 * Don't do an expanding truncate while snapshoting is ongoing.
5021 * This is to ensure the snapshot captures a fully consistent
5022 * state of this file - if the snapshot captures this expanding
5023 * truncation, it must capture all writes that happened before
5024 * this truncation.
5025 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005026 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005027 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005028 if (ret) {
5029 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005030 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005031 }
Yan, Zheng80825102009-11-12 09:35:36 +00005032
Miao Xief4a2f4c2011-12-14 20:12:01 -05005033 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005034 if (IS_ERR(trans)) {
5035 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005036 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005037 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005038
5039 i_size_write(inode, newsize);
5040 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305041 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005042 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005043 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005044 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005045 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005046
Josef Bacika41ad392011-01-31 15:30:16 -05005047 /*
5048 * We're truncating a file that used to have good data down to
5049 * zero. Make sure it gets into the ordered flush list so that
5050 * any new writes get down to disk quickly.
5051 */
5052 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005053 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5054 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005055
Josef Bacikf3fe8202013-01-07 17:03:21 -05005056 /*
5057 * 1 for the orphan item we're going to add
5058 * 1 for the orphan item deletion.
5059 */
5060 trans = btrfs_start_transaction(root, 2);
5061 if (IS_ERR(trans))
5062 return PTR_ERR(trans);
5063
5064 /*
5065 * We need to do this in case we fail at _any_ point during the
5066 * actual truncate. Once we do the truncate_setsize we could
5067 * invalidate pages which forces any outstanding ordered io to
5068 * be instantly completed which will give us extents that need
5069 * to be truncated. If we fail to get an orphan inode down we
5070 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005071 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005072 * will be consistent.
5073 */
5074 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005075 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005076 if (ret)
5077 return ret;
5078
Josef Bacika41ad392011-01-31 15:30:16 -05005079 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5080 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005081
5082 /* Disable nonlocked read DIO to avoid the end less truncate */
5083 btrfs_inode_block_unlocked_dio(inode);
5084 inode_dio_wait(inode);
5085 btrfs_inode_resume_unlocked_dio(inode);
5086
Josef Bacika41ad392011-01-31 15:30:16 -05005087 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005088 if (ret && inode->i_nlink) {
5089 int err;
5090
Liu Bo19fd2df2016-12-01 13:01:02 -08005091 /* To get a stable disk_i_size */
5092 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5093 if (err) {
5094 btrfs_orphan_del(NULL, inode);
5095 return err;
5096 }
5097
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005098 /*
5099 * failed to truncate, disk_i_size is only adjusted down
5100 * as we remove extents, so it should represent the true
5101 * size of the inode, so reset the in memory size and
5102 * delete our orphan entry.
5103 */
5104 trans = btrfs_join_transaction(root);
5105 if (IS_ERR(trans)) {
5106 btrfs_orphan_del(NULL, inode);
5107 return ret;
5108 }
5109 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5110 err = btrfs_orphan_del(trans, inode);
5111 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005112 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005113 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005114 }
Yan, Zheng80825102009-11-12 09:35:36 +00005115 }
5116
Josef Bacika41ad392011-01-31 15:30:16 -05005117 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005118}
5119
Chris Mason39279cc2007-06-12 06:35:45 -04005120static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5121{
David Howells2b0143b2015-03-17 22:25:59 +00005122 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005123 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005124 int err;
5125
Li Zefanb83cc962010-12-20 16:04:08 +08005126 if (btrfs_root_readonly(root))
5127 return -EROFS;
5128
Jan Kara31051c82016-05-26 16:55:18 +02005129 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005130 if (err)
5131 return err;
5132
Chris Mason5a3f23d2009-03-31 13:27:11 -04005133 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005134 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005135 if (err)
5136 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005137 }
Yan Zheng9036c102008-10-30 14:19:41 -04005138
Christoph Hellwig10257742010-06-04 11:30:02 +02005139 if (attr->ia_valid) {
5140 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005141 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005142 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005143
Josef Bacik22c44fe2011-11-30 10:45:38 -05005144 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005145 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005146 }
5147
Chris Mason39279cc2007-06-12 06:35:45 -04005148 return err;
5149}
Chris Mason61295eb2008-01-14 16:24:38 -05005150
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005151/*
5152 * While truncating the inode pages during eviction, we get the VFS calling
5153 * btrfs_invalidatepage() against each page of the inode. This is slow because
5154 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5155 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5156 * extent_state structures over and over, wasting lots of time.
5157 *
5158 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5159 * those expensive operations on a per page basis and do only the ordered io
5160 * finishing, while we release here the extent_map and extent_state structures,
5161 * without the excessive merging and splitting.
5162 */
5163static void evict_inode_truncate_pages(struct inode *inode)
5164{
5165 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5166 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5167 struct rb_node *node;
5168
5169 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005170 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005171
5172 write_lock(&map_tree->lock);
5173 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5174 struct extent_map *em;
5175
5176 node = rb_first(&map_tree->map);
5177 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005178 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5179 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005180 remove_extent_mapping(map_tree, em);
5181 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005182 if (need_resched()) {
5183 write_unlock(&map_tree->lock);
5184 cond_resched();
5185 write_lock(&map_tree->lock);
5186 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005187 }
5188 write_unlock(&map_tree->lock);
5189
Filipe Manana6ca07092015-05-26 00:55:42 +01005190 /*
5191 * Keep looping until we have no more ranges in the io tree.
5192 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005193 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5194 * still in progress (unlocked the pages in the bio but did not yet
5195 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005196 * ranges can still be locked and eviction started because before
5197 * submitting those bios, which are executed by a separate task (work
5198 * queue kthread), inode references (inode->i_count) were not taken
5199 * (which would be dropped in the end io callback of each bio).
5200 * Therefore here we effectively end up waiting for those bios and
5201 * anyone else holding locked ranges without having bumped the inode's
5202 * reference count - if we don't do it, when they access the inode's
5203 * io_tree to unlock a range it may be too late, leading to an
5204 * use-after-free issue.
5205 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005206 spin_lock(&io_tree->lock);
5207 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5208 struct extent_state *state;
5209 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005210 u64 start;
5211 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005212
5213 node = rb_first(&io_tree->state);
5214 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005215 start = state->start;
5216 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005217 spin_unlock(&io_tree->lock);
5218
David Sterbaff13db42015-12-03 14:30:40 +01005219 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005220
5221 /*
5222 * If still has DELALLOC flag, the extent didn't reach disk,
5223 * and its reserved space won't be freed by delayed_ref.
5224 * So we need to free its reserved space here.
5225 * (Refer to comment in btrfs_invalidatepage, case 2)
5226 *
5227 * Note, end is the bytenr of last byte, so we need + 1 here.
5228 */
5229 if (state->state & EXTENT_DELALLOC)
5230 btrfs_qgroup_free_data(inode, start, end - start + 1);
5231
Filipe Manana6ca07092015-05-26 00:55:42 +01005232 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005233 EXTENT_LOCKED | EXTENT_DIRTY |
5234 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5235 EXTENT_DEFRAG, 1, 1,
5236 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005237
Filipe Manana7064dd52014-08-08 02:47:05 +01005238 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005239 spin_lock(&io_tree->lock);
5240 }
5241 spin_unlock(&io_tree->lock);
5242}
5243
Al Virobd555972010-06-07 11:35:40 -04005244void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005245{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005246 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005247 struct btrfs_trans_handle *trans;
5248 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005249 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005250 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005251 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005252 int ret;
5253
liubo1abe9b82011-03-24 11:18:59 +00005254 trace_btrfs_inode_evict(inode);
5255
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005256 if (!root) {
5257 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5258 return;
5259 }
5260
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005261 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005262
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005263 evict_inode_truncate_pages(inode);
5264
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005265 if (inode->i_nlink &&
5266 ((btrfs_root_refs(&root->root_item) != 0 &&
5267 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5268 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005269 goto no_delete;
5270
Chris Mason39279cc2007-06-12 06:35:45 -04005271 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005272 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005273 goto no_delete;
5274 }
Al Virobd555972010-06-07 11:35:40 -04005275 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005276 if (!special_file(inode->i_mode))
5277 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005278
Miao Xief6124962014-09-12 18:44:04 +08005279 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5280
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005281 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005282 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005283 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005284 goto no_delete;
5285 }
5286
Yan, Zheng76dda932009-09-21 16:00:26 -04005287 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005288 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5289 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005290 goto no_delete;
5291 }
5292
Nikolay Borisovaa790212017-01-10 20:35:40 +02005293 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005294 if (ret) {
5295 btrfs_orphan_del(NULL, inode);
5296 goto no_delete;
5297 }
5298
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005299 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005300 if (!rsv) {
5301 btrfs_orphan_del(NULL, inode);
5302 goto no_delete;
5303 }
Josef Bacik4a338542011-08-29 11:01:31 -04005304 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005305 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005306 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005307
Chris Masondbe674a2008-07-17 12:54:05 -04005308 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005309
Josef Bacik4289a662011-08-05 13:22:24 -04005310 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005311 * This is a bit simpler than btrfs_truncate since we've already
5312 * reserved our space for our orphan item in the unlink, so we just
5313 * need to reserve some slack space in case we add bytes and update
5314 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005315 */
Yan, Zheng80825102009-11-12 09:35:36 +00005316 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005317 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5318 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005319
Josef Bacik726c35f2011-09-26 15:46:06 -04005320 /*
5321 * Try and steal from the global reserve since we will
5322 * likely not use this space anyway, we want to try as
5323 * hard as possible to get this to work.
5324 */
5325 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005326 steal_from_global++;
5327 else
5328 steal_from_global = 0;
5329 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005330
Josef Bacik3bce8762015-02-24 12:35:51 -08005331 /*
5332 * steal_from_global == 0: we reserved stuff, hooray!
5333 * steal_from_global == 1: we didn't reserve stuff, boo!
5334 * steal_from_global == 2: we've committed, still not a lot of
5335 * room but maybe we'll have room in the global reserve this
5336 * time.
5337 * steal_from_global == 3: abandon all hope!
5338 */
5339 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005340 btrfs_warn(fs_info,
5341 "Could not get space for a delete, will truncate on mount %d",
5342 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005343 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005344 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005345 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005346 }
5347
Miao Xie0e8c36a2012-12-19 06:59:51 +00005348 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005349 if (IS_ERR(trans)) {
5350 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005351 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005352 goto no_delete;
5353 }
5354
Josef Bacik3bce8762015-02-24 12:35:51 -08005355 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005356 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005357 * sure there is room to do it, if not we need to commit and try
5358 * again.
5359 */
5360 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005361 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005362 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005363 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005364 else
5365 ret = -ENOSPC;
5366 }
5367
5368 /*
5369 * Couldn't steal from the global reserve, we have too much
5370 * pending stuff built up, commit the transaction and try it
5371 * again.
5372 */
5373 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005374 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005375 if (ret) {
5376 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005377 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005378 goto no_delete;
5379 }
5380 continue;
5381 } else {
5382 steal_from_global = 0;
5383 }
5384
Josef Bacik4289a662011-08-05 13:22:24 -04005385 trans->block_rsv = rsv;
5386
Yan, Zhengd68fc572010-05-16 10:49:58 -04005387 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005388 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005389 break;
5390
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005391 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005392 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005393 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005394 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005395 }
5396
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005397 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005398
Josef Bacik4ef31a42013-08-13 14:10:08 -04005399 /*
5400 * Errors here aren't a big deal, it just means we leave orphan items
5401 * in the tree. They will be cleaned up on the next mount.
5402 */
Yan, Zheng80825102009-11-12 09:35:36 +00005403 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005404 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005405 btrfs_orphan_del(trans, inode);
5406 } else {
5407 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005408 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005409
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005410 trans->block_rsv = &fs_info->trans_block_rsv;
5411 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005412 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005413 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005414
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005415 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005416 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005417no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005418 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005419 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005420}
5421
5422/*
5423 * this returns the key found in the dir entry in the location pointer.
5424 * If no dir entries were found, location->objectid is 0.
5425 */
5426static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5427 struct btrfs_key *location)
5428{
5429 const char *name = dentry->d_name.name;
5430 int namelen = dentry->d_name.len;
5431 struct btrfs_dir_item *di;
5432 struct btrfs_path *path;
5433 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005434 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005435
5436 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005437 if (!path)
5438 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005439
David Sterbaf85b7372017-01-20 14:54:07 +01005440 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5441 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005442 if (IS_ERR(di))
5443 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005444
David Sterbac7040052011-04-19 18:00:01 +02005445 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005446 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005447
Chris Mason5f39d392007-10-15 16:14:19 -04005448 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005449out:
Chris Mason39279cc2007-06-12 06:35:45 -04005450 btrfs_free_path(path);
5451 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005452out_err:
5453 location->objectid = 0;
5454 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005455}
5456
5457/*
5458 * when we hit a tree root in a directory, the btrfs part of the inode
5459 * needs to be changed to reflect the root directory of the tree root. This
5460 * is kind of like crossing a mount point.
5461 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005462static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005463 struct inode *dir,
5464 struct dentry *dentry,
5465 struct btrfs_key *location,
5466 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005467{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005468 struct btrfs_path *path;
5469 struct btrfs_root *new_root;
5470 struct btrfs_root_ref *ref;
5471 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005472 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005473 int ret;
5474 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005475
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005476 path = btrfs_alloc_path();
5477 if (!path) {
5478 err = -ENOMEM;
5479 goto out;
5480 }
Chris Mason39279cc2007-06-12 06:35:45 -04005481
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005482 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005483 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5484 key.type = BTRFS_ROOT_REF_KEY;
5485 key.offset = location->objectid;
5486
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005487 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005488 if (ret) {
5489 if (ret < 0)
5490 err = ret;
5491 goto out;
5492 }
Chris Mason39279cc2007-06-12 06:35:45 -04005493
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005494 leaf = path->nodes[0];
5495 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005496 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005497 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5498 goto out;
5499
5500 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5501 (unsigned long)(ref + 1),
5502 dentry->d_name.len);
5503 if (ret)
5504 goto out;
5505
David Sterbab3b4aa72011-04-21 01:20:15 +02005506 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005507
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005508 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005509 if (IS_ERR(new_root)) {
5510 err = PTR_ERR(new_root);
5511 goto out;
5512 }
5513
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 *sub_root = new_root;
5515 location->objectid = btrfs_root_dirid(&new_root->root_item);
5516 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005517 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518 err = 0;
5519out:
5520 btrfs_free_path(path);
5521 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005522}
5523
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005524static void inode_tree_add(struct inode *inode)
5525{
5526 struct btrfs_root *root = BTRFS_I(inode)->root;
5527 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005528 struct rb_node **p;
5529 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005530 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005531 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005532
Al Viro1d3382cb2010-10-23 15:19:20 -04005533 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005534 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005535 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005536 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005537 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005538 while (*p) {
5539 parent = *p;
5540 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5541
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005542 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005543 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005544 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005545 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005546 else {
5547 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005548 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005549 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005550 RB_CLEAR_NODE(parent);
5551 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005552 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005553 }
5554 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005555 rb_link_node(new, parent, p);
5556 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005557 spin_unlock(&root->inode_lock);
5558}
5559
5560static void inode_tree_del(struct inode *inode)
5561{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005562 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005563 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005564 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005565
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005566 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005567 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005568 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005569 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005570 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005571 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005572 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005573
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005574 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005575 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005576 spin_lock(&root->inode_lock);
5577 empty = RB_EMPTY_ROOT(&root->inode_tree);
5578 spin_unlock(&root->inode_lock);
5579 if (empty)
5580 btrfs_add_dead_root(root);
5581 }
5582}
5583
Jeff Mahoney143bede2012-03-01 14:56:26 +01005584void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005585{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005586 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005587 struct rb_node *node;
5588 struct rb_node *prev;
5589 struct btrfs_inode *entry;
5590 struct inode *inode;
5591 u64 objectid = 0;
5592
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005593 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005594 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005595
5596 spin_lock(&root->inode_lock);
5597again:
5598 node = root->inode_tree.rb_node;
5599 prev = NULL;
5600 while (node) {
5601 prev = node;
5602 entry = rb_entry(node, struct btrfs_inode, rb_node);
5603
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005604 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005605 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005606 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005607 node = node->rb_right;
5608 else
5609 break;
5610 }
5611 if (!node) {
5612 while (prev) {
5613 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005614 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005615 node = prev;
5616 break;
5617 }
5618 prev = rb_next(prev);
5619 }
5620 }
5621 while (node) {
5622 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005623 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005624 inode = igrab(&entry->vfs_inode);
5625 if (inode) {
5626 spin_unlock(&root->inode_lock);
5627 if (atomic_read(&inode->i_count) > 1)
5628 d_prune_aliases(inode);
5629 /*
Al Viro45321ac2010-06-07 13:43:19 -04005630 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005631 * the inode cache when its usage count
5632 * hits zero.
5633 */
5634 iput(inode);
5635 cond_resched();
5636 spin_lock(&root->inode_lock);
5637 goto again;
5638 }
5639
5640 if (cond_resched_lock(&root->inode_lock))
5641 goto again;
5642
5643 node = rb_next(node);
5644 }
5645 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005646}
5647
Chris Masone02119d2008-09-05 16:13:11 -04005648static int btrfs_init_locked_inode(struct inode *inode, void *p)
5649{
5650 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005651 inode->i_ino = args->location->objectid;
5652 memcpy(&BTRFS_I(inode)->location, args->location,
5653 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005654 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005655 return 0;
5656}
5657
5658static int btrfs_find_actor(struct inode *inode, void *opaque)
5659{
5660 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005661 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005662 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005663}
5664
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005665static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005666 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005667 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005668{
5669 struct inode *inode;
5670 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005671 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005672
Chris Mason90d3e592014-01-09 17:28:00 -08005673 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005674 args.root = root;
5675
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005676 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005677 btrfs_init_locked_inode,
5678 (void *)&args);
5679 return inode;
5680}
5681
Balaji Rao1a54ef82008-07-21 02:01:04 +05305682/* Get an inode object given its location and corresponding root.
5683 * Returns in *is_new if the inode was read from disk
5684 */
5685struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005686 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305687{
5688 struct inode *inode;
5689
Chris Mason90d3e592014-01-09 17:28:00 -08005690 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305691 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005692 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305693
5694 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005695 int ret;
5696
5697 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005698 if (!is_bad_inode(inode)) {
5699 inode_tree_add(inode);
5700 unlock_new_inode(inode);
5701 if (new)
5702 *new = 1;
5703 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005704 unlock_new_inode(inode);
5705 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005706 ASSERT(ret < 0);
5707 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005708 }
5709 }
5710
Balaji Rao1a54ef82008-07-21 02:01:04 +05305711 return inode;
5712}
5713
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005714static struct inode *new_simple_dir(struct super_block *s,
5715 struct btrfs_key *key,
5716 struct btrfs_root *root)
5717{
5718 struct inode *inode = new_inode(s);
5719
5720 if (!inode)
5721 return ERR_PTR(-ENOMEM);
5722
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005723 BTRFS_I(inode)->root = root;
5724 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005725 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005726
5727 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005728 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005729 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005730 inode->i_fop = &simple_dir_operations;
5731 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005732 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305733 inode->i_atime = inode->i_mtime;
5734 inode->i_ctime = inode->i_mtime;
5735 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005736
5737 return inode;
5738}
5739
Chris Mason3de45862008-11-17 21:02:50 -05005740struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005741{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005742 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005743 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005744 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005745 struct btrfs_root *sub_root = root;
5746 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005747 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005748 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005749
5750 if (dentry->d_name.len > BTRFS_NAME_LEN)
5751 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005752
Jeff Layton39e3c952012-11-28 11:30:53 -05005753 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005754 if (ret < 0)
5755 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005756
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005757 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005758 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005759
5760 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005761 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005762 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005763 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005764
5765 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5766
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005767 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005768 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005769 &location, &sub_root);
5770 if (ret < 0) {
5771 if (ret != -ENOENT)
5772 inode = ERR_PTR(ret);
5773 else
5774 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5775 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005776 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005777 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005778 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005779
Julia Lawall34d19ba2011-01-24 19:55:19 +00005780 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005781 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005782 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005783 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005784 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005785 if (ret) {
5786 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005787 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005788 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005789 }
5790
Chris Mason3de45862008-11-17 21:02:50 -05005791 return inode;
5792}
5793
Nick Pigginfe15ce42011-01-07 17:49:23 +11005794static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005795{
5796 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005797 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005798
Li Zefan848cce02012-02-21 17:04:28 +08005799 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005800 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005801
Li Zefan848cce02012-02-21 17:04:28 +08005802 if (inode) {
5803 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005804 if (btrfs_root_refs(&root->root_item) == 0)
5805 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005806
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005807 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005808 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005809 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005810 return 0;
5811}
5812
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005813static void btrfs_dentry_release(struct dentry *dentry)
5814{
Daeseok Youn944a4512014-04-14 15:37:02 +09005815 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005816}
5817
Chris Mason3de45862008-11-17 21:02:50 -05005818static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005819 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005820{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005821 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005822
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005823 inode = btrfs_lookup_dentry(dir, dentry);
5824 if (IS_ERR(inode)) {
5825 if (PTR_ERR(inode) == -ENOENT)
5826 inode = NULL;
5827 else
5828 return ERR_CAST(inode);
5829 }
5830
Al Viro41d28bc2014-10-12 22:24:21 -04005831 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005832}
5833
Miao Xie16cdcec2011-04-22 18:12:22 +08005834unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005835 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5836};
5837
Al Viro9cdda8d2013-05-22 16:48:09 -04005838static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005839{
Al Viro9cdda8d2013-05-22 16:48:09 -04005840 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005841 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005842 struct btrfs_root *root = BTRFS_I(inode)->root;
5843 struct btrfs_item *item;
5844 struct btrfs_dir_item *di;
5845 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005846 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005847 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005848 struct list_head ins_list;
5849 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005850 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005851 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005852 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005853 unsigned char d_type;
5854 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005855 char tmp_name[32];
5856 char *name_ptr;
5857 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005858 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005859 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005860
Al Viro9cdda8d2013-05-22 16:48:09 -04005861 if (!dir_emit_dots(file, ctx))
5862 return 0;
5863
David Woodhouse49593bf2008-08-17 17:08:36 +01005864 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005865 if (!path)
5866 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005867
David Sterbae4058b52015-11-27 16:31:35 +01005868 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005869
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005870 INIT_LIST_HEAD(&ins_list);
5871 INIT_LIST_HEAD(&del_list);
5872 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005873
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005874 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005875 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005876 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005877
Chris Mason39279cc2007-06-12 06:35:45 -04005878 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5879 if (ret < 0)
5880 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005881
5882 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005883 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005884 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005885 if (slot >= btrfs_header_nritems(leaf)) {
5886 ret = btrfs_next_leaf(root, path);
5887 if (ret < 0)
5888 goto err;
5889 else if (ret > 0)
5890 break;
5891 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005892 }
Chris Mason3de45862008-11-17 21:02:50 -05005893
Ross Kirkdd3cc162013-09-16 15:58:09 +01005894 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005895 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5896
5897 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005898 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005899 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005900 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005901 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005902 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005903 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005904 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005905
Al Viro9cdda8d2013-05-22 16:48:09 -04005906 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005907
Chris Mason39279cc2007-06-12 06:35:45 -04005908 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005909 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005910 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005911
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005912 name_len = btrfs_dir_name_len(leaf, di);
5913 if (name_len <= sizeof(tmp_name)) {
5914 name_ptr = tmp_name;
5915 } else {
5916 name_ptr = kmalloc(name_len, GFP_KERNEL);
5917 if (!name_ptr) {
5918 ret = -ENOMEM;
5919 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005920 }
Chris Mason39279cc2007-06-12 06:35:45 -04005921 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005922 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5923 name_len);
5924
5925 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5926 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5927
5928 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5929 d_type);
5930
5931 if (name_ptr != tmp_name)
5932 kfree(name_ptr);
5933
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005934 if (over)
5935 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005936 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005937next:
5938 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005939 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005940
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005941 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005942 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005943 goto nopos;
5944
Zach Browndb62efb2013-07-11 16:19:42 -07005945 /*
5946 * Stop new entries from being returned after we return the last
5947 * entry.
5948 *
5949 * New directory entries are assigned a strictly increasing
5950 * offset. This means that new entries created during readdir
5951 * are *guaranteed* to be seen in the future by that readdir.
5952 * This has broken buggy programs which operate on names as
5953 * they're returned by readdir. Until we re-use freed offsets
5954 * we have this hack to stop new entries from being returned
5955 * under the assumption that they'll never reach this huge
5956 * offset.
5957 *
5958 * This is being careful not to overflow 32bit loff_t unless the
5959 * last entry requires it because doing so has broken 32bit apps
5960 * in the past.
5961 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005962 if (ctx->pos >= INT_MAX)
5963 ctx->pos = LLONG_MAX;
5964 else
5965 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005966nopos:
5967 ret = 0;
5968err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005969 if (put)
5970 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005971 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005972 return ret;
5973}
5974
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005975int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005976{
5977 struct btrfs_root *root = BTRFS_I(inode)->root;
5978 struct btrfs_trans_handle *trans;
5979 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005980 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005981
Josef Bacik72ac3c02012-05-23 14:13:11 -04005982 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005983 return 0;
5984
Liu Bo83eea1f2012-07-10 05:28:39 -06005985 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005986 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005987
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005988 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005989 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005990 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005991 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005992 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005993 if (IS_ERR(trans))
5994 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005995 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005996 }
5997 return ret;
5998}
5999
6000/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006001 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006002 * inode changes. But, it is most likely to find the inode in cache.
6003 * FIXME, needs more benchmarking...there are no reasons other than performance
6004 * to keep or drop this code.
6005 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006006static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006007{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006008 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006009 struct btrfs_root *root = BTRFS_I(inode)->root;
6010 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006011 int ret;
6012
Josef Bacik72ac3c02012-05-23 14:13:11 -04006013 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006014 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006015
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006016 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006017 if (IS_ERR(trans))
6018 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006019
6020 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006021 if (ret && ret == -ENOSPC) {
6022 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006023 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006024 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006025 if (IS_ERR(trans))
6026 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006027
Chris Mason94b60442010-05-26 11:02:00 -04006028 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006029 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006030 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006031 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006032 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006033
6034 return ret;
6035}
6036
6037/*
6038 * This is a copy of file_update_time. We need this so we can return error on
6039 * ENOSPC for updating the inode in the case of file write and mmap writes.
6040 */
Josef Bacike41f9412012-03-26 09:46:47 -04006041static int btrfs_update_time(struct inode *inode, struct timespec *now,
6042 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006043{
Alexander Block2bc5565282012-06-15 09:49:33 +02006044 struct btrfs_root *root = BTRFS_I(inode)->root;
6045
6046 if (btrfs_root_readonly(root))
6047 return -EROFS;
6048
Josef Bacike41f9412012-03-26 09:46:47 -04006049 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006050 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006051 if (flags & S_CTIME)
6052 inode->i_ctime = *now;
6053 if (flags & S_MTIME)
6054 inode->i_mtime = *now;
6055 if (flags & S_ATIME)
6056 inode->i_atime = *now;
6057 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006058}
6059
Chris Masond352ac62008-09-29 15:18:18 -04006060/*
6061 * find the highest existing sequence number in a directory
6062 * and then set the in-memory index_cnt variable to reflect
6063 * free sequence numbers
6064 */
Josef Bacikaec74772008-07-24 12:12:38 -04006065static int btrfs_set_inode_index_count(struct inode *inode)
6066{
6067 struct btrfs_root *root = BTRFS_I(inode)->root;
6068 struct btrfs_key key, found_key;
6069 struct btrfs_path *path;
6070 struct extent_buffer *leaf;
6071 int ret;
6072
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006073 key.objectid = btrfs_ino(BTRFS_I(inode));
David Sterba962a2982014-06-04 18:41:45 +02006074 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006075 key.offset = (u64)-1;
6076
6077 path = btrfs_alloc_path();
6078 if (!path)
6079 return -ENOMEM;
6080
6081 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6082 if (ret < 0)
6083 goto out;
6084 /* FIXME: we should be able to handle this */
6085 if (ret == 0)
6086 goto out;
6087 ret = 0;
6088
6089 /*
6090 * MAGIC NUMBER EXPLANATION:
6091 * since we search a directory based on f_pos we have to start at 2
6092 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6093 * else has to start at 2
6094 */
6095 if (path->slots[0] == 0) {
6096 BTRFS_I(inode)->index_cnt = 2;
6097 goto out;
6098 }
6099
6100 path->slots[0]--;
6101
6102 leaf = path->nodes[0];
6103 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6104
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006105 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
David Sterba962a2982014-06-04 18:41:45 +02006106 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006107 BTRFS_I(inode)->index_cnt = 2;
6108 goto out;
6109 }
6110
6111 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6112out:
6113 btrfs_free_path(path);
6114 return ret;
6115}
6116
Chris Masond352ac62008-09-29 15:18:18 -04006117/*
6118 * helper to find a free sequence number in a given directory. This current
6119 * code is very simple, later versions will do smarter things in the btree
6120 */
Chris Mason3de45862008-11-17 21:02:50 -05006121int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006122{
6123 int ret = 0;
6124
6125 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Nikolay Borisovf5cc7b82017-01-10 20:35:42 +02006126 ret = btrfs_inode_delayed_dir_index_count(BTRFS_I(dir));
Miao Xie16cdcec2011-04-22 18:12:22 +08006127 if (ret) {
6128 ret = btrfs_set_inode_index_count(dir);
6129 if (ret)
6130 return ret;
6131 }
Josef Bacikaec74772008-07-24 12:12:38 -04006132 }
6133
Chris Mason00e4e6b2008-08-05 11:18:09 -04006134 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006135 BTRFS_I(dir)->index_cnt++;
6136
6137 return ret;
6138}
6139
Chris Masonb0d5d102014-09-08 13:08:51 -07006140static int btrfs_insert_inode_locked(struct inode *inode)
6141{
6142 struct btrfs_iget_args args;
6143 args.location = &BTRFS_I(inode)->location;
6144 args.root = BTRFS_I(inode)->root;
6145
6146 return insert_inode_locked4(inode,
6147 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6148 btrfs_find_actor, &args);
6149}
6150
Chris Mason39279cc2007-06-12 06:35:45 -04006151static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6152 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006153 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006154 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006155 u64 ref_objectid, u64 objectid,
6156 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006157{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006158 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006159 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006160 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006161 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006162 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006163 struct btrfs_inode_ref *ref;
6164 struct btrfs_key key[2];
6165 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006166 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006167 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006168 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006169
Chris Mason5f39d392007-10-15 16:14:19 -04006170 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006171 if (!path)
6172 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006173
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006174 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006175 if (!inode) {
6176 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006177 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006178 }
Chris Mason39279cc2007-06-12 06:35:45 -04006179
Li Zefan581bb052011-04-20 10:06:11 +08006180 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006181 * O_TMPFILE, set link count to 0, so that after this point,
6182 * we fill in an inode item with the correct link count.
6183 */
6184 if (!name)
6185 set_nlink(inode, 0);
6186
6187 /*
Li Zefan581bb052011-04-20 10:06:11 +08006188 * we have to initialize this early, so we can reclaim the inode
6189 * number if we fail afterwards in this function.
6190 */
6191 inode->i_ino = objectid;
6192
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006193 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006194 trace_btrfs_inode_request(dir);
6195
Chris Mason3de45862008-11-17 21:02:50 -05006196 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006197 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006198 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006199 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006200 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006201 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006202 } else if (dir) {
6203 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006204 }
6205 /*
6206 * index_cnt is ignored for everything but a dir,
6207 * btrfs_get_inode_index_count has an explanation for the magic
6208 * number
6209 */
6210 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006211 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006212 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006213 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006214 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006215
Josef Bacik5dc562c2012-08-17 13:14:17 -04006216 /*
6217 * We could have gotten an inode number from somebody who was fsynced
6218 * and then removed in this same transaction, so let's just set full
6219 * sync since it will be a full sync anyway and this will blow away the
6220 * old info in the log.
6221 */
6222 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6223
Chris Mason9c583092008-01-29 15:15:18 -05006224 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006225 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006226 key[0].offset = 0;
6227
Chris Mason9c583092008-01-29 15:15:18 -05006228 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006229
6230 if (name) {
6231 /*
6232 * Start new inodes with an inode_ref. This is slightly more
6233 * efficient for small numbers of hard links since they will
6234 * be packed into one item. Extended refs will kick in if we
6235 * add more hard links than can fit in the ref item.
6236 */
6237 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006238 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006239 key[1].offset = ref_objectid;
6240
6241 sizes[1] = name_len + sizeof(*ref);
6242 }
Chris Mason9c583092008-01-29 15:15:18 -05006243
Chris Masonb0d5d102014-09-08 13:08:51 -07006244 location = &BTRFS_I(inode)->location;
6245 location->objectid = objectid;
6246 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006247 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006248
6249 ret = btrfs_insert_inode_locked(inode);
6250 if (ret < 0)
6251 goto fail;
6252
Chris Masonb9473432009-03-13 11:00:37 -04006253 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006254 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006255 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006256 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006257
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006258 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006259 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306260
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006261 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306262 inode->i_atime = inode->i_mtime;
6263 inode->i_ctime = inode->i_mtime;
6264 BTRFS_I(inode)->i_otime = inode->i_mtime;
6265
Chris Mason5f39d392007-10-15 16:14:19 -04006266 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6267 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006268 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006269 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006270 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006271
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006272 if (name) {
6273 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6274 struct btrfs_inode_ref);
6275 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6276 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6277 ptr = (unsigned long)(ref + 1);
6278 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6279 }
Chris Mason9c583092008-01-29 15:15:18 -05006280
Chris Mason5f39d392007-10-15 16:14:19 -04006281 btrfs_mark_buffer_dirty(path->nodes[0]);
6282 btrfs_free_path(path);
6283
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006284 btrfs_inherit_iflags(inode, dir);
6285
Al Viro569254b2011-07-24 17:08:40 -04006286 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006287 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006288 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006289 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006290 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6291 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006292 }
6293
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006294 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006295
6296 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006297 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006298
Alexander Block8ea05e32012-07-25 17:35:53 +02006299 btrfs_update_root_times(trans, root);
6300
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006301 ret = btrfs_inode_inherit_props(trans, inode, dir);
6302 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006303 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006304 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006305 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006306
Chris Mason39279cc2007-06-12 06:35:45 -04006307 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006308
6309fail_unlock:
6310 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006311fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006312 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006313 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006314 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006315 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006316 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006317}
6318
6319static inline u8 btrfs_inode_type(struct inode *inode)
6320{
6321 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6322}
6323
Chris Masond352ac62008-09-29 15:18:18 -04006324/*
6325 * utility function to add 'inode' into 'parent_inode' with
6326 * a give name and a given sequence number.
6327 * if 'add_backref' is true, also insert a backref from the
6328 * inode to the parent directory.
6329 */
Chris Masone02119d2008-09-05 16:13:11 -04006330int btrfs_add_link(struct btrfs_trans_handle *trans,
6331 struct inode *parent_inode, struct inode *inode,
6332 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006333{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006334 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006335 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006336 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006337 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006338 u64 ino = btrfs_ino(BTRFS_I(inode));
6339 u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006340
Li Zefan33345d012011-04-20 10:31:50 +08006341 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006342 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6343 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006344 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006345 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006346 key.offset = 0;
6347 }
Chris Mason39279cc2007-06-12 06:35:45 -04006348
Li Zefan33345d012011-04-20 10:31:50 +08006349 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006350 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6351 root->root_key.objectid, parent_ino,
6352 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006353 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006354 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6355 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006356 }
6357
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006358 /* Nothing to clean up yet */
6359 if (ret)
6360 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006361
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006362 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6363 parent_inode, &key,
6364 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006365 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006366 goto fail_dir_item;
6367 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006368 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006369 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006370 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006371
6372 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6373 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006374 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006375 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006376 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006377 ret = btrfs_update_inode(trans, root, parent_inode);
6378 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006379 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006380 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006381
6382fail_dir_item:
6383 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6384 u64 local_index;
6385 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006386 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6387 root->root_key.objectid, parent_ino,
6388 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006389
6390 } else if (add_backref) {
6391 u64 local_index;
6392 int err;
6393
6394 err = btrfs_del_inode_ref(trans, root, name, name_len,
6395 ino, parent_ino, &local_index);
6396 }
6397 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006398}
6399
6400static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006401 struct inode *dir, struct dentry *dentry,
6402 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006403{
Josef Bacika1b075d2010-11-19 20:36:11 +00006404 int err = btrfs_add_link(trans, dir, inode,
6405 dentry->d_name.name, dentry->d_name.len,
6406 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006407 if (err > 0)
6408 err = -EEXIST;
6409 return err;
6410}
6411
Josef Bacik618e21d2007-07-11 10:18:17 -04006412static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006413 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006414{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006415 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006416 struct btrfs_trans_handle *trans;
6417 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006418 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006419 int err;
6420 int drop_inode = 0;
6421 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006422 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006423
Josef Bacik9ed74f22009-09-11 16:12:44 -04006424 /*
6425 * 2 for inode item and ref
6426 * 2 for dir items
6427 * 1 for xattr if selinux is on
6428 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006429 trans = btrfs_start_transaction(root, 5);
6430 if (IS_ERR(trans))
6431 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006432
Li Zefan581bb052011-04-20 10:06:11 +08006433 err = btrfs_find_free_ino(root, &objectid);
6434 if (err)
6435 goto out_unlock;
6436
Josef Bacikaec74772008-07-24 12:12:38 -04006437 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006438 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6439 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006440 if (IS_ERR(inode)) {
6441 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006442 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006443 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006444
Casey Schauflerad19db72011-12-15 10:09:07 -05006445 /*
6446 * If the active LSM wants to access the inode during
6447 * d_instantiate it needs these. Smack checks to see
6448 * if the filesystem supports xattrs by looking at the
6449 * ops vector.
6450 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006451 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006452 init_special_inode(inode, inode->i_mode, rdev);
6453
6454 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006455 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006456 goto out_unlock_inode;
6457
6458 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6459 if (err) {
6460 goto out_unlock_inode;
6461 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006462 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006463 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006464 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006465 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006466
Josef Bacik618e21d2007-07-11 10:18:17 -04006467out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006468 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006469 btrfs_balance_delayed_items(fs_info);
6470 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006471 if (drop_inode) {
6472 inode_dec_link_count(inode);
6473 iput(inode);
6474 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006475 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006476
6477out_unlock_inode:
6478 drop_inode = 1;
6479 unlock_new_inode(inode);
6480 goto out_unlock;
6481
Josef Bacik618e21d2007-07-11 10:18:17 -04006482}
6483
Chris Mason39279cc2007-06-12 06:35:45 -04006484static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006485 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006486{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006487 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006488 struct btrfs_trans_handle *trans;
6489 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006490 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006491 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006492 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006493 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006494 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006495
Josef Bacik9ed74f22009-09-11 16:12:44 -04006496 /*
6497 * 2 for inode item and ref
6498 * 2 for dir items
6499 * 1 for xattr if selinux is on
6500 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006501 trans = btrfs_start_transaction(root, 5);
6502 if (IS_ERR(trans))
6503 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006504
Li Zefan581bb052011-04-20 10:06:11 +08006505 err = btrfs_find_free_ino(root, &objectid);
6506 if (err)
6507 goto out_unlock;
6508
Josef Bacikaec74772008-07-24 12:12:38 -04006509 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006510 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6511 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006512 if (IS_ERR(inode)) {
6513 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006514 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006515 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006516 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006517 /*
6518 * If the active LSM wants to access the inode during
6519 * d_instantiate it needs these. Smack checks to see
6520 * if the filesystem supports xattrs by looking at the
6521 * ops vector.
6522 */
6523 inode->i_fop = &btrfs_file_operations;
6524 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006525 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006526
6527 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6528 if (err)
6529 goto out_unlock_inode;
6530
6531 err = btrfs_update_inode(trans, root, inode);
6532 if (err)
6533 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006534
Josef Bacika1b075d2010-11-19 20:36:11 +00006535 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006536 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006537 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006538
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006539 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006540 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006541 d_instantiate(dentry, inode);
6542
Chris Mason39279cc2007-06-12 06:35:45 -04006543out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006544 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006545 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006546 inode_dec_link_count(inode);
6547 iput(inode);
6548 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006549 btrfs_balance_delayed_items(fs_info);
6550 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006551 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006552
6553out_unlock_inode:
6554 unlock_new_inode(inode);
6555 goto out_unlock;
6556
Chris Mason39279cc2007-06-12 06:35:45 -04006557}
6558
6559static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6560 struct dentry *dentry)
6561{
Filipe Manana271dba452016-01-05 16:24:05 +00006562 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006563 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006564 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006565 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006566 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006567 int err;
6568 int drop_inode = 0;
6569
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006570 /* do not allow sys_link's with other subvols of the same device */
6571 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006572 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006573
Mark Fashehf1863732012-08-08 11:32:27 -07006574 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006575 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006576
Chris Mason3de45862008-11-17 21:02:50 -05006577 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006578 if (err)
6579 goto fail;
6580
Yan, Zhenga22285a2010-05-16 10:48:46 -04006581 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006582 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006583 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006584 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006585 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006586 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006587 if (IS_ERR(trans)) {
6588 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006589 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006590 goto fail;
6591 }
Chris Mason5f39d392007-10-15 16:14:19 -04006592
Miao Xie67de1172013-12-26 13:07:06 +08006593 /* There are several dir indexes for this inode, clear the cache. */
6594 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006595 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006596 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006597 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006598 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006599 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006600
Josef Bacika1b075d2010-11-19 20:36:11 +00006601 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006602
Yan, Zhenga5719522009-09-24 09:17:31 -04006603 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006604 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006605 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006606 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006607 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006608 if (err)
6609 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006610 if (inode->i_nlink == 1) {
6611 /*
6612 * If new hard link count is 1, it's a file created
6613 * with open(2) O_TMPFILE flag.
6614 */
6615 err = btrfs_orphan_del(trans, inode);
6616 if (err)
6617 goto fail;
6618 }
Al Viro08c422c2011-12-23 07:58:13 -05006619 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006620 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006621 }
Chris Mason39279cc2007-06-12 06:35:45 -04006622
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006623 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006624fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006625 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006626 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006627 if (drop_inode) {
6628 inode_dec_link_count(inode);
6629 iput(inode);
6630 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006631 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006632 return err;
6633}
6634
Al Viro18bb1db2011-07-26 01:41:39 -04006635static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006636{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006637 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006638 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006639 struct btrfs_trans_handle *trans;
6640 struct btrfs_root *root = BTRFS_I(dir)->root;
6641 int err = 0;
6642 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006643 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006644 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006645
Josef Bacik9ed74f22009-09-11 16:12:44 -04006646 /*
6647 * 2 items for inode and ref
6648 * 2 items for dir items
6649 * 1 for xattr if selinux is on
6650 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006651 trans = btrfs_start_transaction(root, 5);
6652 if (IS_ERR(trans))
6653 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006654
Li Zefan581bb052011-04-20 10:06:11 +08006655 err = btrfs_find_free_ino(root, &objectid);
6656 if (err)
6657 goto out_fail;
6658
Josef Bacikaec74772008-07-24 12:12:38 -04006659 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006660 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6661 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006662 if (IS_ERR(inode)) {
6663 err = PTR_ERR(inode);
6664 goto out_fail;
6665 }
Chris Mason5f39d392007-10-15 16:14:19 -04006666
Chris Mason39279cc2007-06-12 06:35:45 -04006667 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006668 /* these must be set before we unlock the inode */
6669 inode->i_op = &btrfs_dir_inode_operations;
6670 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006671
Eric Paris2a7dba32011-02-01 11:05:39 -05006672 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006673 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006674 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006675
Chris Masondbe674a2008-07-17 12:54:05 -04006676 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006677 err = btrfs_update_inode(trans, root, inode);
6678 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006679 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006680
Josef Bacika1b075d2010-11-19 20:36:11 +00006681 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6682 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006683 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006684 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006685
Chris Mason39279cc2007-06-12 06:35:45 -04006686 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006687 /*
6688 * mkdir is special. We're unlocking after we call d_instantiate
6689 * to avoid a race with nfsd calling d_instantiate.
6690 */
6691 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006692 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006693
6694out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006695 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006696 if (drop_on_err) {
6697 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006698 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006699 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006700 btrfs_balance_delayed_items(fs_info);
6701 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006702 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006703
6704out_fail_inode:
6705 unlock_new_inode(inode);
6706 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006707}
6708
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006709/* Find next extent map of a given extent map, caller needs to ensure locks */
6710static struct extent_map *next_extent_map(struct extent_map *em)
6711{
6712 struct rb_node *next;
6713
6714 next = rb_next(&em->rb_node);
6715 if (!next)
6716 return NULL;
6717 return container_of(next, struct extent_map, rb_node);
6718}
6719
6720static struct extent_map *prev_extent_map(struct extent_map *em)
6721{
6722 struct rb_node *prev;
6723
6724 prev = rb_prev(&em->rb_node);
6725 if (!prev)
6726 return NULL;
6727 return container_of(prev, struct extent_map, rb_node);
6728}
6729
Chris Masond352ac62008-09-29 15:18:18 -04006730/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006731 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006732 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006733 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006734 */
Chris Mason3b951512008-04-17 11:29:12 -04006735static int merge_extent_mapping(struct extent_map_tree *em_tree,
6736 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006737 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006738 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006739{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006740 struct extent_map *prev;
6741 struct extent_map *next;
6742 u64 start;
6743 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006744 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006745
Chris Masone6dcd2d2008-07-17 12:53:50 -04006746 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006747
6748 if (existing->start > map_start) {
6749 next = existing;
6750 prev = prev_extent_map(next);
6751 } else {
6752 prev = existing;
6753 next = next_extent_map(prev);
6754 }
6755
6756 start = prev ? extent_map_end(prev) : em->start;
6757 start = max_t(u64, start, em->start);
6758 end = next ? next->start : extent_map_end(em);
6759 end = min_t(u64, end, extent_map_end(em));
6760 start_diff = start - em->start;
6761 em->start = start;
6762 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006763 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6764 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006765 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006766 em->block_len -= start_diff;
6767 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006768 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006769}
6770
Chris Masonc8b97812008-10-29 14:49:59 -04006771static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006772 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006773 size_t pg_offset, u64 extent_offset,
6774 struct btrfs_file_extent_item *item)
6775{
6776 int ret;
6777 struct extent_buffer *leaf = path->nodes[0];
6778 char *tmp;
6779 size_t max_size;
6780 unsigned long inline_size;
6781 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006782 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006783
6784 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006785 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006786 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6787 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006788 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006789 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006790 if (!tmp)
6791 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006792 ptr = btrfs_file_extent_inline_start(item);
6793
6794 read_extent_buffer(leaf, tmp, ptr, inline_size);
6795
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006796 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006797 ret = btrfs_decompress(compress_type, tmp, page,
6798 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006799 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006800 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006801}
6802
Chris Masond352ac62008-09-29 15:18:18 -04006803/*
6804 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006805 * the ugly parts come from merging extents from the disk with the in-ram
6806 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006807 * where the in-ram extents might be locked pending data=ordered completion.
6808 *
6809 * This also copies inline extents directly into the page.
6810 */
Chris Masond3977122009-01-05 21:25:51 -05006811
Chris Masona52d9a82007-08-27 16:49:44 -04006812struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006813 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006814 int create)
6815{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006816 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006817 int ret;
6818 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006819 u64 extent_start = 0;
6820 u64 extent_end = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006821 u64 objectid = btrfs_ino(BTRFS_I(inode));
Chris Masona52d9a82007-08-27 16:49:44 -04006822 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006823 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006824 struct btrfs_root *root = BTRFS_I(inode)->root;
6825 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006826 struct extent_buffer *leaf;
6827 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006828 struct extent_map *em = NULL;
6829 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006830 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006831 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006832 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006833
Chris Masona52d9a82007-08-27 16:49:44 -04006834again:
Chris Mason890871b2009-09-02 16:24:52 -04006835 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006836 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006837 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006838 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006839 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006840
Chris Masona52d9a82007-08-27 16:49:44 -04006841 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006842 if (em->start > start || em->start + em->len <= start)
6843 free_extent_map(em);
6844 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006845 free_extent_map(em);
6846 else
6847 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006848 }
David Sterba172ddd62011-04-21 00:48:27 +02006849 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006850 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006851 err = -ENOMEM;
6852 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006853 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006854 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006855 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006856 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006857 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006858 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006859
6860 if (!path) {
6861 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006862 if (!path) {
6863 err = -ENOMEM;
6864 goto out;
6865 }
6866 /*
6867 * Chances are we'll be called again, so go ahead and do
6868 * readahead
6869 */
David Sterbae4058b52015-11-27 16:31:35 +01006870 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006871 }
6872
Chris Mason179e29e2007-11-01 11:28:41 -04006873 ret = btrfs_lookup_file_extent(trans, root, path,
6874 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006875 if (ret < 0) {
6876 err = ret;
6877 goto out;
6878 }
6879
6880 if (ret != 0) {
6881 if (path->slots[0] == 0)
6882 goto not_found;
6883 path->slots[0]--;
6884 }
6885
Chris Mason5f39d392007-10-15 16:14:19 -04006886 leaf = path->nodes[0];
6887 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006888 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006889 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006890 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006891 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006892 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006893 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006894 /*
6895 * If we backup past the first extent we want to move forward
6896 * and see if there is an extent in front of us, otherwise we'll
6897 * say there is a hole for our whole search range which can
6898 * cause problems.
6899 */
6900 extent_end = start;
6901 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006902 }
6903
Chris Mason5f39d392007-10-15 16:14:19 -04006904 found_type = btrfs_file_extent_type(leaf, item);
6905 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006906 if (found_type == BTRFS_FILE_EXTENT_REG ||
6907 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006908 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006909 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006910 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6911 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006912 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006913 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006914 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006915 }
Josef Bacik25a50342013-10-14 12:08:38 -04006916next:
Yan Zheng9036c102008-10-30 14:19:41 -04006917 if (start >= extent_end) {
6918 path->slots[0]++;
6919 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6920 ret = btrfs_next_leaf(root, path);
6921 if (ret < 0) {
6922 err = ret;
6923 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006924 }
Yan Zheng9036c102008-10-30 14:19:41 -04006925 if (ret > 0)
6926 goto not_found;
6927 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006928 }
Yan Zheng9036c102008-10-30 14:19:41 -04006929 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6930 if (found_key.objectid != objectid ||
6931 found_key.type != BTRFS_EXTENT_DATA_KEY)
6932 goto not_found;
6933 if (start + len <= found_key.offset)
6934 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006935 if (start > found_key.offset)
6936 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006937 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006938 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006939 em->len = found_key.offset - start;
6940 goto not_found_em;
6941 }
6942
Filipe Manana7ffbb592014-06-09 03:48:05 +01006943 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6944
Yan Zhengd899e052008-10-30 14:25:28 -04006945 if (found_type == BTRFS_FILE_EXTENT_REG ||
6946 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006947 goto insert;
6948 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006949 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006950 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006951 size_t size;
6952 size_t extent_offset;
6953 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006954
Filipe Manana7ffbb592014-06-09 03:48:05 +01006955 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006956 goto out;
Yan689f9342007-10-29 11:41:07 -04006957
Chris Mason514ac8a2014-01-03 21:07:00 -08006958 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006959 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006960 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6961 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006962 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006963 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006964 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006965 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006966 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006967 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006968 if (btrfs_file_extent_compression(leaf, item) !=
6969 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006970 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006971 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006972 if (ret) {
6973 err = ret;
6974 goto out;
6975 }
Chris Masonc8b97812008-10-29 14:49:59 -04006976 } else {
6977 map = kmap(page);
6978 read_extent_buffer(leaf, map + pg_offset, ptr,
6979 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006980 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006981 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006982 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006983 copy_size);
6984 }
Chris Masonc8b97812008-10-29 14:49:59 -04006985 kunmap(page);
6986 }
Chris Mason179e29e2007-11-01 11:28:41 -04006987 flush_dcache_page(page);
6988 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006989 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006990 if (!trans) {
6991 kunmap(page);
6992 free_extent_map(em);
6993 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006994
David Sterbab3b4aa72011-04-21 01:20:15 +02006995 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006996 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006997
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006998 if (IS_ERR(trans))
6999 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007000 goto again;
7001 }
Chris Masonc8b97812008-10-29 14:49:59 -04007002 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007003 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007004 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007005 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007006 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007007 }
Chris Masond1310b22008-01-24 16:13:08 -05007008 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007009 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007010 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007011 }
7012not_found:
7013 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007014 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007015 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007016not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007017 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007018 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007019insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007020 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007021 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007022 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007023 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7024 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007025 err = -EIO;
7026 goto out;
7027 }
Chris Masond1310b22008-01-24 16:13:08 -05007028
7029 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007030 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007031 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007032 /* it is possible that someone inserted the extent into the tree
7033 * while we had the lock dropped. It is also possible that
7034 * an overlapping map exists in the tree
7035 */
Chris Masona52d9a82007-08-27 16:49:44 -04007036 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007037 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007038
7039 ret = 0;
7040
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007041 existing = search_extent_mapping(em_tree, start, len);
7042 /*
7043 * existing will always be non-NULL, since there must be
7044 * extent causing the -EEXIST.
7045 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007046 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007047 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007048 em->block_start == existing->block_start) {
7049 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007050 * The existing extent map already encompasses the
7051 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007052 */
7053 free_extent_map(em);
7054 em = existing;
7055 err = 0;
7056
7057 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007058 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007059 /*
7060 * The existing extent map is the one nearest to
7061 * the [start, start + len) range which overlaps
7062 */
7063 err = merge_extent_mapping(em_tree, existing,
7064 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007065 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007066 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007067 free_extent_map(em);
7068 em = NULL;
7069 }
7070 } else {
7071 free_extent_map(em);
7072 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007073 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007074 }
Chris Masona52d9a82007-08-27 16:49:44 -04007075 }
Chris Mason890871b2009-09-02 16:24:52 -04007076 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007077out:
liubo1abe9b82011-03-24 11:18:59 +00007078
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007079 trace_btrfs_get_extent(root, BTRFS_I(inode), em);
liubo1abe9b82011-03-24 11:18:59 +00007080
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007081 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007082 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007083 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007084 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007085 err = ret;
7086 }
Chris Masona52d9a82007-08-27 16:49:44 -04007087 if (err) {
7088 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007089 return ERR_PTR(err);
7090 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007091 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007092 return em;
7093}
7094
Chris Masonec29ed52011-02-23 16:23:20 -05007095struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7096 size_t pg_offset, u64 start, u64 len,
7097 int create)
7098{
7099 struct extent_map *em;
7100 struct extent_map *hole_em = NULL;
7101 u64 range_start = start;
7102 u64 end;
7103 u64 found;
7104 u64 found_end;
7105 int err = 0;
7106
7107 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7108 if (IS_ERR(em))
7109 return em;
7110 if (em) {
7111 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007112 * if our em maps to
7113 * - a hole or
7114 * - a pre-alloc extent,
7115 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007116 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007117 if (em->block_start != EXTENT_MAP_HOLE &&
7118 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007119 return em;
7120 else
7121 hole_em = em;
7122 }
7123
7124 /* check to see if we've wrapped (len == -1 or similar) */
7125 end = start + len;
7126 if (end < start)
7127 end = (u64)-1;
7128 else
7129 end -= 1;
7130
7131 em = NULL;
7132
7133 /* ok, we didn't find anything, lets look for delalloc */
7134 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7135 end, len, EXTENT_DELALLOC, 1);
7136 found_end = range_start + found;
7137 if (found_end < range_start)
7138 found_end = (u64)-1;
7139
7140 /*
7141 * we didn't find anything useful, return
7142 * the original results from get_extent()
7143 */
7144 if (range_start > end || found_end <= start) {
7145 em = hole_em;
7146 hole_em = NULL;
7147 goto out;
7148 }
7149
7150 /* adjust the range_start to make sure it doesn't
7151 * go backwards from the start they passed in
7152 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307153 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007154 found = found_end - range_start;
7155
7156 if (found > 0) {
7157 u64 hole_start = start;
7158 u64 hole_len = len;
7159
David Sterba172ddd62011-04-21 00:48:27 +02007160 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007161 if (!em) {
7162 err = -ENOMEM;
7163 goto out;
7164 }
7165 /*
7166 * when btrfs_get_extent can't find anything it
7167 * returns one huge hole
7168 *
7169 * make sure what it found really fits our range, and
7170 * adjust to make sure it is based on the start from
7171 * the caller
7172 */
7173 if (hole_em) {
7174 u64 calc_end = extent_map_end(hole_em);
7175
7176 if (calc_end <= start || (hole_em->start > end)) {
7177 free_extent_map(hole_em);
7178 hole_em = NULL;
7179 } else {
7180 hole_start = max(hole_em->start, start);
7181 hole_len = calc_end - hole_start;
7182 }
7183 }
7184 em->bdev = NULL;
7185 if (hole_em && range_start > hole_start) {
7186 /* our hole starts before our delalloc, so we
7187 * have to return just the parts of the hole
7188 * that go until the delalloc starts
7189 */
7190 em->len = min(hole_len,
7191 range_start - hole_start);
7192 em->start = hole_start;
7193 em->orig_start = hole_start;
7194 /*
7195 * don't adjust block start at all,
7196 * it is fixed at EXTENT_MAP_HOLE
7197 */
7198 em->block_start = hole_em->block_start;
7199 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007200 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7201 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007202 } else {
7203 em->start = range_start;
7204 em->len = found;
7205 em->orig_start = range_start;
7206 em->block_start = EXTENT_MAP_DELALLOC;
7207 em->block_len = found;
7208 }
7209 } else if (hole_em) {
7210 return hole_em;
7211 }
7212out:
7213
7214 free_extent_map(hole_em);
7215 if (err) {
7216 free_extent_map(em);
7217 return ERR_PTR(err);
7218 }
7219 return em;
7220}
7221
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007222static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7223 const u64 start,
7224 const u64 len,
7225 const u64 orig_start,
7226 const u64 block_start,
7227 const u64 block_len,
7228 const u64 orig_block_len,
7229 const u64 ram_bytes,
7230 const int type)
7231{
7232 struct extent_map *em = NULL;
7233 int ret;
7234
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007235 if (type != BTRFS_ORDERED_NOCOW) {
7236 em = create_pinned_em(inode, start, len, orig_start,
7237 block_start, block_len, orig_block_len,
7238 ram_bytes, type);
7239 if (IS_ERR(em))
7240 goto out;
7241 }
7242 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7243 len, block_len, type);
7244 if (ret) {
7245 if (em) {
7246 free_extent_map(em);
7247 btrfs_drop_extent_cache(inode, start,
7248 start + len - 1, 0);
7249 }
7250 em = ERR_PTR(ret);
7251 }
7252 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007253
7254 return em;
7255}
7256
Josef Bacik4b46fce2010-05-23 11:00:55 -04007257static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7258 u64 start, u64 len)
7259{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007260 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007261 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007262 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263 struct btrfs_key ins;
7264 u64 alloc_hint;
7265 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266
Josef Bacik4b46fce2010-05-23 11:00:55 -04007267 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007268 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007269 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007270 if (ret)
7271 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007272
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007273 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7274 ins.objectid, ins.offset, ins.offset,
7275 ins.offset, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007276 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007277 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007278 btrfs_free_reserved_extent(fs_info, ins.objectid,
7279 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007280
Josef Bacik4b46fce2010-05-23 11:00:55 -04007281 return em;
7282}
7283
Chris Mason46bfbb52010-05-26 11:04:10 -04007284/*
7285 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7286 * block must be cow'd
7287 */
Josef Bacik00361582013-08-14 14:02:47 -04007288noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007289 u64 *orig_start, u64 *orig_block_len,
7290 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007291{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007292 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -04007293 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007294 struct btrfs_path *path;
7295 int ret;
7296 struct extent_buffer *leaf;
7297 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007298 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007299 struct btrfs_file_extent_item *fi;
7300 struct btrfs_key key;
7301 u64 disk_bytenr;
7302 u64 backref_offset;
7303 u64 extent_end;
7304 u64 num_bytes;
7305 int slot;
7306 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007307 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007308
Chris Mason46bfbb52010-05-26 11:04:10 -04007309 path = btrfs_alloc_path();
7310 if (!path)
7311 return -ENOMEM;
7312
David Sterbaf85b7372017-01-20 14:54:07 +01007313 ret = btrfs_lookup_file_extent(NULL, root, path,
7314 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007315 if (ret < 0)
7316 goto out;
7317
7318 slot = path->slots[0];
7319 if (ret == 1) {
7320 if (slot == 0) {
7321 /* can't find the item, must cow */
7322 ret = 0;
7323 goto out;
7324 }
7325 slot--;
7326 }
7327 ret = 0;
7328 leaf = path->nodes[0];
7329 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007330 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007331 key.type != BTRFS_EXTENT_DATA_KEY) {
7332 /* not our file or wrong item type, must cow */
7333 goto out;
7334 }
7335
7336 if (key.offset > offset) {
7337 /* Wrong offset, must cow */
7338 goto out;
7339 }
7340
7341 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7342 found_type = btrfs_file_extent_type(leaf, fi);
7343 if (found_type != BTRFS_FILE_EXTENT_REG &&
7344 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7345 /* not a regular extent, must cow */
7346 goto out;
7347 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007348
7349 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7350 goto out;
7351
Miao Xiee77751a2013-12-27 21:11:50 +08007352 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7353 if (extent_end <= offset)
7354 goto out;
7355
Chris Mason46bfbb52010-05-26 11:04:10 -04007356 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007357 if (disk_bytenr == 0)
7358 goto out;
7359
7360 if (btrfs_file_extent_compression(leaf, fi) ||
7361 btrfs_file_extent_encryption(leaf, fi) ||
7362 btrfs_file_extent_other_encoding(leaf, fi))
7363 goto out;
7364
Chris Mason46bfbb52010-05-26 11:04:10 -04007365 backref_offset = btrfs_file_extent_offset(leaf, fi);
7366
Josef Bacik7ee9e442013-06-21 16:37:03 -04007367 if (orig_start) {
7368 *orig_start = key.offset - backref_offset;
7369 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7370 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7371 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007372
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007373 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007374 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007375
7376 num_bytes = min(offset + *len, extent_end) - offset;
7377 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7378 u64 range_end;
7379
Jeff Mahoneyda170662016-06-15 09:22:56 -04007380 range_end = round_up(offset + num_bytes,
7381 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007382 ret = test_range_bit(io_tree, offset, range_end,
7383 EXTENT_DELALLOC, 0, NULL);
7384 if (ret) {
7385 ret = -EAGAIN;
7386 goto out;
7387 }
7388 }
7389
Josef Bacik1bda19e2013-10-18 12:10:36 -04007390 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007391
7392 /*
7393 * look for other files referencing this extent, if we
7394 * find any we must cow
7395 */
Josef Bacik00361582013-08-14 14:02:47 -04007396 trans = btrfs_join_transaction(root);
7397 if (IS_ERR(trans)) {
7398 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007399 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007400 }
7401
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007402 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007403 key.offset - backref_offset, disk_bytenr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007404 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007405 if (ret) {
7406 ret = 0;
7407 goto out;
7408 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007409
7410 /*
7411 * adjust disk_bytenr and num_bytes to cover just the bytes
7412 * in this extent we are about to write. If there
7413 * are any csums in that range we have to cow in order
7414 * to keep the csums correct
7415 */
7416 disk_bytenr += backref_offset;
7417 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007418 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7419 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007420 /*
7421 * all of the above have passed, it is safe to overwrite this extent
7422 * without cow
7423 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007424 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007425 ret = 1;
7426out:
7427 btrfs_free_path(path);
7428 return ret;
7429}
7430
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007431bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7432{
7433 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7434 int found = false;
7435 void **pagep = NULL;
7436 struct page *page = NULL;
7437 int start_idx;
7438 int end_idx;
7439
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007440 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007441
7442 /*
7443 * end is the last byte in the last page. end == start is legal
7444 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007445 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007446
7447 rcu_read_lock();
7448
7449 /* Most of the code in this while loop is lifted from
7450 * find_get_page. It's been modified to begin searching from a
7451 * page and return just the first page found in that range. If the
7452 * found idx is less than or equal to the end idx then we know that
7453 * a page exists. If no pages are found or if those pages are
7454 * outside of the range then we're fine (yay!) */
7455 while (page == NULL &&
7456 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7457 page = radix_tree_deref_slot(pagep);
7458 if (unlikely(!page))
7459 break;
7460
7461 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007462 if (radix_tree_deref_retry(page)) {
7463 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007464 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007465 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007466 /*
7467 * Otherwise, shmem/tmpfs must be storing a swap entry
7468 * here as an exceptional entry: so return it without
7469 * attempting to raise page count.
7470 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007471 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007472 break; /* TODO: Is this relevant for this use case? */
7473 }
7474
Filipe Manana91405152014-06-05 13:22:24 +01007475 if (!page_cache_get_speculative(page)) {
7476 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007477 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007478 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007479
7480 /*
7481 * Has the page moved?
7482 * This is part of the lockless pagecache protocol. See
7483 * include/linux/pagemap.h for details.
7484 */
7485 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007486 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007487 page = NULL;
7488 }
7489 }
7490
7491 if (page) {
7492 if (page->index <= end_idx)
7493 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007494 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007495 }
7496
7497 rcu_read_unlock();
7498 return found;
7499}
7500
Josef Bacikeb838e72012-07-31 16:28:48 -04007501static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7502 struct extent_state **cached_state, int writing)
7503{
7504 struct btrfs_ordered_extent *ordered;
7505 int ret = 0;
7506
7507 while (1) {
7508 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007509 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007510 /*
7511 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007512 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007513 * extents in this range.
7514 */
7515 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7516 lockend - lockstart + 1);
7517
7518 /*
7519 * We need to make sure there are no buffered pages in this
7520 * range either, we could have raced between the invalidate in
7521 * generic_file_direct_write and locking the extent. The
7522 * invalidate needs to happen so that reads after a write do not
7523 * get stale data.
7524 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007525 if (!ordered &&
7526 (!writing ||
7527 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007528 break;
7529
7530 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7531 cached_state, GFP_NOFS);
7532
7533 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007534 /*
7535 * If we are doing a DIO read and the ordered extent we
7536 * found is for a buffered write, we can not wait for it
7537 * to complete and retry, because if we do so we can
7538 * deadlock with concurrent buffered writes on page
7539 * locks. This happens only if our DIO read covers more
7540 * than one extent map, if at this point has already
7541 * created an ordered extent for a previous extent map
7542 * and locked its range in the inode's io tree, and a
7543 * concurrent write against that previous extent map's
7544 * range and this range started (we unlock the ranges
7545 * in the io tree only when the bios complete and
7546 * buffered writes always lock pages before attempting
7547 * to lock range in the io tree).
7548 */
7549 if (writing ||
7550 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7551 btrfs_start_ordered_extent(inode, ordered, 1);
7552 else
7553 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007554 btrfs_put_ordered_extent(ordered);
7555 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007556 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007557 * We could trigger writeback for this range (and wait
7558 * for it to complete) and then invalidate the pages for
7559 * this range (through invalidate_inode_pages2_range()),
7560 * but that can lead us to a deadlock with a concurrent
7561 * call to readpages() (a buffered read or a defrag call
7562 * triggered a readahead) on a page lock due to an
7563 * ordered dio extent we created before but did not have
7564 * yet a corresponding bio submitted (whence it can not
7565 * complete), which makes readpages() wait for that
7566 * ordered extent to complete while holding a lock on
7567 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007568 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007569 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007570 }
7571
Filipe Mananaade77022016-02-18 14:28:55 +00007572 if (ret)
7573 break;
7574
Josef Bacikeb838e72012-07-31 16:28:48 -04007575 cond_resched();
7576 }
7577
7578 return ret;
7579}
7580
Josef Bacik69ffb542012-09-11 15:40:07 -04007581static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7582 u64 len, u64 orig_start,
7583 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007584 u64 orig_block_len, u64 ram_bytes,
7585 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007586{
7587 struct extent_map_tree *em_tree;
7588 struct extent_map *em;
7589 struct btrfs_root *root = BTRFS_I(inode)->root;
7590 int ret;
7591
7592 em_tree = &BTRFS_I(inode)->extent_tree;
7593 em = alloc_extent_map();
7594 if (!em)
7595 return ERR_PTR(-ENOMEM);
7596
7597 em->start = start;
7598 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007599 em->mod_start = start;
7600 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007601 em->len = len;
7602 em->block_len = block_len;
7603 em->block_start = block_start;
7604 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007605 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007606 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007607 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007608 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7609 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007610 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007611
7612 do {
7613 btrfs_drop_extent_cache(inode, em->start,
7614 em->start + em->len - 1, 0);
7615 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007616 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007617 write_unlock(&em_tree->lock);
7618 } while (ret == -EEXIST);
7619
7620 if (ret) {
7621 free_extent_map(em);
7622 return ERR_PTR(ret);
7623 }
7624
7625 return em;
7626}
7627
Filipe Manana9c9464c2015-11-04 09:52:04 +00007628static void adjust_dio_outstanding_extents(struct inode *inode,
7629 struct btrfs_dio_data *dio_data,
7630 const u64 len)
7631{
David Sterba823bb202017-01-04 11:09:51 +01007632 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007633
Filipe Manana9c9464c2015-11-04 09:52:04 +00007634 /*
7635 * If we have an outstanding_extents count still set then we're
7636 * within our reservation, otherwise we need to adjust our inode
7637 * counter appropriately.
7638 */
Liu Boc2931662016-12-22 17:13:54 -08007639 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007640 dio_data->outstanding_extents -= num_extents;
7641 } else {
Liu Boc2931662016-12-22 17:13:54 -08007642 /*
7643 * If dio write length has been split due to no large enough
7644 * contiguous space, we need to compensate our inode counter
7645 * appropriately.
7646 */
7647 u64 num_needed = num_extents - dio_data->outstanding_extents;
7648
Filipe Manana9c9464c2015-11-04 09:52:04 +00007649 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007650 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007651 spin_unlock(&BTRFS_I(inode)->lock);
7652 }
7653}
7654
Josef Bacik4b46fce2010-05-23 11:00:55 -04007655static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7656 struct buffer_head *bh_result, int create)
7657{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007658 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007659 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007660 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307661 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007662 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007663 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007664 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007665 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007666 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007667
Miao Xie172a5042013-02-21 02:48:22 -07007668 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007669 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007670 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007671 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007672
Josef Bacikc3298612012-08-03 16:49:19 -04007673 lockstart = start;
7674 lockend = start + len - 1;
7675
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007676 if (current->journal_info) {
7677 /*
7678 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007679 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007680 * confused.
7681 */
chandan50745b02015-08-28 21:10:13 +05307682 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007683 current->journal_info = NULL;
7684 }
7685
Josef Bacikeb838e72012-07-31 16:28:48 -04007686 /*
7687 * If this errors out it's because we couldn't invalidate pagecache for
7688 * this range and we need to fallback to buffered.
7689 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007690 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7691 create)) {
7692 ret = -ENOTBLK;
7693 goto err;
7694 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007695
Josef Bacik4b46fce2010-05-23 11:00:55 -04007696 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007697 if (IS_ERR(em)) {
7698 ret = PTR_ERR(em);
7699 goto unlock_err;
7700 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007701
7702 /*
7703 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7704 * io. INLINE is special, and we could probably kludge it in here, but
7705 * it's still buffered so for safety lets just fall back to the generic
7706 * buffered path.
7707 *
7708 * For COMPRESSED we _have_ to read the entire extent in so we can
7709 * decompress it, so there will be buffering required no matter what we
7710 * do, so go ahead and fallback to buffered.
7711 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007712 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007713 * to buffered IO. Don't blame me, this is the price we pay for using
7714 * the generic code.
7715 */
7716 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7717 em->block_start == EXTENT_MAP_INLINE) {
7718 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007719 ret = -ENOTBLK;
7720 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007721 }
7722
7723 /* Just a good old fashioned hole, return */
7724 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7725 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7726 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007727 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007728 }
7729
7730 /*
7731 * We don't allocate a new extent in the following cases
7732 *
7733 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7734 * existing extent.
7735 * 2) The extent is marked as PREALLOC. We're good to go here and can
7736 * just use the extent.
7737 *
7738 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007739 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007740 len = min(len, em->len - (start - em->start));
7741 lockstart = start + len;
7742 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007743 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007744
7745 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7746 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7747 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007748 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007749 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007750
7751 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7752 type = BTRFS_ORDERED_PREALLOC;
7753 else
7754 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007755 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007756 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007757
Josef Bacik00361582013-08-14 14:02:47 -04007758 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007759 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007760 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007761 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007762
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007763 em2 = btrfs_create_dio_extent(inode, start, len,
7764 orig_start, block_start,
7765 len, orig_block_len,
7766 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007767 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007768 if (type == BTRFS_ORDERED_PREALLOC) {
7769 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007770 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007771 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007772 if (em2 && IS_ERR(em2)) {
7773 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007774 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007775 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007776 /*
7777 * For inode marked NODATACOW or extent marked PREALLOC,
7778 * use the existing or preallocated extent, so does not
7779 * need to adjust btrfs_space_info's bytes_may_use.
7780 */
7781 btrfs_free_reserved_data_space_noquota(inode,
7782 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007783 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007784 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007785 }
Josef Bacik00361582013-08-14 14:02:47 -04007786
Chris Mason46bfbb52010-05-26 11:04:10 -04007787 /*
7788 * this will cow the extent, reset the len in case we changed
7789 * it above
7790 */
7791 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007792 free_extent_map(em);
7793 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007794 if (IS_ERR(em)) {
7795 ret = PTR_ERR(em);
7796 goto unlock_err;
7797 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007798 len = min(len, em->len - (start - em->start));
7799unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007800 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7801 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007802 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007803 bh_result->b_bdev = em->bdev;
7804 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007805 if (create) {
7806 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7807 set_buffer_new(bh_result);
7808
7809 /*
7810 * Need to update the i_size under the extent lock so buffered
7811 * readers will get the updated i_size when we unlock.
7812 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007813 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007814 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007815
Filipe Manana9c9464c2015-11-04 09:52:04 +00007816 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307817 WARN_ON(dio_data->reserve < len);
7818 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007819 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307820 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007821 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007822
Josef Bacikeb838e72012-07-31 16:28:48 -04007823 /*
7824 * In the case of write we need to clear and unlock the entire range,
7825 * in the case of read we need to unlock only the end area that we
7826 * aren't using if there is any left over space.
7827 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007828 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007829 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7830 lockend, unlock_bits, 1, 0,
7831 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007832 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007833 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007834 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007835
Josef Bacik4b46fce2010-05-23 11:00:55 -04007836 free_extent_map(em);
7837
7838 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007839
7840unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007841 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7842 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007843err:
chandan50745b02015-08-28 21:10:13 +05307844 if (dio_data)
7845 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007846 /*
7847 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7848 * write less data then expected, so that we don't underflow our inode's
7849 * outstanding extents counter.
7850 */
7851 if (create && dio_data)
7852 adjust_dio_outstanding_extents(inode, dio_data, len);
7853
Josef Bacikeb838e72012-07-31 16:28:48 -04007854 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007855}
7856
Miao Xie8b110e32014-09-12 18:44:03 +08007857static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007858 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007859{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007860 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007861 int ret;
7862
Mike Christie37226b22016-06-05 14:31:52 -05007863 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007864
7865 bio_get(bio);
7866
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007867 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007868 if (ret)
7869 goto err;
7870
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007871 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007872err:
7873 bio_put(bio);
7874 return ret;
7875}
7876
7877static int btrfs_check_dio_repairable(struct inode *inode,
7878 struct bio *failed_bio,
7879 struct io_failure_record *failrec,
7880 int failed_mirror)
7881{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007882 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007883 int num_copies;
7884
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007885 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007886 if (num_copies == 1) {
7887 /*
7888 * we only have a single copy of the data, so don't bother with
7889 * all the retry and error correction code that follows. no
7890 * matter what the error is, it is very likely to persist.
7891 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007892 btrfs_debug(fs_info,
7893 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7894 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007895 return 0;
7896 }
7897
7898 failrec->failed_mirror = failed_mirror;
7899 failrec->this_mirror++;
7900 if (failrec->this_mirror == failed_mirror)
7901 failrec->this_mirror++;
7902
7903 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007904 btrfs_debug(fs_info,
7905 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7906 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007907 return 0;
7908 }
7909
7910 return 1;
7911}
7912
7913static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307914 struct page *page, unsigned int pgoff,
7915 u64 start, u64 end, int failed_mirror,
7916 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007917{
7918 struct io_failure_record *failrec;
7919 struct bio *bio;
7920 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007921 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007922 int ret;
7923
Mike Christie37226b22016-06-05 14:31:52 -05007924 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007925
7926 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7927 if (ret)
7928 return ret;
7929
7930 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7931 failed_mirror);
7932 if (!ret) {
7933 free_io_failure(inode, failrec);
7934 return -EIO;
7935 }
7936
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307937 if ((failed_bio->bi_vcnt > 1)
7938 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007939 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007940 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007941
7942 isector = start - btrfs_io_bio(failed_bio)->logical;
7943 isector >>= inode->i_sb->s_blocksize_bits;
7944 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307945 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007946 if (!bio) {
7947 free_io_failure(inode, failrec);
7948 return -EIO;
7949 }
Mike Christie37226b22016-06-05 14:31:52 -05007950 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007951
7952 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7953 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7954 read_mode, failrec->this_mirror, failrec->in_validation);
7955
Mike Christie81a75f672016-06-05 14:31:54 -05007956 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007957 if (ret) {
7958 free_io_failure(inode, failrec);
7959 bio_put(bio);
7960 }
7961
7962 return ret;
7963}
7964
7965struct btrfs_retry_complete {
7966 struct completion done;
7967 struct inode *inode;
7968 u64 start;
7969 int uptodate;
7970};
7971
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007972static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007973{
7974 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307975 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007976 struct bio_vec *bvec;
7977 int i;
7978
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007979 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007980 goto end;
7981
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307982 ASSERT(bio->bi_vcnt == 1);
7983 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007984 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307985
Miao Xie8b110e32014-09-12 18:44:03 +08007986 done->uptodate = 1;
7987 bio_for_each_segment_all(bvec, bio, i)
7988 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7989end:
7990 complete(&done->done);
7991 bio_put(bio);
7992}
7993
7994static int __btrfs_correct_data_nocsum(struct inode *inode,
7995 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007996{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307997 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007998 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007999 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008000 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308001 unsigned int pgoff;
8002 u32 sectorsize;
8003 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08008004 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08008005 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08008006
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308007 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008008 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308009
Miao Xiec1dc0892014-09-12 18:43:56 +08008010 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008011 done.inode = inode;
8012
8013 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308014 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8015 pgoff = bvec->bv_offset;
8016
8017next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008018 done.uptodate = 0;
8019 done.start = start;
8020 init_completion(&done.done);
8021
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308022 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8023 pgoff, start, start + sectorsize - 1,
8024 io_bio->mirror_num,
8025 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008026 if (ret)
8027 return ret;
8028
8029 wait_for_completion(&done.done);
8030
8031 if (!done.uptodate) {
8032 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308033 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008034 }
8035
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308036 start += sectorsize;
8037
8038 if (nr_sectors--) {
8039 pgoff += sectorsize;
8040 goto next_block_or_try_again;
8041 }
Miao Xie8b110e32014-09-12 18:44:03 +08008042 }
8043
8044 return 0;
8045}
8046
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008047static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008048{
8049 struct btrfs_retry_complete *done = bio->bi_private;
8050 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308051 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008052 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308053 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008054 int uptodate;
8055 int ret;
8056 int i;
8057
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008058 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008059 goto end;
8060
8061 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062
8063 start = done->start;
8064
8065 ASSERT(bio->bi_vcnt == 1);
8066 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008067 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308068
Miao Xie8b110e32014-09-12 18:44:03 +08008069 bio_for_each_segment_all(bvec, bio, i) {
8070 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308071 bvec->bv_page, bvec->bv_offset,
8072 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008073 if (!ret)
8074 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308075 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008076 else
8077 uptodate = 0;
8078 }
8079
8080 done->uptodate = uptodate;
8081end:
8082 complete(&done->done);
8083 bio_put(bio);
8084}
8085
8086static int __btrfs_subio_endio_read(struct inode *inode,
8087 struct btrfs_io_bio *io_bio, int err)
8088{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308089 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008090 struct bio_vec *bvec;
8091 struct btrfs_retry_complete done;
8092 u64 start;
8093 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308094 u32 sectorsize;
8095 int nr_sectors;
8096 unsigned int pgoff;
8097 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008098 int i;
8099 int ret;
8100
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308101 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008102 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308103
Miao Xie8b110e32014-09-12 18:44:03 +08008104 err = 0;
8105 start = io_bio->logical;
8106 done.inode = inode;
8107
Miao Xiec1dc0892014-09-12 18:43:56 +08008108 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308109 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8110
8111 pgoff = bvec->bv_offset;
8112next_block:
8113 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8114 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8115 bvec->bv_page, pgoff, start,
8116 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008117 if (likely(!ret))
8118 goto next;
8119try_again:
8120 done.uptodate = 0;
8121 done.start = start;
8122 init_completion(&done.done);
8123
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308124 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8125 pgoff, start, start + sectorsize - 1,
8126 io_bio->mirror_num,
8127 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008128 if (ret) {
8129 err = ret;
8130 goto next;
8131 }
8132
8133 wait_for_completion(&done.done);
8134
8135 if (!done.uptodate) {
8136 /* We might have another mirror, so try again */
8137 goto try_again;
8138 }
8139next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308140 offset += sectorsize;
8141 start += sectorsize;
8142
8143 ASSERT(nr_sectors);
8144
8145 if (--nr_sectors) {
8146 pgoff += sectorsize;
8147 goto next_block;
8148 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008149 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008150
8151 return err;
8152}
8153
Miao Xie8b110e32014-09-12 18:44:03 +08008154static int btrfs_subio_endio_read(struct inode *inode,
8155 struct btrfs_io_bio *io_bio, int err)
8156{
8157 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8158
8159 if (skip_csum) {
8160 if (unlikely(err))
8161 return __btrfs_correct_data_nocsum(inode, io_bio);
8162 else
8163 return 0;
8164 } else {
8165 return __btrfs_subio_endio_read(inode, io_bio, err);
8166 }
8167}
8168
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008169static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008170{
8171 struct btrfs_dio_private *dip = bio->bi_private;
8172 struct inode *inode = dip->inode;
8173 struct bio *dio_bio;
8174 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008175 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008176
Miao Xie8b110e32014-09-12 18:44:03 +08008177 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8178 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008179
Josef Bacik4b46fce2010-05-23 11:00:55 -04008180 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008181 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008182 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008183
Josef Bacik4b46fce2010-05-23 11:00:55 -04008184 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008185
Filipe Manana1636d1d2016-02-15 16:20:26 +00008186 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008187 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008188
8189 if (io_bio->end_io)
8190 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008191 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008192}
8193
Filipe Manana14543772015-11-24 16:23:54 +00008194static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8195 const u64 offset,
8196 const u64 bytes,
8197 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008198{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008200 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008201 u64 ordered_offset = offset;
8202 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008203 int ret;
8204
Chris Mason163cf092010-11-28 19:56:33 -05008205again:
8206 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8207 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008208 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008209 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008210 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008211 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008212
Liu Bo9e0af232014-08-15 23:36:53 +08008213 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8214 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008215 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008216out_test:
8217 /*
8218 * our bio might span multiple ordered extents. If we haven't
8219 * completed the accounting for the whole dio, go back and try again
8220 */
Filipe Manana14543772015-11-24 16:23:54 +00008221 if (ordered_offset < offset + bytes) {
8222 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008223 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008224 goto again;
8225 }
Filipe Manana14543772015-11-24 16:23:54 +00008226}
8227
8228static void btrfs_endio_direct_write(struct bio *bio)
8229{
8230 struct btrfs_dio_private *dip = bio->bi_private;
8231 struct bio *dio_bio = dip->dio_bio;
8232
8233 btrfs_endio_direct_write_update_ordered(dip->inode,
8234 dip->logical_offset,
8235 dip->bytes,
8236 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008237
Josef Bacik4b46fce2010-05-23 11:00:55 -04008238 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008239
Filipe Manana1636d1d2016-02-15 16:20:26 +00008240 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008241 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008242 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008243}
8244
Mike Christie81a75f672016-06-05 14:31:54 -05008245static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008246 struct bio *bio, int mirror_num,
8247 unsigned long bio_flags, u64 offset)
8248{
8249 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008250 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008251 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008252 return 0;
8253}
8254
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008255static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008256{
8257 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008258 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008259
Miao Xie8b110e32014-09-12 18:44:03 +08008260 if (err)
8261 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008262 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008263 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8264 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008265 (unsigned long long)bio->bi_iter.bi_sector,
8266 bio->bi_iter.bi_size, err);
8267
8268 if (dip->subio_endio)
8269 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008270
8271 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008272 dip->errors = 1;
8273
8274 /*
8275 * before atomic variable goto zero, we must make sure
8276 * dip->errors is perceived to be set.
8277 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008278 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008279 }
8280
8281 /* if there are more bios still pending for this dio, just exit */
8282 if (!atomic_dec_and_test(&dip->pending_bios))
8283 goto out;
8284
Chris Mason9be33952013-05-17 18:30:14 -04008285 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008286 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008287 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008288 dip->dio_bio->bi_error = 0;
8289 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008290 }
8291out:
8292 bio_put(bio);
8293}
8294
8295static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8296 u64 first_sector, gfp_t gfp_flags)
8297{
Chris Masonda2f0f72015-07-02 13:57:22 -07008298 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008299 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008300 if (bio)
8301 bio_associate_current(bio);
8302 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008303}
8304
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008305static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008306 struct btrfs_dio_private *dip,
8307 struct bio *bio,
8308 u64 file_offset)
8309{
8310 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8311 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8312 int ret;
8313
8314 /*
8315 * We load all the csum data we need when we submit
8316 * the first bio to reduce the csum tree search and
8317 * contention.
8318 */
8319 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008320 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008321 file_offset);
8322 if (ret)
8323 return ret;
8324 }
8325
8326 if (bio == dip->orig_bio)
8327 return 0;
8328
8329 file_offset -= dip->logical_offset;
8330 file_offset >>= inode->i_sb->s_blocksize_bits;
8331 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8332
8333 return 0;
8334}
8335
Miao Xiee65e1532010-11-22 03:04:43 +00008336static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008337 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008338 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008339{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008340 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008341 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008342 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008343 int ret;
8344
Josef Bacikb812ce22012-11-16 13:56:32 -05008345 if (async_submit)
8346 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8347
Miao Xiee65e1532010-11-22 03:04:43 +00008348 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008349
8350 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008351 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008352 if (ret)
8353 goto err;
8354 }
Miao Xiee65e1532010-11-22 03:04:43 +00008355
Josef Bacik1ae39932011-04-06 14:41:34 -04008356 if (skip_sum)
8357 goto map;
8358
8359 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008360 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8361 file_offset,
8362 __btrfs_submit_bio_start_direct_io,
8363 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008364 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008365 } else if (write) {
8366 /*
8367 * If we aren't doing async submit, calculate the csum of the
8368 * bio now.
8369 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008370 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008371 if (ret)
8372 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008373 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008374 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008375 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008376 if (ret)
8377 goto err;
8378 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008379map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008380 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008381err:
8382 bio_put(bio);
8383 return ret;
8384}
8385
Mike Christie81a75f672016-06-05 14:31:54 -05008386static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008387 int skip_sum)
8388{
8389 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008390 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008391 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008392 struct bio *bio;
8393 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008394 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008395 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008396 u64 file_offset = dip->logical_offset;
8397 u64 submit_len = 0;
8398 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008399 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008400 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308401 int nr_sectors;
8402 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008403 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008404
Kent Overstreet4f024f32013-10-11 15:44:27 -07008405 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008406 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8407 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008408 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008409 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008410
Kent Overstreet4f024f32013-10-11 15:44:27 -07008411 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008412 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008413 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008414 goto submit;
8415 }
8416
David Woodhouse53b381b2013-01-29 18:40:14 -05008417 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008418 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008419 async_submit = 0;
8420 else
8421 async_submit = 1;
8422
Josef Bacik02f57c72011-04-06 14:25:44 -04008423 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8424 if (!bio)
8425 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008426
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008427 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008428 bio->bi_private = dip;
8429 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008430 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008431 atomic_inc(&dip->pending_bios);
8432
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008433 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008434 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308435 i = 0;
8436next_block:
8437 if (unlikely(map_length < submit_len + blocksize ||
8438 bio_add_page(bio, bvec->bv_page, blocksize,
8439 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008440 /*
8441 * inc the count before we submit the bio so
8442 * we know the end IO handler won't happen before
8443 * we inc the count. Otherwise, the dip might get freed
8444 * before we're done setting it up
8445 */
8446 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008447 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008448 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008449 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008450 if (ret) {
8451 bio_put(bio);
8452 atomic_dec(&dip->pending_bios);
8453 goto out_err;
8454 }
8455
Miao Xiee65e1532010-11-22 03:04:43 +00008456 start_sector += submit_len >> 9;
8457 file_offset += submit_len;
8458
8459 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008460
8461 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8462 start_sector, GFP_NOFS);
8463 if (!bio)
8464 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008465 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008466 bio->bi_private = dip;
8467 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008468 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008469
Kent Overstreet4f024f32013-10-11 15:44:27 -07008470 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008471 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008472 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008473 &map_length, NULL, 0);
8474 if (ret) {
8475 bio_put(bio);
8476 goto out_err;
8477 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308478
8479 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008480 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308481 submit_len += blocksize;
8482 if (--nr_sectors) {
8483 i++;
8484 goto next_block;
8485 }
Miao Xiee65e1532010-11-22 03:04:43 +00008486 }
8487 }
8488
Josef Bacik02f57c72011-04-06 14:25:44 -04008489submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008490 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008491 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008492 if (!ret)
8493 return 0;
8494
8495 bio_put(bio);
8496out_err:
8497 dip->errors = 1;
8498 /*
8499 * before atomic variable goto zero, we must
8500 * make sure dip->errors is perceived to be set.
8501 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008502 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008503 if (atomic_dec_and_test(&dip->pending_bios))
8504 bio_io_error(dip->orig_bio);
8505
8506 /* bio_end_io() will handle error, so we needn't return it */
8507 return 0;
8508}
8509
Mike Christie8a4c1e42016-06-05 14:31:50 -05008510static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8511 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008512{
Filipe Manana61de7182015-07-01 12:13:10 +01008513 struct btrfs_dio_private *dip = NULL;
8514 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008515 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008516 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008517 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008518 int ret = 0;
8519
8520 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8521
Chris Mason9be33952013-05-17 18:30:14 -04008522 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008523 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008524 ret = -ENOMEM;
8525 goto free_ordered;
8526 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008527
Miao Xiec1dc0892014-09-12 18:43:56 +08008528 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008529 if (!dip) {
8530 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008531 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008532 }
8533
8534 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008535 dip->inode = inode;
8536 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008537 dip->bytes = dio_bio->bi_iter.bi_size;
8538 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008539 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008540 dip->orig_bio = io_bio;
8541 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008542 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008543 btrfs_bio = btrfs_io_bio(io_bio);
8544 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008545
Miao Xiec1dc0892014-09-12 18:43:56 +08008546 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008547 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008548 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008549 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008550 dip->subio_endio = btrfs_subio_endio_read;
8551 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008552
Filipe Mananaf28a4922015-12-08 19:23:20 +00008553 /*
8554 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8555 * even if we fail to submit a bio, because in such case we do the
8556 * corresponding error handling below and it must not be done a second
8557 * time by btrfs_direct_IO().
8558 */
8559 if (write) {
8560 struct btrfs_dio_data *dio_data = current->journal_info;
8561
8562 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8563 dip->bytes;
8564 dio_data->unsubmitted_oe_range_start =
8565 dio_data->unsubmitted_oe_range_end;
8566 }
8567
Mike Christie81a75f672016-06-05 14:31:54 -05008568 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008569 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008570 return;
Chris Mason9be33952013-05-17 18:30:14 -04008571
Miao Xie23ea8e52014-09-12 18:43:54 +08008572 if (btrfs_bio->end_io)
8573 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008574
Josef Bacik4b46fce2010-05-23 11:00:55 -04008575free_ordered:
8576 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008577 * If we arrived here it means either we failed to submit the dip
8578 * or we either failed to clone the dio_bio or failed to allocate the
8579 * dip. If we cloned the dio_bio and allocated the dip, we can just
8580 * call bio_endio against our io_bio so that we get proper resource
8581 * cleanup if we fail to submit the dip, otherwise, we must do the
8582 * same as btrfs_endio_direct_[write|read] because we can't call these
8583 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008584 */
Filipe Manana61de7182015-07-01 12:13:10 +01008585 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008586 io_bio->bi_error = -EIO;
8587 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008588 /*
8589 * The end io callbacks free our dip, do the final put on io_bio
8590 * and all the cleanup and final put for dio_bio (through
8591 * dio_end_io()).
8592 */
8593 dip = NULL;
8594 io_bio = NULL;
8595 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008596 if (write)
8597 btrfs_endio_direct_write_update_ordered(inode,
8598 file_offset,
8599 dio_bio->bi_iter.bi_size,
8600 0);
8601 else
Filipe Manana61de7182015-07-01 12:13:10 +01008602 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8603 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008604
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008605 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008606 /*
8607 * Releases and cleans up our dio_bio, no need to bio_put()
8608 * nor bio_endio()/bio_io_error() against dio_bio.
8609 */
8610 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008611 }
Filipe Manana61de7182015-07-01 12:13:10 +01008612 if (io_bio)
8613 bio_put(io_bio);
8614 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008615}
8616
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008617static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8618 struct kiocb *iocb,
8619 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008620{
8621 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008622 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008623 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008624 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008625
8626 if (offset & blocksize_mask)
8627 goto out;
8628
Al Viro28060d52014-03-22 05:15:17 -04008629 if (iov_iter_alignment(iter) & blocksize_mask)
8630 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008631
Al Viro28060d52014-03-22 05:15:17 -04008632 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008633 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008634 return 0;
8635 /*
8636 * Check to make sure we don't have duplicate iov_base's in this
8637 * iovec, if so return EINVAL, otherwise we'll get csum errors
8638 * when reading back.
8639 */
8640 for (seg = 0; seg < iter->nr_segs; seg++) {
8641 for (i = seg + 1; i < iter->nr_segs; i++) {
8642 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008643 goto out;
8644 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008645 }
8646 retval = 0;
8647out:
8648 return retval;
8649}
Josef Bacikeb838e72012-07-31 16:28:48 -04008650
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008651static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008652{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008653 struct file *file = iocb->ki_filp;
8654 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008655 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308656 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008657 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008658 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008659 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008660 bool wakeup = true;
8661 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008662 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008663
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008664 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008665 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008666
Jens Axboefe0f07d2015-04-15 17:05:48 -06008667 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008668 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008669
Josef Bacik0e267c42013-07-02 10:38:02 -04008670 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008671 * The generic stuff only does filemap_write_and_wait_range, which
8672 * isn't enough if we've written compressed pages to this area, so
8673 * we need to flush the dirty pages again to make absolutely sure
8674 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008675 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008676 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008677 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8678 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008679 filemap_fdatawrite_range(inode->i_mapping, offset,
8680 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008681
Omar Sandoval6f673762015-03-16 04:33:52 -07008682 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008683 /*
8684 * If the write DIO is beyond the EOF, we need update
8685 * the isize, but it is protected by i_mutex. So we can
8686 * not unlock the i_mutex at this case.
8687 */
8688 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008689 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008690 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008691 relock = true;
8692 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008693 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008694 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008695 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008696 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008697
8698 /*
8699 * We need to know how many extents we reserved so that we can
8700 * do the accounting properly if we go over the number we
8701 * originally calculated. Abuse current->journal_info for this.
8702 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008703 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008704 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008705 dio_data.unsubmitted_oe_range_start = (u64)offset;
8706 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308707 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308708 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008709 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8710 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008711 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008712 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8713 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008714 }
8715
Omar Sandoval17f8c842015-03-16 04:33:50 -07008716 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008717 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008718 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008719 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008720 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308721 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008722 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008723 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308724 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008725 btrfs_delalloc_release_space(inode, offset,
8726 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008727 /*
8728 * On error we might have left some ordered extents
8729 * without submitting corresponding bios for them, so
8730 * cleanup them up to avoid other tasks getting them
8731 * and waiting for them to complete forever.
8732 */
8733 if (dio_data.unsubmitted_oe_range_start <
8734 dio_data.unsubmitted_oe_range_end)
8735 btrfs_endio_direct_write_update_ordered(inode,
8736 dio_data.unsubmitted_oe_range_start,
8737 dio_data.unsubmitted_oe_range_end -
8738 dio_data.unsubmitted_oe_range_start,
8739 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008740 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008741 btrfs_delalloc_release_space(inode, offset,
8742 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008743 }
Miao Xie38851cc2013-02-08 07:04:11 +00008744out:
Miao Xie2e60a512013-02-08 07:01:08 +00008745 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008746 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008747 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008748 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008749
8750 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008751}
8752
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008753#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8754
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008755static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8756 __u64 start, __u64 len)
8757{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008758 int ret;
8759
8760 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8761 if (ret)
8762 return ret;
8763
Chris Masonec29ed52011-02-23 16:23:20 -05008764 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008765}
8766
Chris Mason9ebefb182007-06-15 13:50:00 -04008767int btrfs_readpage(struct file *file, struct page *page)
8768{
Chris Masond1310b22008-01-24 16:13:08 -05008769 struct extent_io_tree *tree;
8770 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008771 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008772}
Chris Mason1832a6d2007-12-21 16:27:21 -05008773
Chris Mason39279cc2007-06-12 06:35:45 -04008774static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8775{
Chris Masond1310b22008-01-24 16:13:08 -05008776 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008777 struct inode *inode = page->mapping->host;
8778 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008779
8780 if (current->flags & PF_MEMALLOC) {
8781 redirty_page_for_writepage(wbc, page);
8782 unlock_page(page);
8783 return 0;
8784 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008785
8786 /*
8787 * If we are under memory pressure we will call this directly from the
8788 * VM, we need to make sure we have the inode referenced for the ordered
8789 * extent. If not just return like we didn't do anything.
8790 */
8791 if (!igrab(inode)) {
8792 redirty_page_for_writepage(wbc, page);
8793 return AOP_WRITEPAGE_ACTIVATE;
8794 }
Chris Masond1310b22008-01-24 16:13:08 -05008795 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008796 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8797 btrfs_add_delayed_iput(inode);
8798 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008799}
Chris Mason39279cc2007-06-12 06:35:45 -04008800
Eric Sandeen48a3b632013-04-25 20:41:01 +00008801static int btrfs_writepages(struct address_space *mapping,
8802 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008803{
Chris Masond1310b22008-01-24 16:13:08 -05008804 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008805
Chris Masond1310b22008-01-24 16:13:08 -05008806 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008807 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8808}
8809
Chris Mason3ab2fb52007-11-08 10:59:22 -05008810static int
8811btrfs_readpages(struct file *file, struct address_space *mapping,
8812 struct list_head *pages, unsigned nr_pages)
8813{
Chris Masond1310b22008-01-24 16:13:08 -05008814 struct extent_io_tree *tree;
8815 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008816 return extent_readpages(tree, mapping, pages, nr_pages,
8817 btrfs_get_extent);
8818}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008819static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008820{
Chris Masond1310b22008-01-24 16:13:08 -05008821 struct extent_io_tree *tree;
8822 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008823 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008824
Chris Masond1310b22008-01-24 16:13:08 -05008825 tree = &BTRFS_I(page->mapping->host)->io_tree;
8826 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008827 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008828 if (ret == 1) {
8829 ClearPagePrivate(page);
8830 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008831 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008832 }
8833 return ret;
8834}
Chris Mason39279cc2007-06-12 06:35:45 -04008835
Chris Masone6dcd2d2008-07-17 12:53:50 -04008836static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8837{
Chris Mason98509cf2008-09-11 15:51:43 -04008838 if (PageWriteback(page) || PageDirty(page))
8839 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008840 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008841}
8842
Lukas Czernerd47992f2013-05-21 23:17:23 -04008843static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8844 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008845{
Josef Bacik5fd02042012-05-02 14:00:54 -04008846 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008847 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008848 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008849 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008850 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008851 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308852 u64 start;
8853 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008854 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008855
Chris Mason8b62b722009-09-02 16:53:46 -04008856 /*
8857 * we have the page locked, so new writeback can't start,
8858 * and the dirty bit won't be cleared while we are here.
8859 *
8860 * Wait for IO on this page so that we can safely clear
8861 * the PagePrivate2 bit and do ordered accounting
8862 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008863 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008864
Josef Bacik5fd02042012-05-02 14:00:54 -04008865 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008866 if (offset) {
8867 btrfs_releasepage(page, GFP_NOFS);
8868 return;
8869 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008870
8871 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008872 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308873again:
8874 start = page_start;
8875 ordered = btrfs_lookup_ordered_range(inode, start,
8876 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008877 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308878 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008879 /*
8880 * IO on this page will never be started, so we need
8881 * to account for any ordered extents now
8882 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008883 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308884 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008885 EXTENT_DIRTY | EXTENT_DELALLOC |
8886 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8887 EXTENT_DEFRAG, 1, 0, &cached_state,
8888 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008889 /*
8890 * whoever cleared the private bit is responsible
8891 * for the finish_ordered_io
8892 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008893 if (TestClearPagePrivate2(page)) {
8894 struct btrfs_ordered_inode_tree *tree;
8895 u64 new_len;
8896
8897 tree = &BTRFS_I(inode)->ordered_tree;
8898
8899 spin_lock_irq(&tree->lock);
8900 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308901 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008902 if (new_len < ordered->truncated_len)
8903 ordered->truncated_len = new_len;
8904 spin_unlock_irq(&tree->lock);
8905
8906 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308907 start,
8908 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008909 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008910 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008911 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008912 if (!inode_evicting) {
8913 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308914 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008915 &cached_state);
8916 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308917
8918 start = end + 1;
8919 if (start < page_end)
8920 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008921 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008922
Qu Wenruob9d0b382015-09-29 10:35:16 +08008923 /*
8924 * Qgroup reserved space handler
8925 * Page here will be either
8926 * 1) Already written to disk
8927 * In this case, its reserved space is released from data rsv map
8928 * and will be freed by delayed_ref handler finally.
8929 * So even we call qgroup_free_data(), it won't decrease reserved
8930 * space.
8931 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008932 * This means the reserved space should be freed here. However,
8933 * if a truncate invalidates the page (by clearing PageDirty)
8934 * and the page is accounted for while allocating extent
8935 * in btrfs_check_data_free_space() we let delayed_ref to
8936 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008937 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008938 if (PageDirty(page))
8939 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008940 if (!inode_evicting) {
8941 clear_extent_bit(tree, page_start, page_end,
8942 EXTENT_LOCKED | EXTENT_DIRTY |
8943 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8944 EXTENT_DEFRAG, 1, 1,
8945 &cached_state, GFP_NOFS);
8946
8947 __btrfs_releasepage(page, GFP_NOFS);
8948 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008949
Chris Mason4a096752008-07-21 10:29:44 -04008950 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008951 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008952 ClearPagePrivate(page);
8953 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008954 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008955 }
Chris Mason39279cc2007-06-12 06:35:45 -04008956}
8957
Chris Mason9ebefb182007-06-15 13:50:00 -04008958/*
8959 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8960 * called from a page fault handler when a page is first dirtied. Hence we must
8961 * be careful to check for EOF conditions here. We set the page up correctly
8962 * for a written page which means we get ENOSPC checking when writing into
8963 * holes and correct delalloc and unwritten extent mapping on filesystems that
8964 * support these features.
8965 *
8966 * We are not allowed to take the i_mutex here so we have to play games to
8967 * protect against truncate races as the page could now be beyond EOF. Because
8968 * vmtruncate() writes the inode size before removing pages, once we have the
8969 * page lock we can determine safely if the page is beyond EOF. If it is not
8970 * beyond EOF, then the page is guaranteed safe against truncation until we
8971 * unlock the page.
8972 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008973int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008974{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008975 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008976 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008977 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008978 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8979 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008980 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008981 char *kaddr;
8982 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008983 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008984 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008985 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308986 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008987 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008988 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308989 u64 end;
8990
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008991 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008992
Jan Karab2b5ef52012-06-12 16:20:45 +02008993 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008994 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008995 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308996 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008997
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308998 /*
8999 * Reserving delalloc space after obtaining the page lock can lead to
9000 * deadlock. For example, if a dirty page is locked by this function
9001 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9002 * dirty page write out, then the btrfs_writepage() function could
9003 * end up waiting indefinitely to get a lock on the page currently
9004 * being processed by btrfs_page_mkwrite() function.
9005 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009006 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309007 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009008 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04009009 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009010 reserved = 1;
9011 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009012 if (ret) {
9013 if (ret == -ENOMEM)
9014 ret = VM_FAULT_OOM;
9015 else /* -ENOSPC, -EIO, etc */
9016 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009017 if (reserved)
9018 goto out;
9019 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009020 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009021
Nick Piggin56a76f82009-03-31 15:23:23 -07009022 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009023again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009024 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009025 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009026
Chris Mason9ebefb182007-06-15 13:50:00 -04009027 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009028 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009029 /* page got truncated out from underneath us */
9030 goto out_unlock;
9031 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009032 wait_on_page_writeback(page);
9033
David Sterbaff13db42015-12-03 14:30:40 +01009034 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009035 set_page_extent_mapped(page);
9036
Chris Masoneb84ae02008-07-17 13:53:27 -04009037 /*
9038 * we can't set the delalloc bits if there are pending ordered
9039 * extents. Drop our locks and wait for them to finish
9040 */
Liu Bo9a9239a2017-01-24 15:58:51 -08009041 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009042 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009043 unlock_extent_cached(io_tree, page_start, page_end,
9044 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009045 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009046 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009047 btrfs_put_ordered_extent(ordered);
9048 goto again;
9049 }
9050
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009051 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009052 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009053 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009054 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309055 end = page_start + reserved_space - 1;
9056 spin_lock(&BTRFS_I(inode)->lock);
9057 BTRFS_I(inode)->outstanding_extents++;
9058 spin_unlock(&BTRFS_I(inode)->lock);
9059 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009060 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309061 }
9062 }
9063
Josef Bacikfbf19082009-10-01 17:10:23 -04009064 /*
Liu Bo54160342016-12-13 12:15:19 -08009065 * page_mkwrite gets called when the page is firstly dirtied after it's
9066 * faulted in, but write(2) could also dirty a page and set delalloc
9067 * bits, thus in this case for space account reason, we still need to
9068 * clear any delalloc bits within this page range since we have to
9069 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009070 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309071 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009072 EXTENT_DIRTY | EXTENT_DELALLOC |
9073 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009074 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009075
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309076 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009077 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009078 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009079 unlock_extent_cached(io_tree, page_start, page_end,
9080 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009081 ret = VM_FAULT_SIGBUS;
9082 goto out_unlock;
9083 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009084 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009085
9086 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009087 if (page_start + PAGE_SIZE > size)
9088 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009089 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009090 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009091
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009092 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009093 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009094 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009095 flush_dcache_page(page);
9096 kunmap(page);
9097 }
Chris Mason247e7432008-07-17 12:53:51 -04009098 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009099 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009100 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009101
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009102 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009103 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009104 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009105
Josef Bacik2ac55d42010-02-03 19:33:23 +00009106 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009107
9108out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009109 if (!ret) {
9110 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009111 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009112 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009113 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009114out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309115 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009116out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009117 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009118 return ret;
9119}
9120
Josef Bacika41ad392011-01-31 15:30:16 -05009121static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009122{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009123 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009124 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009125 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009126 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009127 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009128 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009129 u64 mask = fs_info->sectorsize - 1;
9130 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009131
Josef Bacik0ef8b722013-10-25 16:13:35 -04009132 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9133 (u64)-1);
9134 if (ret)
9135 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009136
Josef Bacikfcb80c22011-05-03 10:40:22 -04009137 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009138 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009139 * 3 things going on here
9140 *
9141 * 1) We need to reserve space for our orphan item and the space to
9142 * delete our orphan item. Lord knows we don't want to have a dangling
9143 * orphan item because we didn't reserve space to remove it.
9144 *
9145 * 2) We need to reserve space to update our inode.
9146 *
9147 * 3) We need to have something to cache all the space that is going to
9148 * be free'd up by the truncate operation, but also have some slack
9149 * space reserved in case it uses space during the truncate (thank you
9150 * very much snapshotting).
9151 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009152 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009153 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009154 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009155 * doesn't end up using space reserved for updating the inode or
9156 * removing the orphan item. We also need to be able to stop the
9157 * transaction and start a new one, which means we need to be able to
9158 * update the inode several times, and we have no idea of knowing how
9159 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009160 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009161 * Then there is the orphan item, which does indeed need to be held on
9162 * to for the whole operation, and we need nobody to touch this reserved
9163 * space except the orphan code.
9164 *
9165 * So that leaves us with
9166 *
9167 * 1) root->orphan_block_rsv - for the orphan deletion.
9168 * 2) rsv - for the truncate reservation, which we will steal from the
9169 * transaction reservation.
9170 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9171 * updating the inode.
9172 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009173 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009174 if (!rsv)
9175 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009176 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009177 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009178
Josef Bacik907cbce2011-08-08 13:46:15 -04009179 /*
Josef Bacik07127182011-08-19 10:29:59 -04009180 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009181 * 1 for updating the inode.
9182 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009183 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009184 if (IS_ERR(trans)) {
9185 err = PTR_ERR(trans);
9186 goto out;
9187 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009188
Josef Bacik907cbce2011-08-08 13:46:15 -04009189 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009190 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009191 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009192 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009193
Chris Mason5a3f23d2009-03-31 13:27:11 -04009194 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009195 * So if we truncate and then write and fsync we normally would just
9196 * write the extents that changed, which is a problem if we need to
9197 * first truncate that entire inode. So set this flag so we write out
9198 * all of the extents in the inode to the sync log so we're completely
9199 * safe.
9200 */
9201 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009202 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009203
Yan, Zheng80825102009-11-12 09:35:36 +00009204 while (1) {
9205 ret = btrfs_truncate_inode_items(trans, root, inode,
9206 inode->i_size,
9207 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009208 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009209 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009210 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009211 }
Chris Mason39279cc2007-06-12 06:35:45 -04009212
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009213 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009214 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009215 if (ret) {
9216 err = ret;
9217 break;
9218 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009219
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009220 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009221 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009222
9223 trans = btrfs_start_transaction(root, 2);
9224 if (IS_ERR(trans)) {
9225 ret = err = PTR_ERR(trans);
9226 trans = NULL;
9227 break;
9228 }
9229
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009230 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009231 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009232 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009233 BUG_ON(ret); /* shouldn't happen */
9234 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009235 }
9236
9237 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009238 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009239 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009240 if (ret)
9241 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009242 }
9243
Chris Mason917c16b2011-11-08 14:49:59 -05009244 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009245 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009246 ret = btrfs_update_inode(trans, root, inode);
9247 if (ret && !err)
9248 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009249
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009250 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009251 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009252 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009253out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009254 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009255
Josef Bacik3893e332011-01-31 16:03:11 -05009256 if (ret && !err)
9257 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009258
Josef Bacik3893e332011-01-31 16:03:11 -05009259 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009260}
9261
Sven Wegener3b963622008-06-09 21:57:42 -04009262/*
Chris Masond352ac62008-09-29 15:18:18 -04009263 * create a new subvolume directory/inode (helper for the ioctl).
9264 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009265int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009266 struct btrfs_root *new_root,
9267 struct btrfs_root *parent_root,
9268 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009269{
Chris Mason39279cc2007-06-12 06:35:45 -04009270 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009271 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009272 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009273
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009274 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9275 new_dirid, new_dirid,
9276 S_IFDIR | (~current_umask() & S_IRWXUGO),
9277 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009278 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009279 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009280 inode->i_op = &btrfs_dir_inode_operations;
9281 inode->i_fop = &btrfs_dir_file_operations;
9282
Miklos Szeredibfe86842011-10-28 14:13:29 +02009283 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009284 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009285 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009286
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009287 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9288 if (err)
9289 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009290 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009291 new_root->root_key.objectid, err);
9292
Yan, Zheng76dda932009-09-21 16:00:26 -04009293 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009294
Yan, Zheng76dda932009-09-21 16:00:26 -04009295 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009296 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009297}
9298
Chris Mason39279cc2007-06-12 06:35:45 -04009299struct inode *btrfs_alloc_inode(struct super_block *sb)
9300{
9301 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009302 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009303
9304 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9305 if (!ei)
9306 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009307
9308 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009309 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009310 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009311 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009312 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009313 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009314 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009315 ei->disk_i_size = 0;
9316 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009317 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009318 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009319 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009320 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009321 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009322 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009323
Josef Bacik9e0baf62011-07-15 15:16:44 +00009324 spin_lock_init(&ei->lock);
9325 ei->outstanding_extents = 0;
9326 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009327
Josef Bacik72ac3c02012-05-23 14:13:11 -04009328 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009329 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009330
Miao Xie16cdcec2011-04-22 18:12:22 +08009331 ei->delayed_node = NULL;
9332
chandan r9cc97d62012-07-04 12:48:07 +05309333 ei->i_otime.tv_sec = 0;
9334 ei->i_otime.tv_nsec = 0;
9335
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009336 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009337 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009338 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9339 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009340 ei->io_tree.track_uptodate = 1;
9341 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009342 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009343 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009344 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009345 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009346 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009347 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009348 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009349 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009350
9351 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009352}
9353
Josef Bacikaaedb552013-10-11 14:44:09 -04009354#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9355void btrfs_test_destroy_inode(struct inode *inode)
9356{
9357 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9358 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9359}
9360#endif
9361
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009362static void btrfs_i_callback(struct rcu_head *head)
9363{
9364 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009365 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9366}
9367
Chris Mason39279cc2007-06-12 06:35:45 -04009368void btrfs_destroy_inode(struct inode *inode)
9369{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009370 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009371 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009372 struct btrfs_root *root = BTRFS_I(inode)->root;
9373
Al Virob3d9b7a2012-06-09 13:51:19 -04009374 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009375 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009376 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9377 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009378 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9379 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009380 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009381
Chris Mason5a3f23d2009-03-31 13:27:11 -04009382 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009383 * This can happen where we create an inode, but somebody else also
9384 * created the same inode and we need to destroy the one we already
9385 * created.
9386 */
9387 if (!root)
9388 goto free;
9389
Josef Bacik8a35d952012-05-23 14:26:42 -04009390 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9391 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009392 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009393 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009394 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009395 }
Josef Bacik7b128762008-07-24 12:17:14 -04009396
Chris Masond3977122009-01-05 21:25:51 -05009397 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009398 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9399 if (!ordered)
9400 break;
9401 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009402 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009403 "found ordered extent %llu %llu on inode cleanup",
9404 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009405 btrfs_remove_ordered_extent(inode, ordered);
9406 btrfs_put_ordered_extent(ordered);
9407 btrfs_put_ordered_extent(ordered);
9408 }
9409 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009410 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009411 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009412 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009413free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009414 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009415}
9416
Al Viro45321ac2010-06-07 13:43:19 -04009417int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009418{
9419 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009420
Naohiro Aota6379ef92013-06-06 09:56:34 +00009421 if (root == NULL)
9422 return 1;
9423
Liu Bofa6ac872013-02-20 14:10:23 +00009424 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009425 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009426 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009427 else
Al Viro45321ac2010-06-07 13:43:19 -04009428 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009429}
9430
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009431static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009432{
9433 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9434
9435 inode_init_once(&ei->vfs_inode);
9436}
9437
9438void btrfs_destroy_cachep(void)
9439{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009440 /*
9441 * Make sure all delayed rcu free inodes are flushed before we
9442 * destroy cache.
9443 */
9444 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009445 kmem_cache_destroy(btrfs_inode_cachep);
9446 kmem_cache_destroy(btrfs_trans_handle_cachep);
9447 kmem_cache_destroy(btrfs_transaction_cachep);
9448 kmem_cache_destroy(btrfs_path_cachep);
9449 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009450}
9451
9452int btrfs_init_cachep(void)
9453{
David Sterba837e1972012-09-07 03:00:48 -06009454 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009455 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009456 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9457 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009458 if (!btrfs_inode_cachep)
9459 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009460
David Sterba837e1972012-09-07 03:00:48 -06009461 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009462 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009463 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009464 if (!btrfs_trans_handle_cachep)
9465 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009466
David Sterba837e1972012-09-07 03:00:48 -06009467 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009468 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009469 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009470 if (!btrfs_transaction_cachep)
9471 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009472
David Sterba837e1972012-09-07 03:00:48 -06009473 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009474 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009475 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009476 if (!btrfs_path_cachep)
9477 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009478
David Sterba837e1972012-09-07 03:00:48 -06009479 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009480 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009481 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009482 if (!btrfs_free_space_cachep)
9483 goto fail;
9484
Chris Mason39279cc2007-06-12 06:35:45 -04009485 return 0;
9486fail:
9487 btrfs_destroy_cachep();
9488 return -ENOMEM;
9489}
9490
9491static int btrfs_getattr(struct vfsmount *mnt,
9492 struct dentry *dentry, struct kstat *stat)
9493{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009494 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009495 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009496 u32 blocksize = inode->i_sb->s_blocksize;
9497
Chris Mason39279cc2007-06-12 06:35:45 -04009498 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009499 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009500
9501 spin_lock(&BTRFS_I(inode)->lock);
9502 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9503 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009504 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009505 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009506 return 0;
9507}
9508
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009509static int btrfs_rename_exchange(struct inode *old_dir,
9510 struct dentry *old_dentry,
9511 struct inode *new_dir,
9512 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009513{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009514 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009515 struct btrfs_trans_handle *trans;
9516 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009517 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009518 struct inode *new_inode = new_dentry->d_inode;
9519 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009520 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009521 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009522 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9523 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009524 u64 old_idx = 0;
9525 u64 new_idx = 0;
9526 u64 root_objectid;
9527 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009528 bool root_log_pinned = false;
9529 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009530
9531 /* we only allow rename subvolume link between subvolumes */
9532 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9533 return -EXDEV;
9534
9535 /* close the race window with snapshot create/destroy ioctl */
9536 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009537 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009538 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009539 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009540
9541 /*
9542 * We want to reserve the absolute worst case amount of items. So if
9543 * both inodes are subvols and we need to unlink them then that would
9544 * require 4 item modifications, but if they are both normal inodes it
9545 * would require 5 item modifications, so we'll assume their normal
9546 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9547 * should cover the worst case number of items we'll modify.
9548 */
9549 trans = btrfs_start_transaction(root, 12);
9550 if (IS_ERR(trans)) {
9551 ret = PTR_ERR(trans);
9552 goto out_notrans;
9553 }
9554
9555 /*
9556 * We need to find a free sequence number both in the source and
9557 * in the destination directory for the exchange.
9558 */
9559 ret = btrfs_set_inode_index(new_dir, &old_idx);
9560 if (ret)
9561 goto out_fail;
9562 ret = btrfs_set_inode_index(old_dir, &new_idx);
9563 if (ret)
9564 goto out_fail;
9565
9566 BTRFS_I(old_inode)->dir_index = 0ULL;
9567 BTRFS_I(new_inode)->dir_index = 0ULL;
9568
9569 /* Reference for the source. */
9570 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9571 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009572 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009573 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009574 btrfs_pin_log_trans(root);
9575 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009576 ret = btrfs_insert_inode_ref(trans, dest,
9577 new_dentry->d_name.name,
9578 new_dentry->d_name.len,
9579 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009580 btrfs_ino(BTRFS_I(new_dir)),
9581 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009582 if (ret)
9583 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009584 }
9585
9586 /* And now for the dest. */
9587 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9588 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009589 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009590 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009591 btrfs_pin_log_trans(dest);
9592 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009593 ret = btrfs_insert_inode_ref(trans, root,
9594 old_dentry->d_name.name,
9595 old_dentry->d_name.len,
9596 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009597 btrfs_ino(BTRFS_I(old_dir)),
9598 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009599 if (ret)
9600 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009601 }
9602
9603 /* Update inode version and ctime/mtime. */
9604 inode_inc_iversion(old_dir);
9605 inode_inc_iversion(new_dir);
9606 inode_inc_iversion(old_inode);
9607 inode_inc_iversion(new_inode);
9608 old_dir->i_ctime = old_dir->i_mtime = ctime;
9609 new_dir->i_ctime = new_dir->i_mtime = ctime;
9610 old_inode->i_ctime = ctime;
9611 new_inode->i_ctime = ctime;
9612
9613 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009614 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9615 BTRFS_I(old_inode), 1);
9616 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9617 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618 }
9619
9620 /* src is a subvolume */
9621 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9622 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9623 ret = btrfs_unlink_subvol(trans, root, old_dir,
9624 root_objectid,
9625 old_dentry->d_name.name,
9626 old_dentry->d_name.len);
9627 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009628 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9629 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009630 old_dentry->d_name.name,
9631 old_dentry->d_name.len);
9632 if (!ret)
9633 ret = btrfs_update_inode(trans, root, old_inode);
9634 }
9635 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009636 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009637 goto out_fail;
9638 }
9639
9640 /* dest is a subvolume */
9641 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9642 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9643 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9644 root_objectid,
9645 new_dentry->d_name.name,
9646 new_dentry->d_name.len);
9647 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009648 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9649 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 new_dentry->d_name.name,
9651 new_dentry->d_name.len);
9652 if (!ret)
9653 ret = btrfs_update_inode(trans, dest, new_inode);
9654 }
9655 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009656 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 goto out_fail;
9658 }
9659
9660 ret = btrfs_add_link(trans, new_dir, old_inode,
9661 new_dentry->d_name.name,
9662 new_dentry->d_name.len, 0, old_idx);
9663 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009664 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009665 goto out_fail;
9666 }
9667
9668 ret = btrfs_add_link(trans, old_dir, new_inode,
9669 old_dentry->d_name.name,
9670 old_dentry->d_name.len, 0, new_idx);
9671 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009672 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009673 goto out_fail;
9674 }
9675
9676 if (old_inode->i_nlink == 1)
9677 BTRFS_I(old_inode)->dir_index = old_idx;
9678 if (new_inode->i_nlink == 1)
9679 BTRFS_I(new_inode)->dir_index = new_idx;
9680
Filipe Manana86e8aa02016-05-05 02:02:27 +01009681 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009682 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009683 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9684 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009685 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009686 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009687 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009688 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009689 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009690 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9691 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009693 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694 }
9695out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009696 /*
9697 * If we have pinned a log and an error happened, we unpin tasks
9698 * trying to sync the log and force them to fallback to a transaction
9699 * commit if the log currently contains any of the inodes involved in
9700 * this rename operation (to ensure we do not persist a log with an
9701 * inconsistent state for any of these inodes or leading to any
9702 * inconsistencies when replayed). If the transaction was aborted, the
9703 * abortion reason is propagated to userspace when attempting to commit
9704 * the transaction. If the log does not contain any of these inodes, we
9705 * allow the tasks to sync it.
9706 */
9707 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009708 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9709 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9710 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009711 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009712 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009713 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009714
9715 if (root_log_pinned) {
9716 btrfs_end_log_trans(root);
9717 root_log_pinned = false;
9718 }
9719 if (dest_log_pinned) {
9720 btrfs_end_log_trans(dest);
9721 dest_log_pinned = false;
9722 }
9723 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009724 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009725out_notrans:
9726 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009727 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009728 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009729 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009730
9731 return ret;
9732}
9733
9734static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9735 struct btrfs_root *root,
9736 struct inode *dir,
9737 struct dentry *dentry)
9738{
9739 int ret;
9740 struct inode *inode;
9741 u64 objectid;
9742 u64 index;
9743
9744 ret = btrfs_find_free_ino(root, &objectid);
9745 if (ret)
9746 return ret;
9747
9748 inode = btrfs_new_inode(trans, root, dir,
9749 dentry->d_name.name,
9750 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009751 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009752 objectid,
9753 S_IFCHR | WHITEOUT_MODE,
9754 &index);
9755
9756 if (IS_ERR(inode)) {
9757 ret = PTR_ERR(inode);
9758 return ret;
9759 }
9760
9761 inode->i_op = &btrfs_special_inode_operations;
9762 init_special_inode(inode, inode->i_mode,
9763 WHITEOUT_DEV);
9764
9765 ret = btrfs_init_inode_security(trans, inode, dir,
9766 &dentry->d_name);
9767 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009768 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009769
9770 ret = btrfs_add_nondir(trans, dir, dentry,
9771 inode, 0, index);
9772 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009773 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009774
9775 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009776out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009777 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009778 if (ret)
9779 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009780 iput(inode);
9781
Filipe Mananac9901612016-05-05 01:41:57 +01009782 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009783}
9784
Chris Mason39279cc2007-06-12 06:35:45 -04009785static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009786 struct inode *new_dir, struct dentry *new_dentry,
9787 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009788{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009789 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009790 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009791 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009792 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9793 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009794 struct inode *new_inode = d_inode(new_dentry);
9795 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009796 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009797 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009798 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009799 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009800 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009801
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009802 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009803 return -EPERM;
9804
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009805 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009806 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009807 return -EXDEV;
9808
Li Zefan33345d012011-04-20 10:31:50 +08009809 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009810 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009811 return -ENOTEMPTY;
9812
Chris Mason39279cc2007-06-12 06:35:45 -04009813 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009814 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009815 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009816
9817
9818 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009819 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009820 new_dentry->d_name.name,
9821 new_dentry->d_name.len);
9822
9823 if (ret) {
9824 if (ret == -EEXIST) {
9825 /* we shouldn't get
9826 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309827 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009828 return ret;
9829 }
9830 } else {
9831 /* maybe -EOVERFLOW */
9832 return ret;
9833 }
9834 }
9835 ret = 0;
9836
Chris Mason5a3f23d2009-03-31 13:27:11 -04009837 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009838 * we're using rename to replace one file with another. Start IO on it
9839 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009840 */
Chris Mason8d875f92014-08-12 10:47:42 -07009841 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009842 filemap_flush(old_inode->i_mapping);
9843
Yan, Zheng76dda932009-09-21 16:00:26 -04009844 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009845 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009846 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009847 /*
9848 * We want to reserve the absolute worst case amount of items. So if
9849 * both inodes are subvols and we need to unlink them then that would
9850 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009851 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009852 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9853 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009854 * If our rename has the whiteout flag, we need more 5 units for the
9855 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9856 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009857 */
Filipe Manana5062af32016-05-05 10:26:26 +01009858 trans_num_items = 11;
9859 if (flags & RENAME_WHITEOUT)
9860 trans_num_items += 5;
9861 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009862 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009863 ret = PTR_ERR(trans);
9864 goto out_notrans;
9865 }
Chris Mason5f39d392007-10-15 16:14:19 -04009866
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009867 if (dest != root)
9868 btrfs_record_root_in_trans(trans, dest);
9869
Yan, Zhenga5719522009-09-24 09:17:31 -04009870 ret = btrfs_set_inode_index(new_dir, &index);
9871 if (ret)
9872 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009873
Miao Xie67de1172013-12-26 13:07:06 +08009874 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009875 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009876 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009877 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009878 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009879 btrfs_pin_log_trans(root);
9880 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009881 ret = btrfs_insert_inode_ref(trans, dest,
9882 new_dentry->d_name.name,
9883 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009884 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009885 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009886 if (ret)
9887 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009888 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009889
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009890 inode_inc_iversion(old_dir);
9891 inode_inc_iversion(new_dir);
9892 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009893 old_dir->i_ctime = old_dir->i_mtime =
9894 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009895 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009896
Chris Mason12fcfd22009-03-24 10:24:20 -04009897 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009898 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9899 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009900
Li Zefan33345d012011-04-20 10:31:50 +08009901 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009902 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9903 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9904 old_dentry->d_name.name,
9905 old_dentry->d_name.len);
9906 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009907 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9908 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009909 old_dentry->d_name.name,
9910 old_dentry->d_name.len);
9911 if (!ret)
9912 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009913 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009914 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009915 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009916 goto out_fail;
9917 }
Chris Mason39279cc2007-06-12 06:35:45 -04009918
9919 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009920 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009921 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009922 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009923 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9924 root_objectid = BTRFS_I(new_inode)->location.objectid;
9925 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9926 root_objectid,
9927 new_dentry->d_name.name,
9928 new_dentry->d_name.len);
9929 BUG_ON(new_inode->i_nlink == 0);
9930 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009931 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9932 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009933 new_dentry->d_name.name,
9934 new_dentry->d_name.len);
9935 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009936 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009937 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009938 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009939 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009940 goto out_fail;
9941 }
Chris Mason39279cc2007-06-12 06:35:45 -04009942 }
Josef Bacikaec74772008-07-24 12:12:38 -04009943
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009944 ret = btrfs_add_link(trans, new_dir, old_inode,
9945 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009946 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009947 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009948 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009949 goto out_fail;
9950 }
Chris Mason39279cc2007-06-12 06:35:45 -04009951
Miao Xie67de1172013-12-26 13:07:06 +08009952 if (old_inode->i_nlink == 1)
9953 BTRFS_I(old_inode)->dir_index = index;
9954
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009955 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009956 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009957
David Sterbaf85b7372017-01-20 14:54:07 +01009958 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9959 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009960 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009961 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009962 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009963
9964 if (flags & RENAME_WHITEOUT) {
9965 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9966 old_dentry);
9967
9968 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009969 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009970 goto out_fail;
9971 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009972 }
Chris Mason39279cc2007-06-12 06:35:45 -04009973out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009974 /*
9975 * If we have pinned the log and an error happened, we unpin tasks
9976 * trying to sync the log and force them to fallback to a transaction
9977 * commit if the log currently contains any of the inodes involved in
9978 * this rename operation (to ensure we do not persist a log with an
9979 * inconsistent state for any of these inodes or leading to any
9980 * inconsistencies when replayed). If the transaction was aborted, the
9981 * abortion reason is propagated to userspace when attempting to commit
9982 * the transaction. If the log does not contain any of these inodes, we
9983 * allow the tasks to sync it.
9984 */
9985 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009986 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9987 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9988 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009989 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009990 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009991 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009992
9993 btrfs_end_log_trans(root);
9994 log_pinned = false;
9995 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009996 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009997out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009998 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009999 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010000
Chris Mason39279cc2007-06-12 06:35:45 -040010001 return ret;
10002}
10003
Miklos Szeredi80ace852014-07-23 15:15:32 +020010004static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10005 struct inode *new_dir, struct dentry *new_dentry,
10006 unsigned int flags)
10007{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010008 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010009 return -EINVAL;
10010
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010011 if (flags & RENAME_EXCHANGE)
10012 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10013 new_dentry);
10014
10015 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010016}
10017
Miao Xie8ccf6f192012-10-25 09:28:04 +000010018static void btrfs_run_delalloc_work(struct btrfs_work *work)
10019{
10020 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010021 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010022
10023 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10024 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010025 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010026 filemap_flush(inode->i_mapping);
10027 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10028 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010029 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010030
10031 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010032 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010033 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010034 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010035 complete(&delalloc_work->completion);
10036}
10037
10038struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010039 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010040{
10041 struct btrfs_delalloc_work *work;
10042
David Sterba100d5702015-12-08 14:39:32 +010010043 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010044 if (!work)
10045 return NULL;
10046
10047 init_completion(&work->completion);
10048 INIT_LIST_HEAD(&work->list);
10049 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010050 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010051 WARN_ON_ONCE(!inode);
10052 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10053 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010054
10055 return work;
10056}
10057
10058void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10059{
10060 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010061 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010062}
10063
Chris Masond352ac62008-09-29 15:18:18 -040010064/*
10065 * some fairly slow code that needs optimization. This walks the list
10066 * of all the inodes with pending delalloc and forces them to disk.
10067 */
Miao Xie6c255e62014-03-06 13:55:01 +080010068static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10069 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010070{
Chris Masonea8c2812008-08-04 23:17:27 -040010071 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010072 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010073 struct btrfs_delalloc_work *work, *next;
10074 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010075 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010076 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010077
Miao Xie8ccf6f192012-10-25 09:28:04 +000010078 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010079 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010080
Miao Xie573bfb72014-03-06 13:55:03 +080010081 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010082 spin_lock(&root->delalloc_lock);
10083 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010084 while (!list_empty(&splice)) {
10085 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010086 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010087
Miao Xieeb73c1b2013-05-15 07:48:22 +000010088 list_move_tail(&binode->delalloc_inodes,
10089 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010090 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010091 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010092 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010093 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010094 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010095 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010096
David Sterba651d4942015-11-27 19:24:16 +010010097 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010098 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010099 if (delay_iput)
10100 btrfs_add_delayed_iput(inode);
10101 else
10102 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010103 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010104 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010105 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010106 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010107 btrfs_queue_work(root->fs_info->flush_workers,
10108 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010109 ret++;
10110 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010111 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010112 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010113 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010114 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010115 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010116
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010117out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010118 list_for_each_entry_safe(work, next, &works, list) {
10119 list_del_init(&work->list);
10120 btrfs_wait_and_free_delalloc_work(work);
10121 }
10122
Miao Xieeb73c1b2013-05-15 07:48:22 +000010123 if (!list_empty_careful(&splice)) {
10124 spin_lock(&root->delalloc_lock);
10125 list_splice_tail(&splice, &root->delalloc_inodes);
10126 spin_unlock(&root->delalloc_lock);
10127 }
Miao Xie573bfb72014-03-06 13:55:03 +080010128 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010129 return ret;
10130}
10131
10132int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10133{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010134 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010135 int ret;
10136
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010137 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010138 return -EROFS;
10139
Miao Xie6c255e62014-03-06 13:55:01 +080010140 ret = __start_delalloc_inodes(root, delay_iput, -1);
10141 if (ret > 0)
10142 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010143 /*
10144 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010145 * we have to make sure the IO is actually started and that
10146 * ordered extents get created before we return
10147 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010148 atomic_inc(&fs_info->async_submit_draining);
10149 while (atomic_read(&fs_info->nr_async_submits) ||
10150 atomic_read(&fs_info->async_delalloc_pages)) {
10151 wait_event(fs_info->async_submit_wait,
10152 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10153 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010154 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010155 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010156 return ret;
10157}
10158
Miao Xie6c255e62014-03-06 13:55:01 +080010159int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10160 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010161{
10162 struct btrfs_root *root;
10163 struct list_head splice;
10164 int ret;
10165
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010166 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010167 return -EROFS;
10168
10169 INIT_LIST_HEAD(&splice);
10170
Miao Xie573bfb72014-03-06 13:55:03 +080010171 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010172 spin_lock(&fs_info->delalloc_root_lock);
10173 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010174 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010175 root = list_first_entry(&splice, struct btrfs_root,
10176 delalloc_root);
10177 root = btrfs_grab_fs_root(root);
10178 BUG_ON(!root);
10179 list_move_tail(&root->delalloc_root,
10180 &fs_info->delalloc_roots);
10181 spin_unlock(&fs_info->delalloc_root_lock);
10182
Miao Xie6c255e62014-03-06 13:55:01 +080010183 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010184 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010185 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010186 goto out;
10187
Miao Xie6c255e62014-03-06 13:55:01 +080010188 if (nr != -1) {
10189 nr -= ret;
10190 WARN_ON(nr < 0);
10191 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010192 spin_lock(&fs_info->delalloc_root_lock);
10193 }
10194 spin_unlock(&fs_info->delalloc_root_lock);
10195
Miao Xie6c255e62014-03-06 13:55:01 +080010196 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010197 atomic_inc(&fs_info->async_submit_draining);
10198 while (atomic_read(&fs_info->nr_async_submits) ||
10199 atomic_read(&fs_info->async_delalloc_pages)) {
10200 wait_event(fs_info->async_submit_wait,
10201 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10202 atomic_read(&fs_info->async_delalloc_pages) == 0));
10203 }
10204 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010205out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010206 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010207 spin_lock(&fs_info->delalloc_root_lock);
10208 list_splice_tail(&splice, &fs_info->delalloc_roots);
10209 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010210 }
Miao Xie573bfb72014-03-06 13:55:03 +080010211 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010212 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010213}
10214
Chris Mason39279cc2007-06-12 06:35:45 -040010215static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10216 const char *symname)
10217{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010218 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010219 struct btrfs_trans_handle *trans;
10220 struct btrfs_root *root = BTRFS_I(dir)->root;
10221 struct btrfs_path *path;
10222 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010223 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010224 int err;
10225 int drop_inode = 0;
10226 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010227 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010228 int name_len;
10229 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010230 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010231 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010232 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010233
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010234 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010235 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010236 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010237
Josef Bacik9ed74f22009-09-11 16:12:44 -040010238 /*
10239 * 2 items for inode item and ref
10240 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010241 * 1 item for updating parent inode item
10242 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010243 * 1 item for xattr if selinux is on
10244 */
Filipe Manana9269d122015-12-31 18:16:29 +000010245 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010246 if (IS_ERR(trans))
10247 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010248
Li Zefan581bb052011-04-20 10:06:11 +080010249 err = btrfs_find_free_ino(root, &objectid);
10250 if (err)
10251 goto out_unlock;
10252
Josef Bacikaec74772008-07-24 12:12:38 -040010253 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010254 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10255 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010256 if (IS_ERR(inode)) {
10257 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010258 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010259 }
Chris Mason39279cc2007-06-12 06:35:45 -040010260
Casey Schauflerad19db72011-12-15 10:09:07 -050010261 /*
10262 * If the active LSM wants to access the inode during
10263 * d_instantiate it needs these. Smack checks to see
10264 * if the filesystem supports xattrs by looking at the
10265 * ops vector.
10266 */
10267 inode->i_fop = &btrfs_file_operations;
10268 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010269 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010270 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10271
10272 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10273 if (err)
10274 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010275
Chris Mason39279cc2007-06-12 06:35:45 -040010276 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010277 if (!path) {
10278 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010279 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010280 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010281 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010282 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010283 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010284 datasize = btrfs_file_extent_calc_inline_size(name_len);
10285 err = btrfs_insert_empty_item(trans, root, path, &key,
10286 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010287 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010288 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010289 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010290 }
Chris Mason5f39d392007-10-15 16:14:19 -040010291 leaf = path->nodes[0];
10292 ei = btrfs_item_ptr(leaf, path->slots[0],
10293 struct btrfs_file_extent_item);
10294 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10295 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010296 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010297 btrfs_set_file_extent_encryption(leaf, ei, 0);
10298 btrfs_set_file_extent_compression(leaf, ei, 0);
10299 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10300 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10301
Chris Mason39279cc2007-06-12 06:35:45 -040010302 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010303 write_extent_buffer(leaf, symname, ptr, name_len);
10304 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010305 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010306
Chris Mason39279cc2007-06-12 06:35:45 -040010307 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010308 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010309 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010310 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010311 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010312 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010313 /*
10314 * Last step, add directory indexes for our symlink inode. This is the
10315 * last step to avoid extra cleanup of these indexes if an error happens
10316 * elsewhere above.
10317 */
10318 if (!err)
10319 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010320 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010321 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010322 goto out_unlock_inode;
10323 }
10324
10325 unlock_new_inode(inode);
10326 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010327
10328out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010329 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010330 if (drop_inode) {
10331 inode_dec_link_count(inode);
10332 iput(inode);
10333 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010334 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010335 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010336
10337out_unlock_inode:
10338 drop_inode = 1;
10339 unlock_new_inode(inode);
10340 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010341}
Chris Mason16432982008-04-10 10:23:21 -040010342
Josef Bacik0af3d002010-06-21 14:48:16 -040010343static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10344 u64 start, u64 num_bytes, u64 min_size,
10345 loff_t actual_len, u64 *alloc_hint,
10346 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010347{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010348 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010349 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10350 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010351 struct btrfs_root *root = BTRFS_I(inode)->root;
10352 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010353 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010354 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010355 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010356 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010357 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010358 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010359 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010360
Josef Bacik0af3d002010-06-21 14:48:16 -040010361 if (trans)
10362 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010363 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010364 if (own_trans) {
10365 trans = btrfs_start_transaction(root, 3);
10366 if (IS_ERR(trans)) {
10367 ret = PTR_ERR(trans);
10368 break;
10369 }
Yan Zhengd899e052008-10-30 14:25:28 -040010370 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010371
Byongho Leeee221842015-12-15 01:42:10 +090010372 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010373 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010374 /*
10375 * If we are severely fragmented we could end up with really
10376 * small allocations, so if the allocator is returning small
10377 * chunks lets make its job easier by only searching for those
10378 * sized chunks.
10379 */
10380 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010381 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10382 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010383 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010384 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010385 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010386 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010387 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010388 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010389
Josef Bacik0b670dc2015-09-23 17:11:16 -040010390 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010391 ret = insert_reserved_file_extent(trans, inode,
10392 cur_offset, ins.objectid,
10393 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010394 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010395 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010396 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010397 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010398 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010399 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010400 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010401 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010402 break;
10403 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010404
Chris Masona1ed8352009-09-11 12:27:37 -040010405 btrfs_drop_extent_cache(inode, cur_offset,
10406 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010407
Josef Bacik5dc562c2012-08-17 13:14:17 -040010408 em = alloc_extent_map();
10409 if (!em) {
10410 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10411 &BTRFS_I(inode)->runtime_flags);
10412 goto next;
10413 }
10414
10415 em->start = cur_offset;
10416 em->orig_start = cur_offset;
10417 em->len = ins.offset;
10418 em->block_start = ins.objectid;
10419 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010420 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010421 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010422 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010423 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10424 em->generation = trans->transid;
10425
10426 while (1) {
10427 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010428 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010429 write_unlock(&em_tree->lock);
10430 if (ret != -EEXIST)
10431 break;
10432 btrfs_drop_extent_cache(inode, cur_offset,
10433 cur_offset + ins.offset - 1,
10434 0);
10435 }
10436 free_extent_map(em);
10437next:
Yan Zhengd899e052008-10-30 14:25:28 -040010438 num_bytes -= ins.offset;
10439 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010440 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010441
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010442 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010443 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010444 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010445 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010446 (actual_len > inode->i_size) &&
10447 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010448 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010449 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010450 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010451 i_size = cur_offset;
10452 i_size_write(inode, i_size);
10453 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010454 }
10455
Yan Zhengd899e052008-10-30 14:25:28 -040010456 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010457
10458 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010459 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010460 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010461 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010462 break;
10463 }
Yan Zhengd899e052008-10-30 14:25:28 -040010464
Josef Bacik0af3d002010-06-21 14:48:16 -040010465 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010466 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010467 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010468 if (cur_offset < end)
10469 btrfs_free_reserved_data_space(inode, cur_offset,
10470 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010471 return ret;
10472}
10473
Josef Bacik0af3d002010-06-21 14:48:16 -040010474int btrfs_prealloc_file_range(struct inode *inode, int mode,
10475 u64 start, u64 num_bytes, u64 min_size,
10476 loff_t actual_len, u64 *alloc_hint)
10477{
10478 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10479 min_size, actual_len, alloc_hint,
10480 NULL);
10481}
10482
10483int btrfs_prealloc_file_range_trans(struct inode *inode,
10484 struct btrfs_trans_handle *trans, int mode,
10485 u64 start, u64 num_bytes, u64 min_size,
10486 loff_t actual_len, u64 *alloc_hint)
10487{
10488 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10489 min_size, actual_len, alloc_hint, trans);
10490}
10491
Chris Masone6dcd2d2008-07-17 12:53:50 -040010492static int btrfs_set_page_dirty(struct page *page)
10493{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010494 return __set_page_dirty_nobuffers(page);
10495}
10496
Al Viro10556cb2011-06-20 19:28:19 -040010497static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010498{
Li Zefanb83cc962010-12-20 16:04:08 +080010499 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010500 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010501
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010502 if (mask & MAY_WRITE &&
10503 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10504 if (btrfs_root_readonly(root))
10505 return -EROFS;
10506 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10507 return -EACCES;
10508 }
Al Viro2830ba72011-06-20 19:16:29 -040010509 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010510}
Chris Mason39279cc2007-06-12 06:35:45 -040010511
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010512static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10513{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010514 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010515 struct btrfs_trans_handle *trans;
10516 struct btrfs_root *root = BTRFS_I(dir)->root;
10517 struct inode *inode = NULL;
10518 u64 objectid;
10519 u64 index;
10520 int ret = 0;
10521
10522 /*
10523 * 5 units required for adding orphan entry
10524 */
10525 trans = btrfs_start_transaction(root, 5);
10526 if (IS_ERR(trans))
10527 return PTR_ERR(trans);
10528
10529 ret = btrfs_find_free_ino(root, &objectid);
10530 if (ret)
10531 goto out;
10532
10533 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010534 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010535 if (IS_ERR(inode)) {
10536 ret = PTR_ERR(inode);
10537 inode = NULL;
10538 goto out;
10539 }
10540
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010541 inode->i_fop = &btrfs_file_operations;
10542 inode->i_op = &btrfs_file_inode_operations;
10543
10544 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010545 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10546
Chris Masonb0d5d102014-09-08 13:08:51 -070010547 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10548 if (ret)
10549 goto out_inode;
10550
10551 ret = btrfs_update_inode(trans, root, inode);
10552 if (ret)
10553 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010554 ret = btrfs_orphan_add(trans, inode);
10555 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010556 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010557
Filipe Manana5762b5c2014-08-01 00:10:32 +010010558 /*
10559 * We set number of links to 0 in btrfs_new_inode(), and here we set
10560 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10561 * through:
10562 *
10563 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10564 */
10565 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010566 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010567 d_tmpfile(dentry, inode);
10568 mark_inode_dirty(inode);
10569
10570out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010571 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010572 if (ret)
10573 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010574 btrfs_balance_delayed_items(fs_info);
10575 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010576 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010577
10578out_inode:
10579 unlock_new_inode(inode);
10580 goto out;
10581
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010582}
10583
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010584static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010585 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010586 .lookup = btrfs_lookup,
10587 .create = btrfs_create,
10588 .unlink = btrfs_unlink,
10589 .link = btrfs_link,
10590 .mkdir = btrfs_mkdir,
10591 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010592 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010593 .symlink = btrfs_symlink,
10594 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010595 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010596 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010597 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010598 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010599 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010600 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010601 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010602};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010603static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010604 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010605 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010606 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010607};
Yan, Zheng76dda932009-09-21 16:00:26 -040010608
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010609static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010610 .llseek = generic_file_llseek,
10611 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010612 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010613 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010614#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010615 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010616#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010617 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010618 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010619};
10620
David Sterba20e55062015-11-19 11:42:28 +010010621static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010622 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010623 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010624 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010625 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010626 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010627 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010628 .set_bit_hook = btrfs_set_bit_hook,
10629 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010630 .merge_extent_hook = btrfs_merge_extent_hook,
10631 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010632};
10633
Chris Mason35054392009-01-21 13:11:13 -050010634/*
10635 * btrfs doesn't support the bmap operation because swapfiles
10636 * use bmap to make a mapping of extents in the file. They assume
10637 * these extents won't change over the life of the file and they
10638 * use the bmap result to do IO directly to the drive.
10639 *
10640 * the btrfs bmap call would return logical addresses that aren't
10641 * suitable for IO and they also will change frequently as COW
10642 * operations happen. So, swapfile + btrfs == corruption.
10643 *
10644 * For now we're avoiding this by dropping bmap.
10645 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010646static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010647 .readpage = btrfs_readpage,
10648 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010649 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010650 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010651 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010652 .invalidatepage = btrfs_invalidatepage,
10653 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010654 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010655 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010656};
10657
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010658static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010659 .readpage = btrfs_readpage,
10660 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010661 .invalidatepage = btrfs_invalidatepage,
10662 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010663};
10664
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010665static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010666 .getattr = btrfs_getattr,
10667 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010668 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010669 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010670 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010671 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010672 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010673 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010674};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010675static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010676 .getattr = btrfs_getattr,
10677 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010678 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010679 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010680 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010681 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010682 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010683};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010684static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010685 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010686 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010687 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010688 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010689 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010690 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010691};
Yan, Zheng76dda932009-09-21 16:00:26 -040010692
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010693const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010694 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010695 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010696};