blob: 92416f3ea9a851f1dff0fdc24867d5e2b92ea075 [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;
1250 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001251 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001253 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 u64 cow_start;
1255 u64 cur_offset;
1256 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001257 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 u64 disk_bytenr;
1259 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001260 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001261 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001263 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001264 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 int nocow;
1266 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001267 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001268 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001269
1270 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001271 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001272 extent_clear_unlock_delalloc(inode, start, end, end,
1273 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001274 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001275 EXTENT_DO_ACCOUNTING |
1276 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001277 PAGE_CLEAR_DIRTY |
1278 PAGE_SET_WRITEBACK |
1279 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001280 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001281 }
Li Zefan82d59022011-04-20 10:33:24 +08001282
Liu Bo83eea1f2012-07-10 05:28:39 -06001283 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001284
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 cow_start = (u64)-1;
1286 cur_offset = start;
1287 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001288 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001290 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001291 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1293 leaf = path->nodes[0];
1294 btrfs_item_key_to_cpu(leaf, &found_key,
1295 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001296 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 found_key.type == BTRFS_EXTENT_DATA_KEY)
1298 path->slots[0]--;
1299 }
1300 check_prev = 0;
1301next_slot:
1302 leaf = path->nodes[0];
1303 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1304 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001305 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001306 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 if (ret > 0)
1308 break;
1309 leaf = path->nodes[0];
1310 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001311
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 nocow = 0;
1313 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001314 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001316
Filipe Manana1d512cb2015-11-09 00:33:58 +00001317 if (found_key.objectid > ino)
1318 break;
1319 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1320 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1321 path->slots[0]++;
1322 goto next_slot;
1323 }
1324 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 found_key.offset > end)
1326 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001327
Yan Zheng80ff3852008-10-30 14:20:02 -04001328 if (found_key.offset > cur_offset) {
1329 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001330 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 goto out_check;
1332 }
Chris Masonc31f8832008-01-08 15:46:31 -05001333
Yan Zheng80ff3852008-10-30 14:20:02 -04001334 fi = btrfs_item_ptr(leaf, path->slots[0],
1335 struct btrfs_file_extent_item);
1336 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001337
Josef Bacikcc95bef2013-04-04 14:31:27 -04001338 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001339 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1340 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001342 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 extent_end = found_key.offset +
1344 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001345 disk_num_bytes =
1346 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 if (extent_end <= start) {
1348 path->slots[0]++;
1349 goto next_slot;
1350 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001351 if (disk_bytenr == 0)
1352 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 if (btrfs_file_extent_compression(leaf, fi) ||
1354 btrfs_file_extent_encryption(leaf, fi) ||
1355 btrfs_file_extent_other_encoding(leaf, fi))
1356 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001357 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1358 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001359 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001361 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001362 found_key.offset -
1363 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001364 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001365 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001366 disk_bytenr += cur_offset - found_key.offset;
1367 num_bytes = min(end + 1, extent_end) - cur_offset;
1368 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001369 * if there are pending snapshots for this root,
1370 * we fall into common COW way.
1371 */
1372 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001373 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001374 if (!err)
1375 goto out_check;
1376 }
1377 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001378 * force cow if csum exists in the range.
1379 * this ensure that csum for a given extent are
1380 * either valid or do not exist.
1381 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001382 if (csum_exist_in_range(fs_info, disk_bytenr,
1383 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001384 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001385 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001386 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001387 nocow = 1;
1388 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1389 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001390 btrfs_file_extent_inline_len(leaf,
1391 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001392 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001393 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001394 } else {
1395 BUG_ON(1);
1396 }
1397out_check:
1398 if (extent_end <= start) {
1399 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001400 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001401 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001402 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001403 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001404 goto next_slot;
1405 }
1406 if (!nocow) {
1407 if (cow_start == (u64)-1)
1408 cow_start = cur_offset;
1409 cur_offset = extent_end;
1410 if (cur_offset > end)
1411 break;
1412 path->slots[0]++;
1413 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001414 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001415
David Sterbab3b4aa72011-04-21 01:20:15 +02001416 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001417 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001418 ret = cow_file_range(inode, locked_page,
1419 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001420 end, page_started, nr_written, 1,
1421 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001422 if (ret) {
1423 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001424 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001425 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001426 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001427 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001428 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001429 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001430 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001431 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001432
Yan Zhengd899e052008-10-30 14:25:28 -04001433 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1434 struct extent_map *em;
1435 struct extent_map_tree *em_tree;
1436 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001437 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001438 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001439 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001440 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001441 em->len = num_bytes;
1442 em->block_len = num_bytes;
1443 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001444 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001445 em->ram_bytes = ram_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001446 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001447 em->mod_start = em->start;
1448 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001449 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001450 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001451 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001452 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001453 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001454 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001455 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001456 if (ret != -EEXIST) {
1457 free_extent_map(em);
1458 break;
1459 }
1460 btrfs_drop_extent_cache(inode, em->start,
1461 em->start + em->len - 1, 0);
1462 }
1463 type = BTRFS_ORDERED_PREALLOC;
1464 } else {
1465 type = BTRFS_ORDERED_NOCOW;
1466 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001467
1468 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001469 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001470 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001471 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001472 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001473
Yan, Zhengefa56462010-05-16 10:49:59 -04001474 if (root->root_key.objectid ==
1475 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1476 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1477 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001478 if (ret) {
1479 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001480 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001481 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001482 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001483 }
1484
Josef Bacikc2790a22013-07-29 11:20:47 -04001485 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001486 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001487 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001488 EXTENT_DELALLOC |
1489 EXTENT_CLEAR_DATA_RESV,
1490 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1491
Wang Shilonge9894fd2014-03-27 11:12:25 +08001492 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001493 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001494 cur_offset = extent_end;
1495 if (cur_offset > end)
1496 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001497 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001498 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001499
Josef Bacik17ca04a2012-05-31 15:58:55 -04001500 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001501 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001502 cur_offset = end;
1503 }
1504
Yan Zheng80ff3852008-10-30 14:20:02 -04001505 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001506 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1507 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001508 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001509 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001510 }
1511
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001512error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001513 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001514 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001515 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001516 EXTENT_DELALLOC | EXTENT_DEFRAG |
1517 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1518 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001519 PAGE_SET_WRITEBACK |
1520 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001521 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001522 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001523}
1524
Wang Shilong47059d92014-07-03 18:22:07 +08001525static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1526{
1527
1528 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1529 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1530 return 0;
1531
1532 /*
1533 * @defrag_bytes is a hint value, no spinlock held here,
1534 * if is not zero, it means the file is defragging.
1535 * Force cow if given extent needs to be defragged.
1536 */
1537 if (BTRFS_I(inode)->defrag_bytes &&
1538 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1539 EXTENT_DEFRAG, 0, NULL))
1540 return 1;
1541
1542 return 0;
1543}
1544
Chris Masond352ac62008-09-29 15:18:18 -04001545/*
1546 * extent_io.c call back to do delayed allocation processing
1547 */
Chris Masonc8b97812008-10-29 14:49:59 -04001548static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001549 u64 start, u64 end, int *page_started,
1550 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001551{
Chris Masonbe20aa92007-12-17 20:14:01 -05001552 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001553 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001554
Wang Shilong47059d92014-07-03 18:22:07 +08001555 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001556 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001557 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001558 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001559 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001560 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001561 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001562 ret = cow_file_range(inode, locked_page, start, end, end,
1563 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001564 } else {
1565 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1566 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001567 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001568 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001569 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001570 return ret;
1571}
1572
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001573static void btrfs_split_extent_hook(struct inode *inode,
1574 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001575{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001576 u64 size;
1577
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001578 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001579 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001580 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001581
Josef Bacikdcab6a32015-02-11 15:08:59 -05001582 size = orig->end - orig->start + 1;
1583 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001584 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001585 u64 new_size;
1586
1587 /*
Josef Bacikba117212015-03-13 15:01:24 -04001588 * See the explanation in btrfs_merge_extent_hook, the same
1589 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001590 */
1591 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001592 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001593 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001594 num_extents += count_max_extents(new_size);
1595 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001596 return;
1597 }
1598
Josef Bacik9e0baf62011-07-15 15:16:44 +00001599 spin_lock(&BTRFS_I(inode)->lock);
1600 BTRFS_I(inode)->outstanding_extents++;
1601 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001602}
1603
1604/*
1605 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1606 * extents so we can keep track of new extents that are just merged onto old
1607 * extents, such as when we are doing sequential writes, so we can properly
1608 * account for the metadata space we'll need.
1609 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001610static void btrfs_merge_extent_hook(struct inode *inode,
1611 struct extent_state *new,
1612 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001613{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001614 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001615 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001616
Josef Bacik9ed74f22009-09-11 16:12:44 -04001617 /* not delalloc, ignore it */
1618 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001619 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001620
Josef Bacik8461a3d2015-03-13 15:12:08 -04001621 if (new->start > other->start)
1622 new_size = new->end - other->start + 1;
1623 else
1624 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001625
1626 /* we're not bigger than the max, unreserve the space and go */
1627 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1628 spin_lock(&BTRFS_I(inode)->lock);
1629 BTRFS_I(inode)->outstanding_extents--;
1630 spin_unlock(&BTRFS_I(inode)->lock);
1631 return;
1632 }
1633
1634 /*
Josef Bacikba117212015-03-13 15:01:24 -04001635 * We have to add up either side to figure out how many extents were
1636 * accounted for before we merged into one big extent. If the number of
1637 * extents we accounted for is <= the amount we need for the new range
1638 * then we can return, otherwise drop. Think of it like this
1639 *
1640 * [ 4k][MAX_SIZE]
1641 *
1642 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1643 * need 2 outstanding extents, on one side we have 1 and the other side
1644 * we have 1 so they are == and we can return. But in this case
1645 *
1646 * [MAX_SIZE+4k][MAX_SIZE+4k]
1647 *
1648 * Each range on their own accounts for 2 extents, but merged together
1649 * they are only 3 extents worth of accounting, so we need to drop in
1650 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001651 */
Josef Bacikba117212015-03-13 15:01:24 -04001652 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001653 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001654 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001655 num_extents += count_max_extents(old_size);
1656 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001657 return;
1658
Josef Bacik9e0baf62011-07-15 15:16:44 +00001659 spin_lock(&BTRFS_I(inode)->lock);
1660 BTRFS_I(inode)->outstanding_extents--;
1661 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001662}
1663
Miao Xieeb73c1b2013-05-15 07:48:22 +00001664static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1665 struct inode *inode)
1666{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001667 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1668
Miao Xieeb73c1b2013-05-15 07:48:22 +00001669 spin_lock(&root->delalloc_lock);
1670 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1671 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1672 &root->delalloc_inodes);
1673 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1674 &BTRFS_I(inode)->runtime_flags);
1675 root->nr_delalloc_inodes++;
1676 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001677 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001678 BUG_ON(!list_empty(&root->delalloc_root));
1679 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001680 &fs_info->delalloc_roots);
1681 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001682 }
1683 }
1684 spin_unlock(&root->delalloc_lock);
1685}
1686
1687static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1688 struct inode *inode)
1689{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001690 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1691
Miao Xieeb73c1b2013-05-15 07:48:22 +00001692 spin_lock(&root->delalloc_lock);
1693 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1694 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1695 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1696 &BTRFS_I(inode)->runtime_flags);
1697 root->nr_delalloc_inodes--;
1698 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001699 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001700 BUG_ON(list_empty(&root->delalloc_root));
1701 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001702 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001703 }
1704 }
1705 spin_unlock(&root->delalloc_lock);
1706}
1707
Chris Masond352ac62008-09-29 15:18:18 -04001708/*
1709 * extent_io.c set_bit_hook, used to track delayed allocation
1710 * bytes in this file, and to maintain the list of inodes that
1711 * have pending delalloc work to be done.
1712 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001713static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001714 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001715{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001716
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001717 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1718
Wang Shilong47059d92014-07-03 18:22:07 +08001719 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1720 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001721 /*
1722 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001723 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001724 * bit, which is only set or cleared with irqs on
1725 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001726 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001727 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001728 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001729 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001730
Josef Bacik9e0baf62011-07-15 15:16:44 +00001731 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001732 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001733 } else {
1734 spin_lock(&BTRFS_I(inode)->lock);
1735 BTRFS_I(inode)->outstanding_extents++;
1736 spin_unlock(&BTRFS_I(inode)->lock);
1737 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001738
Josef Bacik6a3891c2015-03-16 17:38:52 -04001739 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001740 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001741 return;
1742
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001743 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1744 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001745 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001746 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001747 if (*bits & EXTENT_DEFRAG)
1748 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001749 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001750 &BTRFS_I(inode)->runtime_flags))
1751 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001752 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001753 }
Chris Mason291d6732008-01-29 15:55:23 -05001754}
1755
Chris Masond352ac62008-09-29 15:18:18 -04001756/*
1757 * extent_io.c clear_bit_hook, see set_bit_hook for why
1758 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001759static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001760 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001761 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001762{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001763 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001764 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001765 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001766
1767 spin_lock(&BTRFS_I(inode)->lock);
1768 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1769 BTRFS_I(inode)->defrag_bytes -= len;
1770 spin_unlock(&BTRFS_I(inode)->lock);
1771
Chris Mason75eff682008-12-15 15:54:40 -05001772 /*
1773 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001774 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001775 * bit, which is only set or cleared with irqs on
1776 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001777 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001778 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001779 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001780
Josef Bacik9e0baf62011-07-15 15:16:44 +00001781 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001782 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001783 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1784 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001785 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001786 spin_unlock(&BTRFS_I(inode)->lock);
1787 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001788
Josef Bacikb6d08f02013-09-27 14:57:43 -04001789 /*
1790 * We don't reserve metadata space for space cache inodes so we
1791 * don't need to call dellalloc_release_metadata if there is an
1792 * error.
1793 */
1794 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001795 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001796 btrfs_delalloc_release_metadata(inode, len);
1797
Josef Bacik6a3891c2015-03-16 17:38:52 -04001798 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001799 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001800 return;
1801
Josef Bacik0cb59c92010-07-02 12:14:14 -04001802 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001803 && do_list && !(state->state & EXTENT_NORESERVE)
1804 && (*bits & (EXTENT_DO_ACCOUNTING |
1805 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001806 btrfs_free_reserved_data_space_noquota(inode,
1807 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001808
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001809 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1810 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001811 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001812 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001813 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001814 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001815 &BTRFS_I(inode)->runtime_flags))
1816 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001817 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001818 }
Chris Mason291d6732008-01-29 15:55:23 -05001819}
1820
Chris Masond352ac62008-09-29 15:18:18 -04001821/*
1822 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1823 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001824 *
1825 * return 1 if page cannot be merged to bio
1826 * return 0 if page can be merged to bio
1827 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001828 */
Mike Christie81a75f672016-06-05 14:31:54 -05001829int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001830 size_t size, struct bio *bio,
1831 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001832{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001833 struct inode *inode = page->mapping->host;
1834 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001835 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001836 u64 length = 0;
1837 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001838 int ret;
1839
Chris Mason771ed682008-11-06 22:02:51 -05001840 if (bio_flags & EXTENT_BIO_COMPRESSED)
1841 return 0;
1842
Kent Overstreet4f024f32013-10-11 15:44:27 -07001843 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001844 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1846 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001847 if (ret < 0)
1848 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001849 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001850 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001851 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001852}
1853
Chris Masond352ac62008-09-29 15:18:18 -04001854/*
1855 * in order to insert checksums into the metadata in large chunks,
1856 * we wait until bio submission time. All the pages in the bio are
1857 * checksummed and sums are attached onto the ordered extent record.
1858 *
1859 * At IO completion time the cums attached on the ordered extent record
1860 * are inserted into the btree
1861 */
Mike Christie81a75f672016-06-05 14:31:54 -05001862static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1863 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001864 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001865{
Chris Mason065631f2008-02-20 12:07:25 -05001866 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001867
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001868 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001869 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001870 return 0;
1871}
Chris Masone0156402008-04-16 11:15:20 -04001872
Chris Mason4a69a412008-11-06 22:03:00 -05001873/*
1874 * in order to insert checksums into the metadata in large chunks,
1875 * we wait until bio submission time. All the pages in the bio are
1876 * checksummed and sums are attached onto the ordered extent record.
1877 *
1878 * At IO completion time the cums attached on the ordered extent record
1879 * are inserted into the btree
1880 */
Mike Christie81a75f672016-06-05 14:31:54 -05001881static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001882 int mirror_num, unsigned long bio_flags,
1883 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001884{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001885 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001886 int ret;
1887
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001888 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001889 if (ret) {
1890 bio->bi_error = ret;
1891 bio_endio(bio);
1892 }
Stefan Behrens61891922012-11-05 18:51:52 +01001893 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001894}
1895
Chris Masond352ac62008-09-29 15:18:18 -04001896/*
Chris Masoncad321a2008-12-17 14:51:42 -05001897 * extent_io.c submission hook. This does the right thing for csum calculation
1898 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001899 */
Mike Christie81a75f672016-06-05 14:31:54 -05001900static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001901 int mirror_num, unsigned long bio_flags,
1902 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001903{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001904 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001905 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301906 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001907 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001908 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001909 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001910
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001911 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001912
Liu Bo83eea1f2012-07-10 05:28:39 -06001913 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301914 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001915
Mike Christie37226b22016-06-05 14:31:52 -05001916 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001917 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001918 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001919 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001920
Chris Masond20f7042008-12-08 16:58:54 -05001921 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001922 ret = btrfs_submit_compressed_read(inode, bio,
1923 mirror_num,
1924 bio_flags);
1925 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001926 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001927 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001928 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001929 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001930 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001931 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001932 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001933 /* csum items have already been cloned */
1934 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1935 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001936 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001937 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1938 bio_flags, bio_offset,
1939 __btrfs_submit_bio_start,
1940 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001941 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001942 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001943 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001944 if (ret)
1945 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001946 }
1947
Chris Mason0b86a832008-03-24 15:01:56 -04001948mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001949 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001950
1951out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001952 if (ret < 0) {
1953 bio->bi_error = ret;
1954 bio_endio(bio);
1955 }
Stefan Behrens61891922012-11-05 18:51:52 +01001956 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001957}
Chris Mason6885f302008-02-20 16:11:05 -05001958
Chris Masond352ac62008-09-29 15:18:18 -04001959/*
1960 * given a list of ordered sums record them in the inode. This happens
1961 * at IO completion time based on sums calculated at bio submission time.
1962 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001963static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001964 struct inode *inode, u64 file_offset,
1965 struct list_head *list)
1966{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001967 struct btrfs_ordered_sum *sum;
1968
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001969 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001970 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001971 btrfs_csum_file_blocks(trans,
1972 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001973 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001974 }
1975 return 0;
1976}
1977
Josef Bacik2ac55d42010-02-03 19:33:23 +00001978int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001979 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04001980{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001981 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001982 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001983 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001984}
1985
Chris Masond352ac62008-09-29 15:18:18 -04001986/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001987struct btrfs_writepage_fixup {
1988 struct page *page;
1989 struct btrfs_work work;
1990};
1991
Christoph Hellwigb2950862008-12-02 09:54:17 -05001992static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001993{
1994 struct btrfs_writepage_fixup *fixup;
1995 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001996 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001997 struct page *page;
1998 struct inode *inode;
1999 u64 page_start;
2000 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002001 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002002
2003 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2004 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002005again:
Chris Mason247e7432008-07-17 12:53:51 -04002006 lock_page(page);
2007 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2008 ClearPageChecked(page);
2009 goto out_page;
2010 }
2011
2012 inode = page->mapping->host;
2013 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002014 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002015
David Sterbaff13db42015-12-03 14:30:40 +01002016 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002017 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002018
2019 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002020 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002021 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002022
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302023 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002024 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002025 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002026 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2027 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002028 unlock_page(page);
2029 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002030 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002031 goto again;
2032 }
Chris Mason247e7432008-07-17 12:53:51 -04002033
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002034 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002035 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002036 if (ret) {
2037 mapping_set_error(page->mapping, ret);
2038 end_extent_writepage(page, ret, page_start, page_end);
2039 ClearPageChecked(page);
2040 goto out;
2041 }
2042
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002043 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2044 0);
Chris Mason247e7432008-07-17 12:53:51 -04002045 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002046 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002047out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002048 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2049 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002050out_page:
2051 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002052 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002053 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002054}
2055
2056/*
2057 * There are a few paths in the higher layers of the kernel that directly
2058 * set the page dirty bit without asking the filesystem if it is a
2059 * good idea. This causes problems because we want to make sure COW
2060 * properly happens and the data=ordered rules are followed.
2061 *
Chris Masonc8b97812008-10-29 14:49:59 -04002062 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002063 * hasn't been properly setup for IO. We kick off an async process
2064 * to fix it up. The async helper will wait for ordered extents, set
2065 * the delalloc bit and make it safe to write the page.
2066 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002067static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002068{
2069 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002070 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002071 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002072
Chris Mason8b62b722009-09-02 16:53:46 -04002073 /* this page is properly in the ordered list */
2074 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002075 return 0;
2076
2077 if (PageChecked(page))
2078 return -EAGAIN;
2079
2080 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2081 if (!fixup)
2082 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002083
Chris Mason247e7432008-07-17 12:53:51 -04002084 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002085 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002086 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2087 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002088 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002089 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002090 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002091}
2092
Yan Zhengd899e052008-10-30 14:25:28 -04002093static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2094 struct inode *inode, u64 file_pos,
2095 u64 disk_bytenr, u64 disk_num_bytes,
2096 u64 num_bytes, u64 ram_bytes,
2097 u8 compression, u8 encryption,
2098 u16 other_encoding, int extent_type)
2099{
2100 struct btrfs_root *root = BTRFS_I(inode)->root;
2101 struct btrfs_file_extent_item *fi;
2102 struct btrfs_path *path;
2103 struct extent_buffer *leaf;
2104 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002105 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002106 int ret;
2107
2108 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002109 if (!path)
2110 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002111
Chris Masona1ed8352009-09-11 12:27:37 -04002112 /*
2113 * we may be replacing one extent in the tree with another.
2114 * The new extent is pinned in the extent map, and we don't want
2115 * to drop it from the cache until it is completely in the btree.
2116 *
2117 * So, tell btrfs_drop_extents to leave this extent in the cache.
2118 * the caller is expected to unpin it and allow it to be merged
2119 * with the others.
2120 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002121 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2122 file_pos + num_bytes, NULL, 0,
2123 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002124 if (ret)
2125 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002126
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002127 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002128 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002129 ins.offset = file_pos;
2130 ins.type = BTRFS_EXTENT_DATA_KEY;
2131
2132 path->leave_spinning = 1;
2133 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2134 sizeof(*fi));
2135 if (ret)
2136 goto out;
2137 }
Yan Zhengd899e052008-10-30 14:25:28 -04002138 leaf = path->nodes[0];
2139 fi = btrfs_item_ptr(leaf, path->slots[0],
2140 struct btrfs_file_extent_item);
2141 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2142 btrfs_set_file_extent_type(leaf, fi, extent_type);
2143 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2144 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2145 btrfs_set_file_extent_offset(leaf, fi, 0);
2146 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2147 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2148 btrfs_set_file_extent_compression(leaf, fi, compression);
2149 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2150 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002151
Yan Zhengd899e052008-10-30 14:25:28 -04002152 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002153 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002154
2155 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002156
2157 ins.objectid = disk_bytenr;
2158 ins.offset = disk_num_bytes;
2159 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002160 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002161 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002162 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002163 * Release the reserved range from inode dirty range map, as it is
2164 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002165 */
2166 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002167out:
Yan Zhengd899e052008-10-30 14:25:28 -04002168 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002169
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002170 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002171}
2172
Liu Bo38c227d2013-01-29 03:18:40 +00002173/* snapshot-aware defrag */
2174struct sa_defrag_extent_backref {
2175 struct rb_node node;
2176 struct old_sa_defrag_extent *old;
2177 u64 root_id;
2178 u64 inum;
2179 u64 file_pos;
2180 u64 extent_offset;
2181 u64 num_bytes;
2182 u64 generation;
2183};
2184
2185struct old_sa_defrag_extent {
2186 struct list_head list;
2187 struct new_sa_defrag_extent *new;
2188
2189 u64 extent_offset;
2190 u64 bytenr;
2191 u64 offset;
2192 u64 len;
2193 int count;
2194};
2195
2196struct new_sa_defrag_extent {
2197 struct rb_root root;
2198 struct list_head head;
2199 struct btrfs_path *path;
2200 struct inode *inode;
2201 u64 file_pos;
2202 u64 len;
2203 u64 bytenr;
2204 u64 disk_len;
2205 u8 compress_type;
2206};
2207
2208static int backref_comp(struct sa_defrag_extent_backref *b1,
2209 struct sa_defrag_extent_backref *b2)
2210{
2211 if (b1->root_id < b2->root_id)
2212 return -1;
2213 else if (b1->root_id > b2->root_id)
2214 return 1;
2215
2216 if (b1->inum < b2->inum)
2217 return -1;
2218 else if (b1->inum > b2->inum)
2219 return 1;
2220
2221 if (b1->file_pos < b2->file_pos)
2222 return -1;
2223 else if (b1->file_pos > b2->file_pos)
2224 return 1;
2225
2226 /*
2227 * [------------------------------] ===> (a range of space)
2228 * |<--->| |<---->| =============> (fs/file tree A)
2229 * |<---------------------------->| ===> (fs/file tree B)
2230 *
2231 * A range of space can refer to two file extents in one tree while
2232 * refer to only one file extent in another tree.
2233 *
2234 * So we may process a disk offset more than one time(two extents in A)
2235 * and locate at the same extent(one extent in B), then insert two same
2236 * backrefs(both refer to the extent in B).
2237 */
2238 return 0;
2239}
2240
2241static void backref_insert(struct rb_root *root,
2242 struct sa_defrag_extent_backref *backref)
2243{
2244 struct rb_node **p = &root->rb_node;
2245 struct rb_node *parent = NULL;
2246 struct sa_defrag_extent_backref *entry;
2247 int ret;
2248
2249 while (*p) {
2250 parent = *p;
2251 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2252
2253 ret = backref_comp(backref, entry);
2254 if (ret < 0)
2255 p = &(*p)->rb_left;
2256 else
2257 p = &(*p)->rb_right;
2258 }
2259
2260 rb_link_node(&backref->node, parent, p);
2261 rb_insert_color(&backref->node, root);
2262}
2263
2264/*
2265 * Note the backref might has changed, and in this case we just return 0.
2266 */
2267static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2268 void *ctx)
2269{
2270 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002271 struct old_sa_defrag_extent *old = ctx;
2272 struct new_sa_defrag_extent *new = old->new;
2273 struct btrfs_path *path = new->path;
2274 struct btrfs_key key;
2275 struct btrfs_root *root;
2276 struct sa_defrag_extent_backref *backref;
2277 struct extent_buffer *leaf;
2278 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002280 int slot;
2281 int ret;
2282 u64 extent_offset;
2283 u64 num_bytes;
2284
2285 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002286 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002287 return 0;
2288
2289 key.objectid = root_id;
2290 key.type = BTRFS_ROOT_ITEM_KEY;
2291 key.offset = (u64)-1;
2292
Liu Bo38c227d2013-01-29 03:18:40 +00002293 root = btrfs_read_fs_root_no_name(fs_info, &key);
2294 if (IS_ERR(root)) {
2295 if (PTR_ERR(root) == -ENOENT)
2296 return 0;
2297 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002298 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002299 inum, offset, root_id);
2300 return PTR_ERR(root);
2301 }
2302
2303 key.objectid = inum;
2304 key.type = BTRFS_EXTENT_DATA_KEY;
2305 if (offset > (u64)-1 << 32)
2306 key.offset = 0;
2307 else
2308 key.offset = offset;
2309
2310 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302311 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002312 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002313 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002314
2315 while (1) {
2316 cond_resched();
2317
2318 leaf = path->nodes[0];
2319 slot = path->slots[0];
2320
2321 if (slot >= btrfs_header_nritems(leaf)) {
2322 ret = btrfs_next_leaf(root, path);
2323 if (ret < 0) {
2324 goto out;
2325 } else if (ret > 0) {
2326 ret = 0;
2327 goto out;
2328 }
2329 continue;
2330 }
2331
2332 path->slots[0]++;
2333
2334 btrfs_item_key_to_cpu(leaf, &key, slot);
2335
2336 if (key.objectid > inum)
2337 goto out;
2338
2339 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2340 continue;
2341
2342 extent = btrfs_item_ptr(leaf, slot,
2343 struct btrfs_file_extent_item);
2344
2345 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2346 continue;
2347
Liu Boe68afa42013-07-01 22:13:26 +08002348 /*
2349 * 'offset' refers to the exact key.offset,
2350 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2351 * (key.offset - extent_offset).
2352 */
2353 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002354 continue;
2355
Liu Boe68afa42013-07-01 22:13:26 +08002356 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002357 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002358
Liu Bo38c227d2013-01-29 03:18:40 +00002359 if (extent_offset >= old->extent_offset + old->offset +
2360 old->len || extent_offset + num_bytes <=
2361 old->extent_offset + old->offset)
2362 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002363 break;
2364 }
2365
2366 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2367 if (!backref) {
2368 ret = -ENOENT;
2369 goto out;
2370 }
2371
2372 backref->root_id = root_id;
2373 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002374 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002375 backref->num_bytes = num_bytes;
2376 backref->extent_offset = extent_offset;
2377 backref->generation = btrfs_file_extent_generation(leaf, extent);
2378 backref->old = old;
2379 backref_insert(&new->root, backref);
2380 old->count++;
2381out:
2382 btrfs_release_path(path);
2383 WARN_ON(ret);
2384 return ret;
2385}
2386
2387static noinline bool record_extent_backrefs(struct btrfs_path *path,
2388 struct new_sa_defrag_extent *new)
2389{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002390 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002391 struct old_sa_defrag_extent *old, *tmp;
2392 int ret;
2393
2394 new->path = path;
2395
2396 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002397 ret = iterate_inodes_from_logical(old->bytenr +
2398 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002399 path, record_one_backref,
2400 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002401 if (ret < 0 && ret != -ENOENT)
2402 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002403
2404 /* no backref to be processed for this extent */
2405 if (!old->count) {
2406 list_del(&old->list);
2407 kfree(old);
2408 }
2409 }
2410
2411 if (list_empty(&new->head))
2412 return false;
2413
2414 return true;
2415}
2416
2417static int relink_is_mergable(struct extent_buffer *leaf,
2418 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002419 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002420{
Liu Bo116e0022013-08-02 16:30:40 +08002421 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002422 return 0;
2423
2424 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2425 return 0;
2426
Liu Bo116e0022013-08-02 16:30:40 +08002427 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2428 return 0;
2429
2430 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002431 btrfs_file_extent_other_encoding(leaf, fi))
2432 return 0;
2433
2434 return 1;
2435}
2436
2437/*
2438 * Note the backref might has changed, and in this case we just return 0.
2439 */
2440static noinline int relink_extent_backref(struct btrfs_path *path,
2441 struct sa_defrag_extent_backref *prev,
2442 struct sa_defrag_extent_backref *backref)
2443{
2444 struct btrfs_file_extent_item *extent;
2445 struct btrfs_file_extent_item *item;
2446 struct btrfs_ordered_extent *ordered;
2447 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002448 struct btrfs_root *root;
2449 struct btrfs_key key;
2450 struct extent_buffer *leaf;
2451 struct old_sa_defrag_extent *old = backref->old;
2452 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002453 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002454 struct inode *inode;
2455 struct extent_state *cached = NULL;
2456 int ret = 0;
2457 u64 start;
2458 u64 len;
2459 u64 lock_start;
2460 u64 lock_end;
2461 bool merge = false;
2462 int index;
2463
2464 if (prev && prev->root_id == backref->root_id &&
2465 prev->inum == backref->inum &&
2466 prev->file_pos + prev->num_bytes == backref->file_pos)
2467 merge = true;
2468
2469 /* step 1: get root */
2470 key.objectid = backref->root_id;
2471 key.type = BTRFS_ROOT_ITEM_KEY;
2472 key.offset = (u64)-1;
2473
Liu Bo38c227d2013-01-29 03:18:40 +00002474 index = srcu_read_lock(&fs_info->subvol_srcu);
2475
2476 root = btrfs_read_fs_root_no_name(fs_info, &key);
2477 if (IS_ERR(root)) {
2478 srcu_read_unlock(&fs_info->subvol_srcu, index);
2479 if (PTR_ERR(root) == -ENOENT)
2480 return 0;
2481 return PTR_ERR(root);
2482 }
Liu Bo38c227d2013-01-29 03:18:40 +00002483
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002484 if (btrfs_root_readonly(root)) {
2485 srcu_read_unlock(&fs_info->subvol_srcu, index);
2486 return 0;
2487 }
2488
Liu Bo38c227d2013-01-29 03:18:40 +00002489 /* step 2: get inode */
2490 key.objectid = backref->inum;
2491 key.type = BTRFS_INODE_ITEM_KEY;
2492 key.offset = 0;
2493
2494 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2495 if (IS_ERR(inode)) {
2496 srcu_read_unlock(&fs_info->subvol_srcu, index);
2497 return 0;
2498 }
2499
2500 srcu_read_unlock(&fs_info->subvol_srcu, index);
2501
2502 /* step 3: relink backref */
2503 lock_start = backref->file_pos;
2504 lock_end = backref->file_pos + backref->num_bytes - 1;
2505 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002506 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002507
2508 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2509 if (ordered) {
2510 btrfs_put_ordered_extent(ordered);
2511 goto out_unlock;
2512 }
2513
2514 trans = btrfs_join_transaction(root);
2515 if (IS_ERR(trans)) {
2516 ret = PTR_ERR(trans);
2517 goto out_unlock;
2518 }
2519
2520 key.objectid = backref->inum;
2521 key.type = BTRFS_EXTENT_DATA_KEY;
2522 key.offset = backref->file_pos;
2523
2524 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2525 if (ret < 0) {
2526 goto out_free_path;
2527 } else if (ret > 0) {
2528 ret = 0;
2529 goto out_free_path;
2530 }
2531
2532 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2533 struct btrfs_file_extent_item);
2534
2535 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2536 backref->generation)
2537 goto out_free_path;
2538
2539 btrfs_release_path(path);
2540
2541 start = backref->file_pos;
2542 if (backref->extent_offset < old->extent_offset + old->offset)
2543 start += old->extent_offset + old->offset -
2544 backref->extent_offset;
2545
2546 len = min(backref->extent_offset + backref->num_bytes,
2547 old->extent_offset + old->offset + old->len);
2548 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2549
2550 ret = btrfs_drop_extents(trans, root, inode, start,
2551 start + len, 1);
2552 if (ret)
2553 goto out_free_path;
2554again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002555 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002556 key.type = BTRFS_EXTENT_DATA_KEY;
2557 key.offset = start;
2558
Liu Boa09a0a72013-03-11 09:20:58 +00002559 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002560 if (merge) {
2561 struct btrfs_file_extent_item *fi;
2562 u64 extent_len;
2563 struct btrfs_key found_key;
2564
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002565 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002566 if (ret < 0)
2567 goto out_free_path;
2568
2569 path->slots[0]--;
2570 leaf = path->nodes[0];
2571 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2572
2573 fi = btrfs_item_ptr(leaf, path->slots[0],
2574 struct btrfs_file_extent_item);
2575 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2576
Liu Bo116e0022013-08-02 16:30:40 +08002577 if (extent_len + found_key.offset == start &&
2578 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002579 btrfs_set_file_extent_num_bytes(leaf, fi,
2580 extent_len + len);
2581 btrfs_mark_buffer_dirty(leaf);
2582 inode_add_bytes(inode, len);
2583
2584 ret = 1;
2585 goto out_free_path;
2586 } else {
2587 merge = false;
2588 btrfs_release_path(path);
2589 goto again;
2590 }
2591 }
2592
2593 ret = btrfs_insert_empty_item(trans, root, path, &key,
2594 sizeof(*extent));
2595 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002596 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002597 goto out_free_path;
2598 }
2599
2600 leaf = path->nodes[0];
2601 item = btrfs_item_ptr(leaf, path->slots[0],
2602 struct btrfs_file_extent_item);
2603 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2604 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2605 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2606 btrfs_set_file_extent_num_bytes(leaf, item, len);
2607 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2608 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2609 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2610 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2611 btrfs_set_file_extent_encryption(leaf, item, 0);
2612 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2613
2614 btrfs_mark_buffer_dirty(leaf);
2615 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002616 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002617
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002618 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002619 new->disk_len, 0,
2620 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002621 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002622 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002623 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002624 goto out_free_path;
2625 }
2626
2627 ret = 1;
2628out_free_path:
2629 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002630 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002631 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002632out_unlock:
2633 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2634 &cached, GFP_NOFS);
2635 iput(inode);
2636 return ret;
2637}
2638
Liu Bo6f519562013-10-29 10:45:05 +08002639static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2640{
2641 struct old_sa_defrag_extent *old, *tmp;
2642
2643 if (!new)
2644 return;
2645
2646 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002647 kfree(old);
2648 }
2649 kfree(new);
2650}
2651
Liu Bo38c227d2013-01-29 03:18:40 +00002652static void relink_file_extents(struct new_sa_defrag_extent *new)
2653{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002654 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002655 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002656 struct sa_defrag_extent_backref *backref;
2657 struct sa_defrag_extent_backref *prev = NULL;
2658 struct inode *inode;
2659 struct btrfs_root *root;
2660 struct rb_node *node;
2661 int ret;
2662
2663 inode = new->inode;
2664 root = BTRFS_I(inode)->root;
2665
2666 path = btrfs_alloc_path();
2667 if (!path)
2668 return;
2669
2670 if (!record_extent_backrefs(path, new)) {
2671 btrfs_free_path(path);
2672 goto out;
2673 }
2674 btrfs_release_path(path);
2675
2676 while (1) {
2677 node = rb_first(&new->root);
2678 if (!node)
2679 break;
2680 rb_erase(node, &new->root);
2681
2682 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2683
2684 ret = relink_extent_backref(path, prev, backref);
2685 WARN_ON(ret < 0);
2686
2687 kfree(prev);
2688
2689 if (ret == 1)
2690 prev = backref;
2691 else
2692 prev = NULL;
2693 cond_resched();
2694 }
2695 kfree(prev);
2696
2697 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002698out:
Liu Bo6f519562013-10-29 10:45:05 +08002699 free_sa_defrag_extent(new);
2700
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002701 atomic_dec(&fs_info->defrag_running);
2702 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002703}
2704
2705static struct new_sa_defrag_extent *
2706record_old_file_extents(struct inode *inode,
2707 struct btrfs_ordered_extent *ordered)
2708{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002709 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002710 struct btrfs_root *root = BTRFS_I(inode)->root;
2711 struct btrfs_path *path;
2712 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002713 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002714 struct new_sa_defrag_extent *new;
2715 int ret;
2716
2717 new = kmalloc(sizeof(*new), GFP_NOFS);
2718 if (!new)
2719 return NULL;
2720
2721 new->inode = inode;
2722 new->file_pos = ordered->file_offset;
2723 new->len = ordered->len;
2724 new->bytenr = ordered->start;
2725 new->disk_len = ordered->disk_len;
2726 new->compress_type = ordered->compress_type;
2727 new->root = RB_ROOT;
2728 INIT_LIST_HEAD(&new->head);
2729
2730 path = btrfs_alloc_path();
2731 if (!path)
2732 goto out_kfree;
2733
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002734 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002735 key.type = BTRFS_EXTENT_DATA_KEY;
2736 key.offset = new->file_pos;
2737
2738 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2739 if (ret < 0)
2740 goto out_free_path;
2741 if (ret > 0 && path->slots[0] > 0)
2742 path->slots[0]--;
2743
2744 /* find out all the old extents for the file range */
2745 while (1) {
2746 struct btrfs_file_extent_item *extent;
2747 struct extent_buffer *l;
2748 int slot;
2749 u64 num_bytes;
2750 u64 offset;
2751 u64 end;
2752 u64 disk_bytenr;
2753 u64 extent_offset;
2754
2755 l = path->nodes[0];
2756 slot = path->slots[0];
2757
2758 if (slot >= btrfs_header_nritems(l)) {
2759 ret = btrfs_next_leaf(root, path);
2760 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002761 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002762 else if (ret > 0)
2763 break;
2764 continue;
2765 }
2766
2767 btrfs_item_key_to_cpu(l, &key, slot);
2768
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002769 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002770 break;
2771 if (key.type != BTRFS_EXTENT_DATA_KEY)
2772 break;
2773 if (key.offset >= new->file_pos + new->len)
2774 break;
2775
2776 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2777
2778 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2779 if (key.offset + num_bytes < new->file_pos)
2780 goto next;
2781
2782 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2783 if (!disk_bytenr)
2784 goto next;
2785
2786 extent_offset = btrfs_file_extent_offset(l, extent);
2787
2788 old = kmalloc(sizeof(*old), GFP_NOFS);
2789 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002790 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002791
2792 offset = max(new->file_pos, key.offset);
2793 end = min(new->file_pos + new->len, key.offset + num_bytes);
2794
2795 old->bytenr = disk_bytenr;
2796 old->extent_offset = extent_offset;
2797 old->offset = offset - key.offset;
2798 old->len = end - offset;
2799 old->new = new;
2800 old->count = 0;
2801 list_add_tail(&old->list, &new->head);
2802next:
2803 path->slots[0]++;
2804 cond_resched();
2805 }
2806
2807 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002808 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002809
2810 return new;
2811
Liu Bo38c227d2013-01-29 03:18:40 +00002812out_free_path:
2813 btrfs_free_path(path);
2814out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002815 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002816 return NULL;
2817}
2818
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002819static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002820 u64 start, u64 len)
2821{
2822 struct btrfs_block_group_cache *cache;
2823
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002824 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002825 ASSERT(cache);
2826
2827 spin_lock(&cache->lock);
2828 cache->delalloc_bytes -= len;
2829 spin_unlock(&cache->lock);
2830
2831 btrfs_put_block_group(cache);
2832}
2833
Chris Masond352ac62008-09-29 15:18:18 -04002834/* as ordered data IO finishes, this gets called so we can finish
2835 * an ordered extent if the range of bytes in the file it covers are
2836 * fully written.
2837 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002838static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002839{
Josef Bacik5fd02042012-05-02 14:00:54 -04002840 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002841 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002842 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002843 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002844 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002845 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002846 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002847 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002848 int ret = 0;
2849 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002850 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002851 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002852
Liu Bo83eea1f2012-07-10 05:28:39 -06002853 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002854
Josef Bacik5fd02042012-05-02 14:00:54 -04002855 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2856 ret = -EIO;
2857 goto out;
2858 }
2859
Miao Xief6124962014-09-12 18:44:04 +08002860 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2861 ordered_extent->file_offset +
2862 ordered_extent->len - 1);
2863
Josef Bacik77cef2e2013-08-29 13:57:21 -04002864 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2865 truncated = true;
2866 logical_len = ordered_extent->truncated_len;
2867 /* Truncated the entire extent, don't bother adding */
2868 if (!logical_len)
2869 goto out;
2870 }
2871
Yan, Zhengc2167752009-11-12 09:34:21 +00002872 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002873 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002874
2875 /*
2876 * For mwrite(mmap + memset to write) case, we still reserve
2877 * space for NOCOW range.
2878 * As NOCOW won't cause a new delayed ref, just free the space
2879 */
2880 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2881 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002882 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2883 if (nolock)
2884 trans = btrfs_join_transaction_nolock(root);
2885 else
2886 trans = btrfs_join_transaction(root);
2887 if (IS_ERR(trans)) {
2888 ret = PTR_ERR(trans);
2889 trans = NULL;
2890 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002891 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002892 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002893 ret = btrfs_update_inode_fallback(trans, root, inode);
2894 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002895 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002896 goto out;
2897 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002898
Josef Bacik2ac55d42010-02-03 19:33:23 +00002899 lock_extent_bits(io_tree, ordered_extent->file_offset,
2900 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002901 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002902
Liu Bo38c227d2013-01-29 03:18:40 +00002903 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2904 ordered_extent->file_offset + ordered_extent->len - 1,
2905 EXTENT_DEFRAG, 1, cached_state);
2906 if (ret) {
2907 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002908 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002909 /* the inode is shared */
2910 new = record_old_file_extents(inode, ordered_extent);
2911
2912 clear_extent_bit(io_tree, ordered_extent->file_offset,
2913 ordered_extent->file_offset + ordered_extent->len - 1,
2914 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2915 }
2916
Josef Bacik0cb59c92010-07-02 12:14:14 -04002917 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002918 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002919 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002920 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002921 if (IS_ERR(trans)) {
2922 ret = PTR_ERR(trans);
2923 trans = NULL;
2924 goto out_unlock;
2925 }
Chris Masona79b7d42014-05-22 16:18:52 -07002926
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002927 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002928
Chris Masonc8b97812008-10-29 14:49:59 -04002929 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002930 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002931 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002932 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002933 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002934 ordered_extent->file_offset,
2935 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002936 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002937 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002939 ret = insert_reserved_file_extent(trans, inode,
2940 ordered_extent->file_offset,
2941 ordered_extent->start,
2942 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002943 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002944 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002945 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002946 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002947 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002948 ordered_extent->start,
2949 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002950 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002951 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2952 ordered_extent->file_offset, ordered_extent->len,
2953 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002954 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002955 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002956 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002957 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002958
Chris Masone6dcd2d2008-07-17 12:53:50 -04002959 add_pending_csums(trans, inode, ordered_extent->file_offset,
2960 &ordered_extent->list);
2961
Josef Bacik6c760c02012-11-09 10:53:21 -05002962 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2963 ret = btrfs_update_inode_fallback(trans, root, inode);
2964 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002965 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002966 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002967 }
2968 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002969out_unlock:
2970 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2971 ordered_extent->file_offset +
2972 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002973out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002974 if (root != fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002975 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002976 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002977 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002978
Josef Bacik77cef2e2013-08-29 13:57:21 -04002979 if (ret || truncated) {
2980 u64 start, end;
2981
2982 if (truncated)
2983 start = ordered_extent->file_offset + logical_len;
2984 else
2985 start = ordered_extent->file_offset;
2986 end = ordered_extent->file_offset + ordered_extent->len - 1;
2987 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2988
2989 /* Drop the cache for the part of the extent we didn't write. */
2990 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002991
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002992 /*
2993 * If the ordered extent had an IOERR or something else went
2994 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002995 * back to the allocator. We only free the extent in the
2996 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002997 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002998 if ((ret || !logical_len) &&
2999 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003000 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003001 btrfs_free_reserved_extent(fs_info,
3002 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003003 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003004 }
3005
3006
Josef Bacik5fd02042012-05-02 14:00:54 -04003007 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003008 * This needs to be done to make sure anybody waiting knows we are done
3009 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003010 */
3011 btrfs_remove_ordered_extent(inode, ordered_extent);
3012
Liu Bo38c227d2013-01-29 03:18:40 +00003013 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003014 if (new) {
3015 if (ret) {
3016 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003017 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003018 } else {
3019 relink_file_extents(new);
3020 }
3021 }
Liu Bo38c227d2013-01-29 03:18:40 +00003022
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023 /* once for us */
3024 btrfs_put_ordered_extent(ordered_extent);
3025 /* once for the tree */
3026 btrfs_put_ordered_extent(ordered_extent);
3027
Josef Bacik5fd02042012-05-02 14:00:54 -04003028 return ret;
3029}
3030
3031static void finish_ordered_fn(struct btrfs_work *work)
3032{
3033 struct btrfs_ordered_extent *ordered_extent;
3034 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3035 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003036}
3037
Christoph Hellwigb2950862008-12-02 09:54:17 -05003038static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003039 struct extent_state *state, int uptodate)
3040{
Josef Bacik5fd02042012-05-02 14:00:54 -04003041 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003042 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003043 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003044 struct btrfs_workqueue *wq;
3045 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003046
liubo1abe9b82011-03-24 11:18:59 +00003047 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3048
Chris Mason8b62b722009-09-02 16:53:46 -04003049 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003050 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3051 end - start + 1, uptodate))
3052 return 0;
3053
Liu Bo9e0af232014-08-15 23:36:53 +08003054 if (btrfs_is_free_space_inode(inode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003055 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003056 func = btrfs_freespace_write_helper;
3057 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003058 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003059 func = btrfs_endio_write_helper;
3060 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003061
Liu Bo9e0af232014-08-15 23:36:53 +08003062 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3063 NULL);
3064 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003065
3066 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003067}
3068
Miao Xiedc380ae2014-09-12 18:43:55 +08003069static int __readpage_endio_check(struct inode *inode,
3070 struct btrfs_io_bio *io_bio,
3071 int icsum, struct page *page,
3072 int pgoff, u64 start, size_t len)
3073{
3074 char *kaddr;
3075 u32 csum_expected;
3076 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003077
3078 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3079
3080 kaddr = kmap_atomic(page);
3081 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003082 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003083 if (csum != csum_expected)
3084 goto zeroit;
3085
3086 kunmap_atomic(kaddr);
3087 return 0;
3088zeroit:
David Sterba94647322015-10-08 11:01:36 +02003089 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3090 "csum failed ino %llu off %llu csum %u expected csum %u",
David Sterbaf85b7372017-01-20 14:54:07 +01003091 btrfs_ino(BTRFS_I(inode)), start, csum, csum_expected);
Miao Xiedc380ae2014-09-12 18:43:55 +08003092 memset(kaddr + pgoff, 1, len);
3093 flush_dcache_page(page);
3094 kunmap_atomic(kaddr);
3095 if (csum_expected == 0)
3096 return 0;
3097 return -EIO;
3098}
3099
Chris Masond352ac62008-09-29 15:18:18 -04003100/*
Chris Masond352ac62008-09-29 15:18:18 -04003101 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003102 * if there's a match, we allow the bio to finish. If not, the code in
3103 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003104 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003105static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3106 u64 phy_offset, struct page *page,
3107 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003108{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003109 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003110 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003111 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003112 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003113
Chris Masond20f7042008-12-08 16:58:54 -05003114 if (PageChecked(page)) {
3115 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003116 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003117 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003118
3119 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003120 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003121
Yan Zheng17d217f2008-12-12 10:03:38 -05003122 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003123 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003124 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003125 return 0;
3126 }
3127
Miao Xiefacc8a222013-07-25 19:22:34 +08003128 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003129 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3130 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003131}
Chris Masonb888db2b2007-08-27 16:49:44 -04003132
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003133void btrfs_add_delayed_iput(struct inode *inode)
3134{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003135 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003136 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003137
3138 if (atomic_add_unless(&inode->i_count, -1, 1))
3139 return;
3140
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003141 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003142 if (binode->delayed_iput_count == 0) {
3143 ASSERT(list_empty(&binode->delayed_iput));
3144 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3145 } else {
3146 binode->delayed_iput_count++;
3147 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003148 spin_unlock(&fs_info->delayed_iput_lock);
3149}
3150
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003151void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003152{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003153
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003154 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003155 while (!list_empty(&fs_info->delayed_iputs)) {
3156 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003157
David Sterba8089fe62015-11-19 14:15:51 +01003158 inode = list_first_entry(&fs_info->delayed_iputs,
3159 struct btrfs_inode, delayed_iput);
3160 if (inode->delayed_iput_count) {
3161 inode->delayed_iput_count--;
3162 list_move_tail(&inode->delayed_iput,
3163 &fs_info->delayed_iputs);
3164 } else {
3165 list_del_init(&inode->delayed_iput);
3166 }
3167 spin_unlock(&fs_info->delayed_iput_lock);
3168 iput(&inode->vfs_inode);
3169 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003170 }
David Sterba8089fe62015-11-19 14:15:51 +01003171 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003172}
3173
Yan, Zhengd68fc572010-05-16 10:49:58 -04003174/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003175 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003176 * files in the subvolume, it removes orphan item and frees block_rsv
3177 * structure.
3178 */
3179void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3180 struct btrfs_root *root)
3181{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003182 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003183 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003184 int ret;
3185
Josef Bacik8a35d952012-05-23 14:26:42 -04003186 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003187 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3188 return;
3189
Josef Bacik90290e12011-12-02 15:44:12 -05003190 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003191 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003192 spin_unlock(&root->orphan_lock);
3193 return;
3194 }
3195
3196 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3197 spin_unlock(&root->orphan_lock);
3198 return;
3199 }
3200
3201 block_rsv = root->orphan_block_rsv;
3202 root->orphan_block_rsv = NULL;
3203 spin_unlock(&root->orphan_lock);
3204
Miao Xie27cdeb72014-04-02 19:51:05 +08003205 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003206 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003207 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003208 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003209 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003210 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003211 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003212 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3213 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003214 }
3215
Josef Bacik90290e12011-12-02 15:44:12 -05003216 if (block_rsv) {
3217 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003218 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003219 }
3220}
3221
3222/*
Josef Bacik7b128762008-07-24 12:17:14 -04003223 * This creates an orphan entry for the given inode in case something goes
3224 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003225 *
3226 * NOTE: caller of this function should reserve 5 units of metadata for
3227 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003228 */
3229int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3230{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003232 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003233 struct btrfs_block_rsv *block_rsv = NULL;
3234 int reserve = 0;
3235 int insert = 0;
3236 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003237
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003239 block_rsv = btrfs_alloc_block_rsv(fs_info,
3240 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003241 if (!block_rsv)
3242 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003243 }
3244
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245 spin_lock(&root->orphan_lock);
3246 if (!root->orphan_block_rsv) {
3247 root->orphan_block_rsv = block_rsv;
3248 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003249 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 block_rsv = NULL;
3251 }
Josef Bacik7b128762008-07-24 12:17:14 -04003252
Josef Bacik8a35d952012-05-23 14:26:42 -04003253 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3254 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255#if 0
3256 /*
3257 * For proper ENOSPC handling, we should do orphan
3258 * cleanup when mounting. But this introduces backward
3259 * compatibility issue.
3260 */
3261 if (!xchg(&root->orphan_item_inserted, 1))
3262 insert = 2;
3263 else
3264 insert = 1;
3265#endif
3266 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003267 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003268 }
Josef Bacik7b128762008-07-24 12:17:14 -04003269
Josef Bacik72ac3c02012-05-23 14:13:11 -04003270 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3271 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003272 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003273 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003274
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 /* grab metadata reservation from transaction handle */
3276 if (reserve) {
3277 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003278 ASSERT(!ret);
3279 if (ret) {
3280 atomic_dec(&root->orphan_inodes);
3281 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3282 &BTRFS_I(inode)->runtime_flags);
3283 if (insert)
3284 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3285 &BTRFS_I(inode)->runtime_flags);
3286 return ret;
3287 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 }
3289
3290 /* insert an orphan item to track this unlinked/truncated file */
3291 if (insert >= 1) {
David Sterbaf85b7372017-01-20 14:54:07 +01003292 ret = btrfs_insert_orphan_item(trans, root,
3293 btrfs_ino(BTRFS_I(inode)));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003294 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003295 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003296 if (reserve) {
3297 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3298 &BTRFS_I(inode)->runtime_flags);
3299 btrfs_orphan_release_metadata(inode);
3300 }
3301 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003302 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3303 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003304 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003305 return ret;
3306 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003307 }
3308 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003309 }
3310
3311 /* insert an orphan item to track subvolume contains orphan files */
3312 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003313 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003314 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003315 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003316 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003317 return ret;
3318 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003319 }
3320 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003321}
3322
3323/*
3324 * We have done the truncate/delete so we can go ahead and remove the orphan
3325 * item for this particular inode.
3326 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003327static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3328 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003329{
3330 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003331 int delete_item = 0;
3332 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003333 int ret = 0;
3334
Yan, Zhengd68fc572010-05-16 10:49:58 -04003335 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003336 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3337 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003338 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003339
Josef Bacik72ac3c02012-05-23 14:13:11 -04003340 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3341 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003342 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003343 spin_unlock(&root->orphan_lock);
3344
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003345 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003346 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003347 if (trans)
3348 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003349 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04003350 }
Josef Bacik7b128762008-07-24 12:17:14 -04003351
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003352 if (release_rsv)
3353 btrfs_orphan_release_metadata(inode);
3354
Josef Bacik4ef31a42013-08-13 14:10:08 -04003355 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003356}
3357
3358/*
3359 * this cleans up any orphans that may be left on the list from the last use
3360 * of this root.
3361 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003362int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003363{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003364 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003365 struct btrfs_path *path;
3366 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003367 struct btrfs_key key, found_key;
3368 struct btrfs_trans_handle *trans;
3369 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003370 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003371 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3372
Yan, Zhengd68fc572010-05-16 10:49:58 -04003373 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003374 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003375
3376 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003377 if (!path) {
3378 ret = -ENOMEM;
3379 goto out;
3380 }
David Sterbae4058b52015-11-27 16:31:35 +01003381 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003382
3383 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003384 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003385 key.offset = (u64)-1;
3386
Josef Bacik7b128762008-07-24 12:17:14 -04003387 while (1) {
3388 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003389 if (ret < 0)
3390 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003391
3392 /*
3393 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003394 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003395 * find the key and see if we have stuff that matches
3396 */
3397 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003398 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003399 if (path->slots[0] == 0)
3400 break;
3401 path->slots[0]--;
3402 }
3403
3404 /* pull out the item */
3405 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003406 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3407
3408 /* make sure the item matches what we want */
3409 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3410 break;
David Sterba962a2982014-06-04 18:41:45 +02003411 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003412 break;
3413
3414 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003415 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003416
3417 /*
3418 * this is where we are basically btrfs_lookup, without the
3419 * crossing root thing. we store the inode number in the
3420 * offset of the orphan item.
3421 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003422
3423 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003424 btrfs_err(fs_info,
3425 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003426 ret = -EINVAL;
3427 goto out;
3428 }
3429
3430 last_objectid = found_key.offset;
3431
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003432 found_key.objectid = found_key.offset;
3433 found_key.type = BTRFS_INODE_ITEM_KEY;
3434 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003435 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303436 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003437 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003438 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003439
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003440 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003441 struct btrfs_root *dead_root;
3442 struct btrfs_fs_info *fs_info = root->fs_info;
3443 int is_dead_root = 0;
3444
3445 /*
3446 * this is an orphan in the tree root. Currently these
3447 * could come from 2 sources:
3448 * a) a snapshot deletion in progress
3449 * b) a free space cache inode
3450 * We need to distinguish those two, as the snapshot
3451 * orphan must not get deleted.
3452 * find_dead_roots already ran before us, so if this
3453 * is a snapshot deletion, we should find the root
3454 * in the dead_roots list
3455 */
3456 spin_lock(&fs_info->trans_lock);
3457 list_for_each_entry(dead_root, &fs_info->dead_roots,
3458 root_list) {
3459 if (dead_root->root_key.objectid ==
3460 found_key.objectid) {
3461 is_dead_root = 1;
3462 break;
3463 }
3464 }
3465 spin_unlock(&fs_info->trans_lock);
3466 if (is_dead_root) {
3467 /* prevent this orphan from being found again */
3468 key.offset = found_key.objectid - 1;
3469 continue;
3470 }
3471 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003472 /*
3473 * Inode is already gone but the orphan item is still there,
3474 * kill the orphan item.
3475 */
Filipe Manana67710892016-06-06 11:51:25 +01003476 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003477 trans = btrfs_start_transaction(root, 1);
3478 if (IS_ERR(trans)) {
3479 ret = PTR_ERR(trans);
3480 goto out;
3481 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003482 btrfs_debug(fs_info, "auto deleting %Lu",
3483 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003484 ret = btrfs_del_orphan_item(trans, root,
3485 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003486 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003487 if (ret)
3488 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003489 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003490 }
Josef Bacik7b128762008-07-24 12:17:14 -04003491
Josef Bacik7b128762008-07-24 12:17:14 -04003492 /*
3493 * add this inode to the orphan list so btrfs_orphan_del does
3494 * the proper thing when we hit it
3495 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003496 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3497 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003498 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003499
Josef Bacik7b128762008-07-24 12:17:14 -04003500 /* if we have links, this was a truncate, lets do that */
3501 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303502 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003503 iput(inode);
3504 continue;
3505 }
Josef Bacik7b128762008-07-24 12:17:14 -04003506 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003507
3508 /* 1 for the orphan item deletion. */
3509 trans = btrfs_start_transaction(root, 1);
3510 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003511 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003512 ret = PTR_ERR(trans);
3513 goto out;
3514 }
3515 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003516 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003517 if (ret) {
3518 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003519 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003520 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003521
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003522 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003523 if (ret)
3524 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003525 } else {
3526 nr_unlink++;
3527 }
3528
3529 /* this will do delete_inode and everything for us */
3530 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003531 if (ret)
3532 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003533 }
Miao Xie3254c872011-11-10 20:45:05 -05003534 /* release the path since we're done with it */
3535 btrfs_release_path(path);
3536
Yan, Zhengd68fc572010-05-16 10:49:58 -04003537 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3538
3539 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003540 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003541 (u64)-1);
3542
Miao Xie27cdeb72014-04-02 19:51:05 +08003543 if (root->orphan_block_rsv ||
3544 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003545 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003546 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003547 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003548 }
Josef Bacik7b128762008-07-24 12:17:14 -04003549
3550 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003551 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003552 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003553 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003554
3555out:
3556 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003557 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003558 btrfs_free_path(path);
3559 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003560}
3561
Chris Masond352ac62008-09-29 15:18:18 -04003562/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003563 * very simple check to peek ahead in the leaf looking for xattrs. If we
3564 * don't find any xattrs, we know there can't be any acls.
3565 *
3566 * slot is the slot the inode is in, objectid is the objectid of the inode
3567 */
3568static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003569 int slot, u64 objectid,
3570 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003571{
3572 u32 nritems = btrfs_header_nritems(leaf);
3573 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003574 static u64 xattr_access = 0;
3575 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003576 int scanned = 0;
3577
Josef Bacikf23b5a52013-06-19 10:16:26 -04003578 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003579 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3580 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3581 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3582 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003583 }
3584
Chris Mason46a53cc2009-04-27 11:47:50 -04003585 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003586 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003587 while (slot < nritems) {
3588 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3589
3590 /* we found a different objectid, there must not be acls */
3591 if (found_key.objectid != objectid)
3592 return 0;
3593
3594 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003595 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003596 if (*first_xattr_slot == -1)
3597 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003598 if (found_key.offset == xattr_access ||
3599 found_key.offset == xattr_default)
3600 return 1;
3601 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003602
3603 /*
3604 * we found a key greater than an xattr key, there can't
3605 * be any acls later on
3606 */
3607 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3608 return 0;
3609
3610 slot++;
3611 scanned++;
3612
3613 /*
3614 * it goes inode, inode backrefs, xattrs, extents,
3615 * so if there are a ton of hard links to an inode there can
3616 * be a lot of backrefs. Don't waste time searching too hard,
3617 * this is just an optimization
3618 */
3619 if (scanned >= 8)
3620 break;
3621 }
3622 /* we hit the end of the leaf before we found an xattr or
3623 * something larger than an xattr. We have to assume the inode
3624 * has acls
3625 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003626 if (*first_xattr_slot == -1)
3627 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003628 return 1;
3629}
3630
3631/*
Chris Masond352ac62008-09-29 15:18:18 -04003632 * read an inode from the btree into the in-memory inode
3633 */
Filipe Manana67710892016-06-06 11:51:25 +01003634static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003635{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003636 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003637 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003638 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003639 struct btrfs_inode_item *inode_item;
3640 struct btrfs_root *root = BTRFS_I(inode)->root;
3641 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003642 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003643 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003644 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003645 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003646 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003647 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003648
3649 ret = btrfs_fill_inode(inode, &rdev);
3650 if (!ret)
3651 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003652
3653 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003654 if (!path) {
3655 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003656 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003657 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003658
Chris Mason39279cc2007-06-12 06:35:45 -04003659 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003660
Chris Mason39279cc2007-06-12 06:35:45 -04003661 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003662 if (ret) {
3663 if (ret > 0)
3664 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003665 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003666 }
Chris Mason39279cc2007-06-12 06:35:45 -04003667
Chris Mason5f39d392007-10-15 16:14:19 -04003668 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003669
3670 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003671 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003672
Chris Mason5f39d392007-10-15 16:14:19 -04003673 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3674 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003675 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003676 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003677 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3678 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003679 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003680
David Sterbaa937b972014-12-12 17:39:12 +01003681 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3682 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003683
David Sterbaa937b972014-12-12 17:39:12 +01003684 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3685 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003686
David Sterbaa937b972014-12-12 17:39:12 +01003687 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3688 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003689
chandan r9cc97d62012-07-04 12:48:07 +05303690 BTRFS_I(inode)->i_otime.tv_sec =
3691 btrfs_timespec_sec(leaf, &inode_item->otime);
3692 BTRFS_I(inode)->i_otime.tv_nsec =
3693 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003694
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003695 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003696 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003697 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3698
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003699 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003700 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003701 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003702 rdev = btrfs_inode_rdev(leaf, inode_item);
3703
Josef Bacikaec74772008-07-24 12:12:38 -04003704 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003705 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003706
3707cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003708 /*
3709 * If we were modified in the current generation and evicted from memory
3710 * and then re-read we need to do a full sync since we don't have any
3711 * idea about which extents were modified before we were evicted from
3712 * cache.
3713 *
3714 * This is required for both inode re-read from disk and delayed inode
3715 * in delayed_nodes_tree.
3716 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003717 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003718 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3719 &BTRFS_I(inode)->runtime_flags);
3720
Filipe Mananabde6c242015-07-24 00:00:19 +01003721 /*
3722 * We don't persist the id of the transaction where an unlink operation
3723 * against the inode was last made. So here we assume the inode might
3724 * have been evicted, and therefore the exact value of last_unlink_trans
3725 * lost, and set it to last_trans to avoid metadata inconsistencies
3726 * between the inode and its parent if the inode is fsync'ed and the log
3727 * replayed. For example, in the scenario:
3728 *
3729 * touch mydir/foo
3730 * ln mydir/foo mydir/bar
3731 * sync
3732 * unlink mydir/bar
3733 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3734 * xfs_io -c fsync mydir/foo
3735 * <power failure>
3736 * mount fs, triggers fsync log replay
3737 *
3738 * We must make sure that when we fsync our inode foo we also log its
3739 * parent inode, otherwise after log replay the parent still has the
3740 * dentry with the "bar" name but our inode foo has a link count of 1
3741 * and doesn't have an inode ref with the name "bar" anymore.
3742 *
3743 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003744 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003745 * transaction commits on fsync if our inode is a directory, or if our
3746 * inode is not a directory, logging its parent unnecessarily.
3747 */
3748 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3749
Miao Xie67de1172013-12-26 13:07:06 +08003750 path->slots[0]++;
3751 if (inode->i_nlink != 1 ||
3752 path->slots[0] >= btrfs_header_nritems(leaf))
3753 goto cache_acl;
3754
3755 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003756 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003757 goto cache_acl;
3758
3759 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3760 if (location.type == BTRFS_INODE_REF_KEY) {
3761 struct btrfs_inode_ref *ref;
3762
3763 ref = (struct btrfs_inode_ref *)ptr;
3764 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3765 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3766 struct btrfs_inode_extref *extref;
3767
3768 extref = (struct btrfs_inode_extref *)ptr;
3769 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3770 extref);
3771 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003772cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003773 /*
3774 * try to precache a NULL acl entry for files that don't have
3775 * any xattrs or acls
3776 */
Li Zefan33345d012011-04-20 10:31:50 +08003777 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003778 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003779 if (first_xattr_slot != -1) {
3780 path->slots[0] = first_xattr_slot;
3781 ret = btrfs_load_inode_props(inode, path);
3782 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003783 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003784 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003785 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003786 root->root_key.objectid, ret);
3787 }
3788 btrfs_free_path(path);
3789
Al Viro72c04902009-06-24 16:58:48 -04003790 if (!maybe_acls)
3791 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003792
Chris Mason39279cc2007-06-12 06:35:45 -04003793 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003794 case S_IFREG:
3795 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003796 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003797 inode->i_fop = &btrfs_file_operations;
3798 inode->i_op = &btrfs_file_inode_operations;
3799 break;
3800 case S_IFDIR:
3801 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003802 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003803 break;
3804 case S_IFLNK:
3805 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003806 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003807 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3808 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003809 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003810 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003811 init_special_inode(inode, inode->i_mode, rdev);
3812 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003813 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003814
3815 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003816 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003817
3818make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003819 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003820 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003821 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003822}
3823
Chris Masond352ac62008-09-29 15:18:18 -04003824/*
3825 * given a leaf and an inode, copy the inode fields into the leaf
3826 */
Chris Masone02119d2008-09-05 16:13:11 -04003827static void fill_inode_item(struct btrfs_trans_handle *trans,
3828 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003829 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003830 struct inode *inode)
3831{
Liu Bo51fab692012-12-27 09:01:21 +00003832 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003833
Liu Bo51fab692012-12-27 09:01:21 +00003834 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003835
Liu Bo51fab692012-12-27 09:01:21 +00003836 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3837 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3838 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3839 &token);
3840 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3841 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003842
David Sterbaa937b972014-12-12 17:39:12 +01003843 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003844 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003845 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003846 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003847
David Sterbaa937b972014-12-12 17:39:12 +01003848 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003849 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003850 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003851 inode->i_mtime.tv_nsec, &token);
3852
David Sterbaa937b972014-12-12 17:39:12 +01003853 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003854 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003855 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003856 inode->i_ctime.tv_nsec, &token);
3857
chandan r9cc97d62012-07-04 12:48:07 +05303858 btrfs_set_token_timespec_sec(leaf, &item->otime,
3859 BTRFS_I(inode)->i_otime.tv_sec, &token);
3860 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3861 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3862
Liu Bo51fab692012-12-27 09:01:21 +00003863 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3864 &token);
3865 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3866 &token);
3867 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3868 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3869 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3870 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3871 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003872}
3873
Chris Masond352ac62008-09-29 15:18:18 -04003874/*
3875 * copy everything in the in-memory inode into the btree.
3876 */
Chris Mason21151332011-11-10 20:39:08 -05003877static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003878 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003879{
3880 struct btrfs_inode_item *inode_item;
3881 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003882 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003883 int ret;
3884
3885 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003886 if (!path)
3887 return -ENOMEM;
3888
Chris Masonb9473432009-03-13 11:00:37 -04003889 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003890 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3891 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003892 if (ret) {
3893 if (ret > 0)
3894 ret = -ENOENT;
3895 goto failed;
3896 }
3897
Chris Mason5f39d392007-10-15 16:14:19 -04003898 leaf = path->nodes[0];
3899 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003900 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003901
Chris Masone02119d2008-09-05 16:13:11 -04003902 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003903 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003904 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003905 ret = 0;
3906failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003907 btrfs_free_path(path);
3908 return ret;
3909}
3910
Chris Masond352ac62008-09-29 15:18:18 -04003911/*
Chris Mason21151332011-11-10 20:39:08 -05003912 * copy everything in the in-memory inode into the btree.
3913 */
3914noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3915 struct btrfs_root *root, struct inode *inode)
3916{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003917 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003918 int ret;
3919
3920 /*
3921 * If the inode is a free space inode, we can deadlock during commit
3922 * if we put it into the delayed code.
3923 *
3924 * The data relocation inode should also be directly updated
3925 * without delay
3926 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003927 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003928 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003929 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003930 btrfs_update_root_times(trans, root);
3931
Chris Mason21151332011-11-10 20:39:08 -05003932 ret = btrfs_delayed_update_inode(trans, root, inode);
3933 if (!ret)
3934 btrfs_set_inode_last_trans(trans, inode);
3935 return ret;
3936 }
3937
3938 return btrfs_update_inode_item(trans, root, inode);
3939}
3940
Josef Bacikbe6aef62012-10-22 15:43:12 -04003941noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3942 struct btrfs_root *root,
3943 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003944{
3945 int ret;
3946
3947 ret = btrfs_update_inode(trans, root, inode);
3948 if (ret == -ENOSPC)
3949 return btrfs_update_inode_item(trans, root, inode);
3950 return ret;
3951}
3952
3953/*
Chris Masond352ac62008-09-29 15:18:18 -04003954 * unlink helper that gets used here in inode.c and in the tree logging
3955 * recovery code. It remove a link in a directory with a given name, and
3956 * also drops the back refs in the inode to the directory
3957 */
Al Viro92986792011-03-04 17:14:37 +00003958static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3959 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003960 struct btrfs_inode *dir,
3961 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003962 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003963{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003964 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003965 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003966 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003967 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003968 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003969 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003970 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003971 u64 ino = btrfs_ino(inode);
3972 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003973
3974 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003975 if (!path) {
3976 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003977 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003978 }
3979
Chris Masonb9473432009-03-13 11:00:37 -04003980 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003981 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003982 name, name_len, -1);
3983 if (IS_ERR(di)) {
3984 ret = PTR_ERR(di);
3985 goto err;
3986 }
3987 if (!di) {
3988 ret = -ENOENT;
3989 goto err;
3990 }
Chris Mason5f39d392007-10-15 16:14:19 -04003991 leaf = path->nodes[0];
3992 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003993 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003994 if (ret)
3995 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003996 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003997
Miao Xie67de1172013-12-26 13:07:06 +08003998 /*
3999 * If we don't have dir index, we have to get it by looking up
4000 * the inode ref, since we get the inode ref, remove it directly,
4001 * it is unnecessary to do delayed deletion.
4002 *
4003 * But if we have dir index, needn't search inode ref to get it.
4004 * Since the inode ref is close to the inode item, it is better
4005 * that we delay to delete it, and just do this deletion when
4006 * we update the inode item.
4007 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004008 if (inode->dir_index) {
4009 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08004010 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004011 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004012 goto skip_backref;
4013 }
4014 }
4015
Li Zefan33345d012011-04-20 10:31:50 +08004016 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4017 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004018 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004019 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004020 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004021 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004022 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004023 goto err;
4024 }
Miao Xie67de1172013-12-26 13:07:06 +08004025skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004026 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004027 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004028 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004029 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004030 }
Chris Mason39279cc2007-06-12 06:35:45 -04004031
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004032 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4033 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004034 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004035 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004036 goto err;
4037 }
Chris Masone02119d2008-09-05 16:13:11 -04004038
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004039 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4040 index);
Chris Mason6418c962010-10-30 07:34:24 -04004041 if (ret == -ENOENT)
4042 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004043 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004044 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004045err:
4046 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004047 if (ret)
4048 goto out;
4049
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004050 btrfs_i_size_write(&dir->vfs_inode,
4051 dir->vfs_inode.i_size - name_len * 2);
4052 inode_inc_iversion(&inode->vfs_inode);
4053 inode_inc_iversion(&dir->vfs_inode);
4054 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4055 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4056 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004057out:
Chris Mason39279cc2007-06-12 06:35:45 -04004058 return ret;
4059}
4060
Al Viro92986792011-03-04 17:14:37 +00004061int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4062 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004063 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004064 const char *name, int name_len)
4065{
4066 int ret;
4067 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4068 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004069 drop_nlink(&inode->vfs_inode);
4070 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004071 }
4072 return ret;
4073}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004074
4075/*
4076 * helper to start transaction for unlink and rmdir.
4077 *
Josef Bacikd52be812013-05-29 14:54:47 -04004078 * unlink and rmdir are special in btrfs, they do not always free space, so
4079 * if we cannot make our reservations the normal way try and see if there is
4080 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4081 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004082 */
Josef Bacikd52be812013-05-29 14:54:47 -04004083static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004084{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004085 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004086
Josef Bacike70bea52011-10-11 14:18:24 -04004087 /*
4088 * 1 for the possible orphan item
4089 * 1 for the dir item
4090 * 1 for the dir index
4091 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004092 * 1 for the inode
4093 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004094 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004095}
4096
Chris Mason39279cc2007-06-12 06:35:45 -04004097static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4098{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004099 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004100 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004101 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004102 int ret;
4103
Josef Bacikd52be812013-05-29 14:54:47 -04004104 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004105 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004106 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004107
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004108 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4109 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004110
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004111 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4112 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4113 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004114 if (ret)
4115 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004116
Yan, Zhenga22285a2010-05-16 10:48:46 -04004117 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004118 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004119 if (ret)
4120 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004121 }
Josef Bacik7b128762008-07-24 12:17:14 -04004122
Tsutomu Itohb5324022011-07-19 07:27:20 +00004123out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004124 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004125 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004126 return ret;
4127}
4128
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004129int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4130 struct btrfs_root *root,
4131 struct inode *dir, u64 objectid,
4132 const char *name, int name_len)
4133{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004134 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004135 struct btrfs_path *path;
4136 struct extent_buffer *leaf;
4137 struct btrfs_dir_item *di;
4138 struct btrfs_key key;
4139 u64 index;
4140 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004141 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004142
4143 path = btrfs_alloc_path();
4144 if (!path)
4145 return -ENOMEM;
4146
Li Zefan33345d012011-04-20 10:31:50 +08004147 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004148 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004149 if (IS_ERR_OR_NULL(di)) {
4150 if (!di)
4151 ret = -ENOENT;
4152 else
4153 ret = PTR_ERR(di);
4154 goto out;
4155 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004156
4157 leaf = path->nodes[0];
4158 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4159 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4160 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004161 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004162 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004163 goto out;
4164 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004165 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004166
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004167 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4168 root->root_key.objectid, dir_ino,
4169 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004170 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004171 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004172 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004173 goto out;
4174 }
Li Zefan33345d012011-04-20 10:31:50 +08004175 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004176 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004177 if (IS_ERR_OR_NULL(di)) {
4178 if (!di)
4179 ret = -ENOENT;
4180 else
4181 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004182 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004183 goto out;
4184 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004185
4186 leaf = path->nodes[0];
4187 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004188 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004189 index = key.offset;
4190 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004191 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004192
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004193 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004194 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004195 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004196 goto out;
4197 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004198
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004199 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004200 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004201 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004202 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004203 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004204 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004205out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004206 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004207 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004208}
4209
Chris Mason39279cc2007-06-12 06:35:45 -04004210static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4211{
David Howells2b0143b2015-03-17 22:25:59 +00004212 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004213 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004214 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004215 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004216 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004217
David Sterbab3ae2442012-09-13 16:04:34 -06004218 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004219 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004220 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004221 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004222
Josef Bacikd52be812013-05-29 14:54:47 -04004223 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004224 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004225 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004226
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004227 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004228 err = btrfs_unlink_subvol(trans, root, dir,
4229 BTRFS_I(inode)->location.objectid,
4230 dentry->d_name.name,
4231 dentry->d_name.len);
4232 goto out;
4233 }
4234
Josef Bacik7b128762008-07-24 12:17:14 -04004235 err = btrfs_orphan_add(trans, inode);
4236 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004237 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004238
Filipe Manana44f714d2016-06-06 16:11:13 +01004239 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4240
Chris Mason39279cc2007-06-12 06:35:45 -04004241 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004242 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4243 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4244 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004245 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004246 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004247 /*
4248 * Propagate the last_unlink_trans value of the deleted dir to
4249 * its parent directory. This is to prevent an unrecoverable
4250 * log tree in the case we do something like this:
4251 * 1) create dir foo
4252 * 2) create snapshot under dir foo
4253 * 3) delete the snapshot
4254 * 4) rmdir foo
4255 * 5) mkdir foo
4256 * 6) fsync foo or some file inside foo
4257 */
4258 if (last_unlink_trans >= trans->transid)
4259 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4260 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004261out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004262 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004263 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004264
Chris Mason39279cc2007-06-12 06:35:45 -04004265 return err;
4266}
4267
Chris Mason28f75a02015-02-04 06:59:29 -08004268static int truncate_space_check(struct btrfs_trans_handle *trans,
4269 struct btrfs_root *root,
4270 u64 bytes_deleted)
4271{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004272 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004273 int ret;
4274
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004275 /*
4276 * This is only used to apply pressure to the enospc system, we don't
4277 * intend to use this reservation at all.
4278 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004279 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004280 bytes_deleted *= fs_info->nodesize;
4281 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004282 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004283 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004284 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004285 trans->transid,
4286 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004287 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004288 }
Chris Mason28f75a02015-02-04 06:59:29 -08004289 return ret;
4290
4291}
4292
Filipe Manana0305cd52015-10-16 12:34:25 +01004293static int truncate_inline_extent(struct inode *inode,
4294 struct btrfs_path *path,
4295 struct btrfs_key *found_key,
4296 const u64 item_end,
4297 const u64 new_size)
4298{
4299 struct extent_buffer *leaf = path->nodes[0];
4300 int slot = path->slots[0];
4301 struct btrfs_file_extent_item *fi;
4302 u32 size = (u32)(new_size - found_key->offset);
4303 struct btrfs_root *root = BTRFS_I(inode)->root;
4304
4305 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4306
4307 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4308 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004309 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004310
4311 /*
4312 * Zero out the remaining of the last page of our inline extent,
4313 * instead of directly truncating our inline extent here - that
4314 * would be much more complex (decompressing all the data, then
4315 * compressing the truncated data, which might be bigger than
4316 * the size of the inline extent, resize the extent, etc).
4317 * We release the path because to get the page we might need to
4318 * read the extent item from disk (data not in the page cache).
4319 */
4320 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304321 return btrfs_truncate_block(inode, offset, page_end - offset,
4322 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004323 }
4324
4325 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4326 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004327 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004328
4329 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4330 inode_sub_bytes(inode, item_end + 1 - new_size);
4331
4332 return 0;
4333}
4334
Chris Mason323ac952008-10-01 19:05:46 -04004335/*
Chris Mason39279cc2007-06-12 06:35:45 -04004336 * this can truncate away extent items, csum items and directory items.
4337 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004338 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004339 *
4340 * csum items that cross the new i_size are truncated to the new size
4341 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004342 *
4343 * min_type is the minimum key type to truncate down to. If set to 0, this
4344 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004345 */
Yan, Zheng80825102009-11-12 09:35:36 +00004346int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4347 struct btrfs_root *root,
4348 struct inode *inode,
4349 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004350{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004351 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004352 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004353 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004354 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004355 struct btrfs_key key;
4356 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004357 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004358 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004359 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004360 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004361 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004362 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004363 int found_extent;
4364 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004365 int pending_del_nr = 0;
4366 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004367 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004368 int ret;
4369 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004370 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004371 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004372 bool be_nice = 0;
4373 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004374 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004375
4376 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004377
Chris Mason28ed1342014-12-17 09:41:04 -08004378 /*
4379 * for non-free space inodes and ref cows, we want to back off from
4380 * time to time
4381 */
4382 if (!btrfs_is_free_space_inode(inode) &&
4383 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4384 be_nice = 1;
4385
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004386 path = btrfs_alloc_path();
4387 if (!path)
4388 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004389 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004390
Josef Bacik5dc562c2012-08-17 13:14:17 -04004391 /*
4392 * We want to drop from the next block forward in case this new size is
4393 * not block aligned since we will be keeping the last block of the
4394 * extent just the way it is.
4395 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004396 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004397 root == fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004398 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004399 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004400 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004401
Miao Xie16cdcec2011-04-22 18:12:22 +08004402 /*
4403 * This function is also used to drop the items in the log tree before
4404 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4405 * it is used to drop the loged items. So we shouldn't kill the delayed
4406 * items.
4407 */
4408 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004409 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004410
Li Zefan33345d012011-04-20 10:31:50 +08004411 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004412 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004413 key.type = (u8)-1;
4414
Chris Mason85e21ba2008-01-29 15:11:36 -05004415search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004416 /*
4417 * with a 16K leaf size and 128MB extents, you can actually queue
4418 * up a huge file in a single leaf. Most of the time that
4419 * bytes_deleted is > 0, it will be huge by the time we get here
4420 */
Byongho Leeee221842015-12-15 01:42:10 +09004421 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004422 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004423 err = -EAGAIN;
4424 goto error;
4425 }
4426 }
4427
4428
Chris Masonb9473432009-03-13 11:00:37 -04004429 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004430 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004431 if (ret < 0) {
4432 err = ret;
4433 goto out;
4434 }
Chris Masond3977122009-01-05 21:25:51 -05004435
Chris Mason85e21ba2008-01-29 15:11:36 -05004436 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004437 /* there are no items in the tree for us to truncate, we're
4438 * done
4439 */
Yan, Zheng80825102009-11-12 09:35:36 +00004440 if (path->slots[0] == 0)
4441 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004442 path->slots[0]--;
4443 }
4444
Chris Masond3977122009-01-05 21:25:51 -05004445 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004446 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004447 leaf = path->nodes[0];
4448 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004449 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004450
Li Zefan33345d012011-04-20 10:31:50 +08004451 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004452 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004453
Chris Mason85e21ba2008-01-29 15:11:36 -05004454 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004455 break;
4456
Chris Mason5f39d392007-10-15 16:14:19 -04004457 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004458 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004459 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004460 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004461 extent_type = btrfs_file_extent_type(leaf, fi);
4462 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004463 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004464 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004465 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004466 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004467 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004468 }
Yan008630c2007-11-07 13:31:09 -05004469 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004470 }
Yan, Zheng80825102009-11-12 09:35:36 +00004471 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004472 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004473 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004474 if (item_end < new_size) {
4475 /*
4476 * With NO_HOLES mode, for the following mapping
4477 *
4478 * [0-4k][hole][8k-12k]
4479 *
4480 * if truncating isize down to 6k, it ends up
4481 * isize being 8k.
4482 */
4483 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4484 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004485 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004486 }
Yan, Zheng80825102009-11-12 09:35:36 +00004487 if (found_key.offset >= new_size)
4488 del_item = 1;
4489 else
4490 del_item = 0;
4491 }
Chris Mason39279cc2007-06-12 06:35:45 -04004492 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004493 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004494 if (found_type != BTRFS_EXTENT_DATA_KEY)
4495 goto delete;
4496
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004497 if (del_item)
4498 last_size = found_key.offset;
4499 else
4500 last_size = new_size;
4501
Chris Mason179e29e2007-11-01 11:28:41 -04004502 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004503 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004504 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004505 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004506 u64 orig_num_bytes =
4507 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004508 extent_num_bytes = ALIGN(new_size -
4509 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004510 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004511 btrfs_set_file_extent_num_bytes(leaf, fi,
4512 extent_num_bytes);
4513 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004514 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004515 if (test_bit(BTRFS_ROOT_REF_COWS,
4516 &root->state) &&
4517 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004518 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004519 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004520 } else {
Chris Masondb945352007-10-15 16:15:53 -04004521 extent_num_bytes =
4522 btrfs_file_extent_disk_num_bytes(leaf,
4523 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004524 extent_offset = found_key.offset -
4525 btrfs_file_extent_offset(leaf, fi);
4526
Chris Mason39279cc2007-06-12 06:35:45 -04004527 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004528 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004529 if (extent_start != 0) {
4530 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004531 if (test_bit(BTRFS_ROOT_REF_COWS,
4532 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004533 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004534 }
4535 }
Chris Mason90692182008-02-08 13:49:28 -05004536 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004537 /*
4538 * we can't truncate inline items that have had
4539 * special encodings
4540 */
4541 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004542 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4543 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004544
4545 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004546 * Need to release path in order to truncate a
4547 * compressed extent. So delete any accumulated
4548 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004549 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004550 if (btrfs_file_extent_compression(leaf, fi) !=
4551 BTRFS_COMPRESS_NONE && pending_del_nr) {
4552 err = btrfs_del_items(trans, root, path,
4553 pending_del_slot,
4554 pending_del_nr);
4555 if (err) {
4556 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004557 err);
4558 goto error;
4559 }
4560 pending_del_nr = 0;
4561 }
4562
4563 err = truncate_inline_extent(inode, path,
4564 &found_key,
4565 item_end,
4566 new_size);
4567 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004568 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004569 goto error;
4570 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004571 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4572 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004573 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004574 }
Chris Mason39279cc2007-06-12 06:35:45 -04004575 }
Chris Mason179e29e2007-11-01 11:28:41 -04004576delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004577 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004578 if (!pending_del_nr) {
4579 /* no pending yet, add ourselves */
4580 pending_del_slot = path->slots[0];
4581 pending_del_nr = 1;
4582 } else if (pending_del_nr &&
4583 path->slots[0] + 1 == pending_del_slot) {
4584 /* hop on the pending chunk */
4585 pending_del_nr++;
4586 pending_del_slot = path->slots[0];
4587 } else {
Chris Masond3977122009-01-05 21:25:51 -05004588 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004589 }
Chris Mason39279cc2007-06-12 06:35:45 -04004590 } else {
4591 break;
4592 }
Chris Mason28f75a02015-02-04 06:59:29 -08004593 should_throttle = 0;
4594
Miao Xie27cdeb72014-04-02 19:51:05 +08004595 if (found_extent &&
4596 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004597 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004598 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004599 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004600 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004601 extent_num_bytes, 0,
4602 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004603 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004604 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004605 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4606 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004607 trans->delayed_ref_updates * 2,
4608 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004609 if (be_nice) {
4610 if (truncate_space_check(trans, root,
4611 extent_num_bytes)) {
4612 should_end = 1;
4613 }
4614 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004615 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004616 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004617 }
Chris Mason39279cc2007-06-12 06:35:45 -04004618 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004619
Yan, Zheng80825102009-11-12 09:35:36 +00004620 if (found_type == BTRFS_INODE_ITEM_KEY)
4621 break;
4622
4623 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004624 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004625 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004626 if (pending_del_nr) {
4627 ret = btrfs_del_items(trans, root, path,
4628 pending_del_slot,
4629 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004630 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004631 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004632 goto error;
4633 }
Yan, Zheng80825102009-11-12 09:35:36 +00004634 pending_del_nr = 0;
4635 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004636 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004637 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004638 unsigned long updates = trans->delayed_ref_updates;
4639 if (updates) {
4640 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004641 ret = btrfs_run_delayed_refs(trans,
4642 fs_info,
4643 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004644 if (ret && !err)
4645 err = ret;
4646 }
4647 }
Chris Mason28f75a02015-02-04 06:59:29 -08004648 /*
4649 * if we failed to refill our space rsv, bail out
4650 * and let the transaction restart
4651 */
4652 if (should_end) {
4653 err = -EAGAIN;
4654 goto error;
4655 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004656 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004657 } else {
4658 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004659 }
Chris Mason39279cc2007-06-12 06:35:45 -04004660 }
Yan, Zheng80825102009-11-12 09:35:36 +00004661out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004662 if (pending_del_nr) {
4663 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4664 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004665 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004666 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004667 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004668error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004669 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004670 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004671
Chris Mason39279cc2007-06-12 06:35:45 -04004672 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004673
Liu Bo19fd2df2016-12-01 13:01:02 -08004674 if (err == 0) {
4675 /* only inline file may have last_size != new_size */
4676 if (new_size >= fs_info->sectorsize ||
4677 new_size > fs_info->max_inline)
4678 ASSERT(last_size == new_size);
4679 }
4680
Byongho Leeee221842015-12-15 01:42:10 +09004681 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004682 unsigned long updates = trans->delayed_ref_updates;
4683 if (updates) {
4684 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004685 ret = btrfs_run_delayed_refs(trans, fs_info,
4686 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004687 if (ret && !err)
4688 err = ret;
4689 }
4690 }
Yan, Zheng80825102009-11-12 09:35:36 +00004691 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004692}
4693
Chris Masona52d9a82007-08-27 16:49:44 -04004694/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304695 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004696 * @inode - inode that we're zeroing
4697 * @from - the offset to start zeroing
4698 * @len - the length to zero, 0 to zero the entire range respective to the
4699 * offset
4700 * @front - zero up to the offset instead of from the offset on
4701 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304702 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004703 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004704 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304705int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004706 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004707{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004708 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004709 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004710 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4711 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004712 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004713 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004714 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004715 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304716 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004717 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004718 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004719 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304720 u64 block_start;
4721 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004722
Josef Bacik2aaa6652012-08-29 14:27:18 -04004723 if ((offset & (blocksize - 1)) == 0 &&
4724 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004725 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304726
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004727 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304728 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004729 if (ret)
4730 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004731
Chris Mason211c17f2008-05-15 09:13:45 -04004732again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004733 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004734 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004735 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304736 round_down(from, blocksize),
4737 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004738 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004739 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004740 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004741
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304742 block_start = round_down(from, blocksize);
4743 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004744
Chris Masona52d9a82007-08-27 16:49:44 -04004745 if (!PageUptodate(page)) {
4746 ret = btrfs_readpage(NULL, page);
4747 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004748 if (page->mapping != mapping) {
4749 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004750 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004751 goto again;
4752 }
Chris Masona52d9a82007-08-27 16:49:44 -04004753 if (!PageUptodate(page)) {
4754 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004755 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004756 }
4757 }
Chris Mason211c17f2008-05-15 09:13:45 -04004758 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004759
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304760 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004761 set_page_extent_mapped(page);
4762
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304763 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004764 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004766 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004767 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004768 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004769 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004770 btrfs_put_ordered_extent(ordered);
4771 goto again;
4772 }
4773
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304774 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004775 EXTENT_DIRTY | EXTENT_DELALLOC |
4776 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004777 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004778
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304779 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004780 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004781 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304782 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004783 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004784 goto out_unlock;
4785 }
4786
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304787 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004788 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304789 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004790 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004791 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304792 memset(kaddr + (block_start - page_offset(page)),
4793 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004794 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304795 memset(kaddr + (block_start - page_offset(page)) + offset,
4796 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004797 flush_dcache_page(page);
4798 kunmap(page);
4799 }
Chris Mason247e7432008-07-17 12:53:51 -04004800 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004801 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304802 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004803 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004804
Chris Mason89642222008-07-24 09:41:53 -04004805out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004806 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304807 btrfs_delalloc_release_space(inode, block_start,
4808 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004809 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004810 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004811out:
4812 return ret;
4813}
4814
Josef Bacik16e75492013-10-22 12:18:51 -04004815static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4816 u64 offset, u64 len)
4817{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004818 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004819 struct btrfs_trans_handle *trans;
4820 int ret;
4821
4822 /*
4823 * Still need to make sure the inode looks like it's been updated so
4824 * that any holes get logged if we fsync.
4825 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004826 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4827 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004828 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4829 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4830 return 0;
4831 }
4832
4833 /*
4834 * 1 - for the one we're dropping
4835 * 1 - for the one we're adding
4836 * 1 - for updating the inode.
4837 */
4838 trans = btrfs_start_transaction(root, 3);
4839 if (IS_ERR(trans))
4840 return PTR_ERR(trans);
4841
4842 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4843 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004844 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004845 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004846 return ret;
4847 }
4848
David Sterbaf85b7372017-01-20 14:54:07 +01004849 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4850 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004851 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004852 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004853 else
4854 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004855 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004856 return ret;
4857}
4858
Josef Bacik695a0d02011-03-04 15:46:53 -05004859/*
4860 * This function puts in dummy file extents for the area we're creating a hole
4861 * for. So if we are truncating this file to a larger size we need to insert
4862 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4863 * the range between oldsize and size
4864 */
Josef Bacika41ad392011-01-31 15:30:16 -05004865int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004866{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004867 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004868 struct btrfs_root *root = BTRFS_I(inode)->root;
4869 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004870 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004871 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004872 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004873 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4874 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004875 u64 last_byte;
4876 u64 cur_offset;
4877 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004878 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004879
Josef Bacika71754f2013-06-17 17:14:39 -04004880 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304881 * If our size started in the middle of a block we need to zero out the
4882 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004883 * expose stale data.
4884 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304885 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004886 if (err)
4887 return err;
4888
Yan Zheng9036c102008-10-30 14:19:41 -04004889 if (size <= hole_start)
4890 return 0;
4891
Yan Zheng9036c102008-10-30 14:19:41 -04004892 while (1) {
4893 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004894
David Sterbaff13db42015-12-03 14:30:40 +01004895 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004896 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004897 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4898 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004899 if (!ordered)
4900 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004901 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4902 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004903 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004904 btrfs_put_ordered_extent(ordered);
4905 }
4906
Yan Zheng9036c102008-10-30 14:19:41 -04004907 cur_offset = hole_start;
4908 while (1) {
4909 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4910 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004911 if (IS_ERR(em)) {
4912 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004913 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004914 break;
4915 }
Yan Zheng9036c102008-10-30 14:19:41 -04004916 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004917 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004918 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004919 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004920 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004921
Josef Bacik16e75492013-10-22 12:18:51 -04004922 err = maybe_insert_hole(root, inode, cur_offset,
4923 hole_size);
4924 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004925 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004926 btrfs_drop_extent_cache(inode, cur_offset,
4927 cur_offset + hole_size - 1, 0);
4928 hole_em = alloc_extent_map();
4929 if (!hole_em) {
4930 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4931 &BTRFS_I(inode)->runtime_flags);
4932 goto next;
4933 }
4934 hole_em->start = cur_offset;
4935 hole_em->len = hole_size;
4936 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004937
Josef Bacik5dc562c2012-08-17 13:14:17 -04004938 hole_em->block_start = EXTENT_MAP_HOLE;
4939 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004940 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004941 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004942 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004943 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004944 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004945
4946 while (1) {
4947 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004948 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004949 write_unlock(&em_tree->lock);
4950 if (err != -EEXIST)
4951 break;
4952 btrfs_drop_extent_cache(inode, cur_offset,
4953 cur_offset +
4954 hole_size - 1, 0);
4955 }
4956 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004957 }
Josef Bacik16e75492013-10-22 12:18:51 -04004958next:
Yan Zheng9036c102008-10-30 14:19:41 -04004959 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004960 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004961 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004962 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004963 break;
4964 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004965 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004966 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4967 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004968 return err;
4969}
4970
Eric Sandeen3972f262013-01-12 02:57:22 +00004971static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004972{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004973 struct btrfs_root *root = BTRFS_I(inode)->root;
4974 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004975 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004976 loff_t newsize = attr->ia_size;
4977 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004978 int ret;
4979
Eric Sandeen3972f262013-01-12 02:57:22 +00004980 /*
4981 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4982 * special case where we need to update the times despite not having
4983 * these flags set. For all other operations the VFS set these flags
4984 * explicitly if it wants a timestamp update.
4985 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004986 if (newsize != oldsize) {
4987 inode_inc_iversion(inode);
4988 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4989 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004990 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004991 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004992
Josef Bacika41ad392011-01-31 15:30:16 -05004993 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004994 /*
4995 * Don't do an expanding truncate while snapshoting is ongoing.
4996 * This is to ensure the snapshot captures a fully consistent
4997 * state of this file - if the snapshot captures this expanding
4998 * truncation, it must capture all writes that happened before
4999 * this truncation.
5000 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005001 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005002 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005003 if (ret) {
5004 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005005 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005006 }
Yan, Zheng80825102009-11-12 09:35:36 +00005007
Miao Xief4a2f4c2011-12-14 20:12:01 -05005008 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005009 if (IS_ERR(trans)) {
5010 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005011 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005012 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005013
5014 i_size_write(inode, newsize);
5015 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305016 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005017 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005018 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005019 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005020 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005021
Josef Bacika41ad392011-01-31 15:30:16 -05005022 /*
5023 * We're truncating a file that used to have good data down to
5024 * zero. Make sure it gets into the ordered flush list so that
5025 * any new writes get down to disk quickly.
5026 */
5027 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005028 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5029 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005030
Josef Bacikf3fe8202013-01-07 17:03:21 -05005031 /*
5032 * 1 for the orphan item we're going to add
5033 * 1 for the orphan item deletion.
5034 */
5035 trans = btrfs_start_transaction(root, 2);
5036 if (IS_ERR(trans))
5037 return PTR_ERR(trans);
5038
5039 /*
5040 * We need to do this in case we fail at _any_ point during the
5041 * actual truncate. Once we do the truncate_setsize we could
5042 * invalidate pages which forces any outstanding ordered io to
5043 * be instantly completed which will give us extents that need
5044 * to be truncated. If we fail to get an orphan inode down we
5045 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005046 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005047 * will be consistent.
5048 */
5049 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005050 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005051 if (ret)
5052 return ret;
5053
Josef Bacika41ad392011-01-31 15:30:16 -05005054 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5055 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005056
5057 /* Disable nonlocked read DIO to avoid the end less truncate */
5058 btrfs_inode_block_unlocked_dio(inode);
5059 inode_dio_wait(inode);
5060 btrfs_inode_resume_unlocked_dio(inode);
5061
Josef Bacika41ad392011-01-31 15:30:16 -05005062 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005063 if (ret && inode->i_nlink) {
5064 int err;
5065
Liu Bo19fd2df2016-12-01 13:01:02 -08005066 /* To get a stable disk_i_size */
5067 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5068 if (err) {
5069 btrfs_orphan_del(NULL, inode);
5070 return err;
5071 }
5072
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005073 /*
5074 * failed to truncate, disk_i_size is only adjusted down
5075 * as we remove extents, so it should represent the true
5076 * size of the inode, so reset the in memory size and
5077 * delete our orphan entry.
5078 */
5079 trans = btrfs_join_transaction(root);
5080 if (IS_ERR(trans)) {
5081 btrfs_orphan_del(NULL, inode);
5082 return ret;
5083 }
5084 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5085 err = btrfs_orphan_del(trans, inode);
5086 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005087 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005088 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005089 }
Yan, Zheng80825102009-11-12 09:35:36 +00005090 }
5091
Josef Bacika41ad392011-01-31 15:30:16 -05005092 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005093}
5094
Chris Mason39279cc2007-06-12 06:35:45 -04005095static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5096{
David Howells2b0143b2015-03-17 22:25:59 +00005097 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005098 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005099 int err;
5100
Li Zefanb83cc962010-12-20 16:04:08 +08005101 if (btrfs_root_readonly(root))
5102 return -EROFS;
5103
Jan Kara31051c82016-05-26 16:55:18 +02005104 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005105 if (err)
5106 return err;
5107
Chris Mason5a3f23d2009-03-31 13:27:11 -04005108 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005109 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005110 if (err)
5111 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005112 }
Yan Zheng9036c102008-10-30 14:19:41 -04005113
Christoph Hellwig10257742010-06-04 11:30:02 +02005114 if (attr->ia_valid) {
5115 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005116 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005117 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005118
Josef Bacik22c44fe2011-11-30 10:45:38 -05005119 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005120 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005121 }
5122
Chris Mason39279cc2007-06-12 06:35:45 -04005123 return err;
5124}
Chris Mason61295eb2008-01-14 16:24:38 -05005125
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005126/*
5127 * While truncating the inode pages during eviction, we get the VFS calling
5128 * btrfs_invalidatepage() against each page of the inode. This is slow because
5129 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5130 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5131 * extent_state structures over and over, wasting lots of time.
5132 *
5133 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5134 * those expensive operations on a per page basis and do only the ordered io
5135 * finishing, while we release here the extent_map and extent_state structures,
5136 * without the excessive merging and splitting.
5137 */
5138static void evict_inode_truncate_pages(struct inode *inode)
5139{
5140 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5141 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5142 struct rb_node *node;
5143
5144 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005145 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005146
5147 write_lock(&map_tree->lock);
5148 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5149 struct extent_map *em;
5150
5151 node = rb_first(&map_tree->map);
5152 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005153 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5154 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005155 remove_extent_mapping(map_tree, em);
5156 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005157 if (need_resched()) {
5158 write_unlock(&map_tree->lock);
5159 cond_resched();
5160 write_lock(&map_tree->lock);
5161 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005162 }
5163 write_unlock(&map_tree->lock);
5164
Filipe Manana6ca07092015-05-26 00:55:42 +01005165 /*
5166 * Keep looping until we have no more ranges in the io tree.
5167 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005168 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5169 * still in progress (unlocked the pages in the bio but did not yet
5170 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005171 * ranges can still be locked and eviction started because before
5172 * submitting those bios, which are executed by a separate task (work
5173 * queue kthread), inode references (inode->i_count) were not taken
5174 * (which would be dropped in the end io callback of each bio).
5175 * Therefore here we effectively end up waiting for those bios and
5176 * anyone else holding locked ranges without having bumped the inode's
5177 * reference count - if we don't do it, when they access the inode's
5178 * io_tree to unlock a range it may be too late, leading to an
5179 * use-after-free issue.
5180 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005181 spin_lock(&io_tree->lock);
5182 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5183 struct extent_state *state;
5184 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005185 u64 start;
5186 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005187
5188 node = rb_first(&io_tree->state);
5189 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005190 start = state->start;
5191 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005192 spin_unlock(&io_tree->lock);
5193
David Sterbaff13db42015-12-03 14:30:40 +01005194 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005195
5196 /*
5197 * If still has DELALLOC flag, the extent didn't reach disk,
5198 * and its reserved space won't be freed by delayed_ref.
5199 * So we need to free its reserved space here.
5200 * (Refer to comment in btrfs_invalidatepage, case 2)
5201 *
5202 * Note, end is the bytenr of last byte, so we need + 1 here.
5203 */
5204 if (state->state & EXTENT_DELALLOC)
5205 btrfs_qgroup_free_data(inode, start, end - start + 1);
5206
Filipe Manana6ca07092015-05-26 00:55:42 +01005207 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005208 EXTENT_LOCKED | EXTENT_DIRTY |
5209 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5210 EXTENT_DEFRAG, 1, 1,
5211 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005212
Filipe Manana7064dd52014-08-08 02:47:05 +01005213 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005214 spin_lock(&io_tree->lock);
5215 }
5216 spin_unlock(&io_tree->lock);
5217}
5218
Al Virobd555972010-06-07 11:35:40 -04005219void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005220{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005221 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005222 struct btrfs_trans_handle *trans;
5223 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005224 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005225 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005226 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005227 int ret;
5228
liubo1abe9b82011-03-24 11:18:59 +00005229 trace_btrfs_inode_evict(inode);
5230
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005231 if (!root) {
5232 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5233 return;
5234 }
5235
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005236 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005237
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005238 evict_inode_truncate_pages(inode);
5239
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005240 if (inode->i_nlink &&
5241 ((btrfs_root_refs(&root->root_item) != 0 &&
5242 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5243 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005244 goto no_delete;
5245
Chris Mason39279cc2007-06-12 06:35:45 -04005246 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005247 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005248 goto no_delete;
5249 }
Al Virobd555972010-06-07 11:35:40 -04005250 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005251 if (!special_file(inode->i_mode))
5252 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005253
Miao Xief6124962014-09-12 18:44:04 +08005254 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5255
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005256 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005257 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005258 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005259 goto no_delete;
5260 }
5261
Yan, Zheng76dda932009-09-21 16:00:26 -04005262 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005263 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5264 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005265 goto no_delete;
5266 }
5267
Nikolay Borisovaa790212017-01-10 20:35:40 +02005268 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005269 if (ret) {
5270 btrfs_orphan_del(NULL, inode);
5271 goto no_delete;
5272 }
5273
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005274 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005275 if (!rsv) {
5276 btrfs_orphan_del(NULL, inode);
5277 goto no_delete;
5278 }
Josef Bacik4a338542011-08-29 11:01:31 -04005279 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005280 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005281 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005282
Chris Masondbe674a2008-07-17 12:54:05 -04005283 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005284
Josef Bacik4289a662011-08-05 13:22:24 -04005285 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005286 * This is a bit simpler than btrfs_truncate since we've already
5287 * reserved our space for our orphan item in the unlink, so we just
5288 * need to reserve some slack space in case we add bytes and update
5289 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005290 */
Yan, Zheng80825102009-11-12 09:35:36 +00005291 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005292 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5293 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005294
Josef Bacik726c35f2011-09-26 15:46:06 -04005295 /*
5296 * Try and steal from the global reserve since we will
5297 * likely not use this space anyway, we want to try as
5298 * hard as possible to get this to work.
5299 */
5300 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005301 steal_from_global++;
5302 else
5303 steal_from_global = 0;
5304 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005305
Josef Bacik3bce8762015-02-24 12:35:51 -08005306 /*
5307 * steal_from_global == 0: we reserved stuff, hooray!
5308 * steal_from_global == 1: we didn't reserve stuff, boo!
5309 * steal_from_global == 2: we've committed, still not a lot of
5310 * room but maybe we'll have room in the global reserve this
5311 * time.
5312 * steal_from_global == 3: abandon all hope!
5313 */
5314 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005315 btrfs_warn(fs_info,
5316 "Could not get space for a delete, will truncate on mount %d",
5317 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005318 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005319 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005320 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005321 }
5322
Miao Xie0e8c36a2012-12-19 06:59:51 +00005323 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005324 if (IS_ERR(trans)) {
5325 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005326 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005327 goto no_delete;
5328 }
5329
Josef Bacik3bce8762015-02-24 12:35:51 -08005330 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005331 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005332 * sure there is room to do it, if not we need to commit and try
5333 * again.
5334 */
5335 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005336 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005337 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005338 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005339 else
5340 ret = -ENOSPC;
5341 }
5342
5343 /*
5344 * Couldn't steal from the global reserve, we have too much
5345 * pending stuff built up, commit the transaction and try it
5346 * again.
5347 */
5348 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005349 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005350 if (ret) {
5351 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005352 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005353 goto no_delete;
5354 }
5355 continue;
5356 } else {
5357 steal_from_global = 0;
5358 }
5359
Josef Bacik4289a662011-08-05 13:22:24 -04005360 trans->block_rsv = rsv;
5361
Yan, Zhengd68fc572010-05-16 10:49:58 -04005362 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005363 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005364 break;
5365
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005366 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005367 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005368 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005369 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005370 }
5371
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005372 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005373
Josef Bacik4ef31a42013-08-13 14:10:08 -04005374 /*
5375 * Errors here aren't a big deal, it just means we leave orphan items
5376 * in the tree. They will be cleaned up on the next mount.
5377 */
Yan, Zheng80825102009-11-12 09:35:36 +00005378 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005379 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005380 btrfs_orphan_del(trans, inode);
5381 } else {
5382 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005383 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005384
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005385 trans->block_rsv = &fs_info->trans_block_rsv;
5386 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005387 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005388 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005389
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005390 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005391 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005392no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005393 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005394 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005395}
5396
5397/*
5398 * this returns the key found in the dir entry in the location pointer.
5399 * If no dir entries were found, location->objectid is 0.
5400 */
5401static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5402 struct btrfs_key *location)
5403{
5404 const char *name = dentry->d_name.name;
5405 int namelen = dentry->d_name.len;
5406 struct btrfs_dir_item *di;
5407 struct btrfs_path *path;
5408 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005409 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005410
5411 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005412 if (!path)
5413 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005414
David Sterbaf85b7372017-01-20 14:54:07 +01005415 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5416 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005417 if (IS_ERR(di))
5418 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005419
David Sterbac7040052011-04-19 18:00:01 +02005420 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005421 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005422
Chris Mason5f39d392007-10-15 16:14:19 -04005423 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005424out:
Chris Mason39279cc2007-06-12 06:35:45 -04005425 btrfs_free_path(path);
5426 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005427out_err:
5428 location->objectid = 0;
5429 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005430}
5431
5432/*
5433 * when we hit a tree root in a directory, the btrfs part of the inode
5434 * needs to be changed to reflect the root directory of the tree root. This
5435 * is kind of like crossing a mount point.
5436 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005437static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005438 struct inode *dir,
5439 struct dentry *dentry,
5440 struct btrfs_key *location,
5441 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005442{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005443 struct btrfs_path *path;
5444 struct btrfs_root *new_root;
5445 struct btrfs_root_ref *ref;
5446 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005447 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005448 int ret;
5449 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005450
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005451 path = btrfs_alloc_path();
5452 if (!path) {
5453 err = -ENOMEM;
5454 goto out;
5455 }
Chris Mason39279cc2007-06-12 06:35:45 -04005456
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005457 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005458 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5459 key.type = BTRFS_ROOT_REF_KEY;
5460 key.offset = location->objectid;
5461
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005462 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005463 if (ret) {
5464 if (ret < 0)
5465 err = ret;
5466 goto out;
5467 }
Chris Mason39279cc2007-06-12 06:35:45 -04005468
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005469 leaf = path->nodes[0];
5470 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005471 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005472 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5473 goto out;
5474
5475 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5476 (unsigned long)(ref + 1),
5477 dentry->d_name.len);
5478 if (ret)
5479 goto out;
5480
David Sterbab3b4aa72011-04-21 01:20:15 +02005481 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005482
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005483 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005484 if (IS_ERR(new_root)) {
5485 err = PTR_ERR(new_root);
5486 goto out;
5487 }
5488
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489 *sub_root = new_root;
5490 location->objectid = btrfs_root_dirid(&new_root->root_item);
5491 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005492 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005493 err = 0;
5494out:
5495 btrfs_free_path(path);
5496 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005497}
5498
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005499static void inode_tree_add(struct inode *inode)
5500{
5501 struct btrfs_root *root = BTRFS_I(inode)->root;
5502 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005503 struct rb_node **p;
5504 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005505 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005506 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005507
Al Viro1d3382cb2010-10-23 15:19:20 -04005508 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005509 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005510 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005511 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005512 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005513 while (*p) {
5514 parent = *p;
5515 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5516
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005517 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005518 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005519 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005520 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005521 else {
5522 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005523 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005524 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005525 RB_CLEAR_NODE(parent);
5526 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005527 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005528 }
5529 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005530 rb_link_node(new, parent, p);
5531 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005532 spin_unlock(&root->inode_lock);
5533}
5534
5535static void inode_tree_del(struct inode *inode)
5536{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005537 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005538 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005539 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005540
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005541 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005542 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005543 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005544 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005545 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005546 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005547 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005548
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005549 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005550 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005551 spin_lock(&root->inode_lock);
5552 empty = RB_EMPTY_ROOT(&root->inode_tree);
5553 spin_unlock(&root->inode_lock);
5554 if (empty)
5555 btrfs_add_dead_root(root);
5556 }
5557}
5558
Jeff Mahoney143bede2012-03-01 14:56:26 +01005559void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005560{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005561 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005562 struct rb_node *node;
5563 struct rb_node *prev;
5564 struct btrfs_inode *entry;
5565 struct inode *inode;
5566 u64 objectid = 0;
5567
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005568 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005569 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005570
5571 spin_lock(&root->inode_lock);
5572again:
5573 node = root->inode_tree.rb_node;
5574 prev = NULL;
5575 while (node) {
5576 prev = node;
5577 entry = rb_entry(node, struct btrfs_inode, rb_node);
5578
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005579 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005580 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005581 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005582 node = node->rb_right;
5583 else
5584 break;
5585 }
5586 if (!node) {
5587 while (prev) {
5588 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005589 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005590 node = prev;
5591 break;
5592 }
5593 prev = rb_next(prev);
5594 }
5595 }
5596 while (node) {
5597 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005598 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005599 inode = igrab(&entry->vfs_inode);
5600 if (inode) {
5601 spin_unlock(&root->inode_lock);
5602 if (atomic_read(&inode->i_count) > 1)
5603 d_prune_aliases(inode);
5604 /*
Al Viro45321ac2010-06-07 13:43:19 -04005605 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005606 * the inode cache when its usage count
5607 * hits zero.
5608 */
5609 iput(inode);
5610 cond_resched();
5611 spin_lock(&root->inode_lock);
5612 goto again;
5613 }
5614
5615 if (cond_resched_lock(&root->inode_lock))
5616 goto again;
5617
5618 node = rb_next(node);
5619 }
5620 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005621}
5622
Chris Masone02119d2008-09-05 16:13:11 -04005623static int btrfs_init_locked_inode(struct inode *inode, void *p)
5624{
5625 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005626 inode->i_ino = args->location->objectid;
5627 memcpy(&BTRFS_I(inode)->location, args->location,
5628 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005629 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005630 return 0;
5631}
5632
5633static int btrfs_find_actor(struct inode *inode, void *opaque)
5634{
5635 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005636 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005637 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005638}
5639
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005640static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005641 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005642 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005643{
5644 struct inode *inode;
5645 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005646 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005647
Chris Mason90d3e592014-01-09 17:28:00 -08005648 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005649 args.root = root;
5650
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005651 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005652 btrfs_init_locked_inode,
5653 (void *)&args);
5654 return inode;
5655}
5656
Balaji Rao1a54ef82008-07-21 02:01:04 +05305657/* Get an inode object given its location and corresponding root.
5658 * Returns in *is_new if the inode was read from disk
5659 */
5660struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005661 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305662{
5663 struct inode *inode;
5664
Chris Mason90d3e592014-01-09 17:28:00 -08005665 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305666 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005667 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305668
5669 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005670 int ret;
5671
5672 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005673 if (!is_bad_inode(inode)) {
5674 inode_tree_add(inode);
5675 unlock_new_inode(inode);
5676 if (new)
5677 *new = 1;
5678 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005679 unlock_new_inode(inode);
5680 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005681 ASSERT(ret < 0);
5682 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005683 }
5684 }
5685
Balaji Rao1a54ef82008-07-21 02:01:04 +05305686 return inode;
5687}
5688
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005689static struct inode *new_simple_dir(struct super_block *s,
5690 struct btrfs_key *key,
5691 struct btrfs_root *root)
5692{
5693 struct inode *inode = new_inode(s);
5694
5695 if (!inode)
5696 return ERR_PTR(-ENOMEM);
5697
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005698 BTRFS_I(inode)->root = root;
5699 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005700 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005701
5702 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005703 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005704 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005705 inode->i_fop = &simple_dir_operations;
5706 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005707 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305708 inode->i_atime = inode->i_mtime;
5709 inode->i_ctime = inode->i_mtime;
5710 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005711
5712 return inode;
5713}
5714
Chris Mason3de45862008-11-17 21:02:50 -05005715struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005716{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005717 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005718 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005719 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005720 struct btrfs_root *sub_root = root;
5721 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005722 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005723 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005724
5725 if (dentry->d_name.len > BTRFS_NAME_LEN)
5726 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005727
Jeff Layton39e3c952012-11-28 11:30:53 -05005728 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005729 if (ret < 0)
5730 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005731
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005732 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005733 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005734
5735 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005736 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005737 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005738 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005739
5740 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5741
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005742 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005743 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005744 &location, &sub_root);
5745 if (ret < 0) {
5746 if (ret != -ENOENT)
5747 inode = ERR_PTR(ret);
5748 else
5749 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5750 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005751 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005752 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005753 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005754
Julia Lawall34d19ba2011-01-24 19:55:19 +00005755 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005756 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005757 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005758 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005759 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005760 if (ret) {
5761 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005762 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005763 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005764 }
5765
Chris Mason3de45862008-11-17 21:02:50 -05005766 return inode;
5767}
5768
Nick Pigginfe15ce42011-01-07 17:49:23 +11005769static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005770{
5771 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005772 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005773
Li Zefan848cce02012-02-21 17:04:28 +08005774 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005775 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005776
Li Zefan848cce02012-02-21 17:04:28 +08005777 if (inode) {
5778 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005779 if (btrfs_root_refs(&root->root_item) == 0)
5780 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005781
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005782 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005783 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005784 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005785 return 0;
5786}
5787
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005788static void btrfs_dentry_release(struct dentry *dentry)
5789{
Daeseok Youn944a4512014-04-14 15:37:02 +09005790 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005791}
5792
Chris Mason3de45862008-11-17 21:02:50 -05005793static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005794 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005795{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005796 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005797
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005798 inode = btrfs_lookup_dentry(dir, dentry);
5799 if (IS_ERR(inode)) {
5800 if (PTR_ERR(inode) == -ENOENT)
5801 inode = NULL;
5802 else
5803 return ERR_CAST(inode);
5804 }
5805
Al Viro41d28bc2014-10-12 22:24:21 -04005806 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005807}
5808
Miao Xie16cdcec2011-04-22 18:12:22 +08005809unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005810 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5811};
5812
Al Viro9cdda8d2013-05-22 16:48:09 -04005813static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005814{
Al Viro9cdda8d2013-05-22 16:48:09 -04005815 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005816 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005817 struct btrfs_root *root = BTRFS_I(inode)->root;
5818 struct btrfs_item *item;
5819 struct btrfs_dir_item *di;
5820 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005821 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005822 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005823 struct list_head ins_list;
5824 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005825 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005826 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005827 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005828 unsigned char d_type;
5829 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005830 char tmp_name[32];
5831 char *name_ptr;
5832 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005833 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005834 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005835
Al Viro9cdda8d2013-05-22 16:48:09 -04005836 if (!dir_emit_dots(file, ctx))
5837 return 0;
5838
David Woodhouse49593bf2008-08-17 17:08:36 +01005839 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005840 if (!path)
5841 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005842
David Sterbae4058b52015-11-27 16:31:35 +01005843 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005844
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005845 INIT_LIST_HEAD(&ins_list);
5846 INIT_LIST_HEAD(&del_list);
5847 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005848
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005849 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005850 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005851 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005852
Chris Mason39279cc2007-06-12 06:35:45 -04005853 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5854 if (ret < 0)
5855 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005856
5857 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005858 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005859 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005860 if (slot >= btrfs_header_nritems(leaf)) {
5861 ret = btrfs_next_leaf(root, path);
5862 if (ret < 0)
5863 goto err;
5864 else if (ret > 0)
5865 break;
5866 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005867 }
Chris Mason3de45862008-11-17 21:02:50 -05005868
Ross Kirkdd3cc162013-09-16 15:58:09 +01005869 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005870 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5871
5872 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005873 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005874 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005875 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005876 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005877 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005878 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005879 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005880
Al Viro9cdda8d2013-05-22 16:48:09 -04005881 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005882
Chris Mason39279cc2007-06-12 06:35:45 -04005883 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005884 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005885 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005886
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005887 name_len = btrfs_dir_name_len(leaf, di);
5888 if (name_len <= sizeof(tmp_name)) {
5889 name_ptr = tmp_name;
5890 } else {
5891 name_ptr = kmalloc(name_len, GFP_KERNEL);
5892 if (!name_ptr) {
5893 ret = -ENOMEM;
5894 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005895 }
Chris Mason39279cc2007-06-12 06:35:45 -04005896 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005897 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5898 name_len);
5899
5900 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5901 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5902
5903 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5904 d_type);
5905
5906 if (name_ptr != tmp_name)
5907 kfree(name_ptr);
5908
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005909 if (over)
5910 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005911 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005912next:
5913 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005914 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005915
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005916 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005917 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005918 goto nopos;
5919
Zach Browndb62efb2013-07-11 16:19:42 -07005920 /*
5921 * Stop new entries from being returned after we return the last
5922 * entry.
5923 *
5924 * New directory entries are assigned a strictly increasing
5925 * offset. This means that new entries created during readdir
5926 * are *guaranteed* to be seen in the future by that readdir.
5927 * This has broken buggy programs which operate on names as
5928 * they're returned by readdir. Until we re-use freed offsets
5929 * we have this hack to stop new entries from being returned
5930 * under the assumption that they'll never reach this huge
5931 * offset.
5932 *
5933 * This is being careful not to overflow 32bit loff_t unless the
5934 * last entry requires it because doing so has broken 32bit apps
5935 * in the past.
5936 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005937 if (ctx->pos >= INT_MAX)
5938 ctx->pos = LLONG_MAX;
5939 else
5940 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005941nopos:
5942 ret = 0;
5943err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005944 if (put)
5945 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005946 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005947 return ret;
5948}
5949
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005950int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005951{
5952 struct btrfs_root *root = BTRFS_I(inode)->root;
5953 struct btrfs_trans_handle *trans;
5954 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005955 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005956
Josef Bacik72ac3c02012-05-23 14:13:11 -04005957 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005958 return 0;
5959
Liu Bo83eea1f2012-07-10 05:28:39 -06005960 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005961 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005962
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005963 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005964 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005965 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005966 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005967 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005968 if (IS_ERR(trans))
5969 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005970 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005971 }
5972 return ret;
5973}
5974
5975/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005976 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005977 * inode changes. But, it is most likely to find the inode in cache.
5978 * FIXME, needs more benchmarking...there are no reasons other than performance
5979 * to keep or drop this code.
5980 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005981static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005982{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005983 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005984 struct btrfs_root *root = BTRFS_I(inode)->root;
5985 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005986 int ret;
5987
Josef Bacik72ac3c02012-05-23 14:13:11 -04005988 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005989 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005990
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005991 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005992 if (IS_ERR(trans))
5993 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005994
5995 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005996 if (ret && ret == -ENOSPC) {
5997 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005998 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005999 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006000 if (IS_ERR(trans))
6001 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006002
Chris Mason94b60442010-05-26 11:02:00 -04006003 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006004 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006005 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006006 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006007 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006008
6009 return ret;
6010}
6011
6012/*
6013 * This is a copy of file_update_time. We need this so we can return error on
6014 * ENOSPC for updating the inode in the case of file write and mmap writes.
6015 */
Josef Bacike41f9412012-03-26 09:46:47 -04006016static int btrfs_update_time(struct inode *inode, struct timespec *now,
6017 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006018{
Alexander Block2bc5565282012-06-15 09:49:33 +02006019 struct btrfs_root *root = BTRFS_I(inode)->root;
6020
6021 if (btrfs_root_readonly(root))
6022 return -EROFS;
6023
Josef Bacike41f9412012-03-26 09:46:47 -04006024 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006025 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006026 if (flags & S_CTIME)
6027 inode->i_ctime = *now;
6028 if (flags & S_MTIME)
6029 inode->i_mtime = *now;
6030 if (flags & S_ATIME)
6031 inode->i_atime = *now;
6032 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006033}
6034
Chris Masond352ac62008-09-29 15:18:18 -04006035/*
6036 * find the highest existing sequence number in a directory
6037 * and then set the in-memory index_cnt variable to reflect
6038 * free sequence numbers
6039 */
Josef Bacikaec74772008-07-24 12:12:38 -04006040static int btrfs_set_inode_index_count(struct inode *inode)
6041{
6042 struct btrfs_root *root = BTRFS_I(inode)->root;
6043 struct btrfs_key key, found_key;
6044 struct btrfs_path *path;
6045 struct extent_buffer *leaf;
6046 int ret;
6047
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006048 key.objectid = btrfs_ino(BTRFS_I(inode));
David Sterba962a2982014-06-04 18:41:45 +02006049 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006050 key.offset = (u64)-1;
6051
6052 path = btrfs_alloc_path();
6053 if (!path)
6054 return -ENOMEM;
6055
6056 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6057 if (ret < 0)
6058 goto out;
6059 /* FIXME: we should be able to handle this */
6060 if (ret == 0)
6061 goto out;
6062 ret = 0;
6063
6064 /*
6065 * MAGIC NUMBER EXPLANATION:
6066 * since we search a directory based on f_pos we have to start at 2
6067 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6068 * else has to start at 2
6069 */
6070 if (path->slots[0] == 0) {
6071 BTRFS_I(inode)->index_cnt = 2;
6072 goto out;
6073 }
6074
6075 path->slots[0]--;
6076
6077 leaf = path->nodes[0];
6078 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6079
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006080 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
David Sterba962a2982014-06-04 18:41:45 +02006081 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006082 BTRFS_I(inode)->index_cnt = 2;
6083 goto out;
6084 }
6085
6086 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6087out:
6088 btrfs_free_path(path);
6089 return ret;
6090}
6091
Chris Masond352ac62008-09-29 15:18:18 -04006092/*
6093 * helper to find a free sequence number in a given directory. This current
6094 * code is very simple, later versions will do smarter things in the btree
6095 */
Chris Mason3de45862008-11-17 21:02:50 -05006096int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006097{
6098 int ret = 0;
6099
6100 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Nikolay Borisovf5cc7b82017-01-10 20:35:42 +02006101 ret = btrfs_inode_delayed_dir_index_count(BTRFS_I(dir));
Miao Xie16cdcec2011-04-22 18:12:22 +08006102 if (ret) {
6103 ret = btrfs_set_inode_index_count(dir);
6104 if (ret)
6105 return ret;
6106 }
Josef Bacikaec74772008-07-24 12:12:38 -04006107 }
6108
Chris Mason00e4e6b2008-08-05 11:18:09 -04006109 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006110 BTRFS_I(dir)->index_cnt++;
6111
6112 return ret;
6113}
6114
Chris Masonb0d5d102014-09-08 13:08:51 -07006115static int btrfs_insert_inode_locked(struct inode *inode)
6116{
6117 struct btrfs_iget_args args;
6118 args.location = &BTRFS_I(inode)->location;
6119 args.root = BTRFS_I(inode)->root;
6120
6121 return insert_inode_locked4(inode,
6122 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6123 btrfs_find_actor, &args);
6124}
6125
Chris Mason39279cc2007-06-12 06:35:45 -04006126static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6127 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006128 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006129 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006130 u64 ref_objectid, u64 objectid,
6131 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006132{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006133 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006134 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006135 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006136 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006137 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006138 struct btrfs_inode_ref *ref;
6139 struct btrfs_key key[2];
6140 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006141 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006142 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006143 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006144
Chris Mason5f39d392007-10-15 16:14:19 -04006145 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006146 if (!path)
6147 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006148
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006149 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006150 if (!inode) {
6151 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006152 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006153 }
Chris Mason39279cc2007-06-12 06:35:45 -04006154
Li Zefan581bb052011-04-20 10:06:11 +08006155 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006156 * O_TMPFILE, set link count to 0, so that after this point,
6157 * we fill in an inode item with the correct link count.
6158 */
6159 if (!name)
6160 set_nlink(inode, 0);
6161
6162 /*
Li Zefan581bb052011-04-20 10:06:11 +08006163 * we have to initialize this early, so we can reclaim the inode
6164 * number if we fail afterwards in this function.
6165 */
6166 inode->i_ino = objectid;
6167
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006168 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006169 trace_btrfs_inode_request(dir);
6170
Chris Mason3de45862008-11-17 21:02:50 -05006171 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006172 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006173 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006174 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006175 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006176 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006177 } else if (dir) {
6178 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006179 }
6180 /*
6181 * index_cnt is ignored for everything but a dir,
6182 * btrfs_get_inode_index_count has an explanation for the magic
6183 * number
6184 */
6185 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006186 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006187 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006188 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006189 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006190
Josef Bacik5dc562c2012-08-17 13:14:17 -04006191 /*
6192 * We could have gotten an inode number from somebody who was fsynced
6193 * and then removed in this same transaction, so let's just set full
6194 * sync since it will be a full sync anyway and this will blow away the
6195 * old info in the log.
6196 */
6197 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6198
Chris Mason9c583092008-01-29 15:15:18 -05006199 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006200 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006201 key[0].offset = 0;
6202
Chris Mason9c583092008-01-29 15:15:18 -05006203 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006204
6205 if (name) {
6206 /*
6207 * Start new inodes with an inode_ref. This is slightly more
6208 * efficient for small numbers of hard links since they will
6209 * be packed into one item. Extended refs will kick in if we
6210 * add more hard links than can fit in the ref item.
6211 */
6212 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006213 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006214 key[1].offset = ref_objectid;
6215
6216 sizes[1] = name_len + sizeof(*ref);
6217 }
Chris Mason9c583092008-01-29 15:15:18 -05006218
Chris Masonb0d5d102014-09-08 13:08:51 -07006219 location = &BTRFS_I(inode)->location;
6220 location->objectid = objectid;
6221 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006222 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006223
6224 ret = btrfs_insert_inode_locked(inode);
6225 if (ret < 0)
6226 goto fail;
6227
Chris Masonb9473432009-03-13 11:00:37 -04006228 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006229 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006230 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006231 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006232
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006233 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006234 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306235
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006236 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306237 inode->i_atime = inode->i_mtime;
6238 inode->i_ctime = inode->i_mtime;
6239 BTRFS_I(inode)->i_otime = inode->i_mtime;
6240
Chris Mason5f39d392007-10-15 16:14:19 -04006241 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6242 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006243 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006244 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006245 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006246
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006247 if (name) {
6248 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6249 struct btrfs_inode_ref);
6250 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6251 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6252 ptr = (unsigned long)(ref + 1);
6253 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6254 }
Chris Mason9c583092008-01-29 15:15:18 -05006255
Chris Mason5f39d392007-10-15 16:14:19 -04006256 btrfs_mark_buffer_dirty(path->nodes[0]);
6257 btrfs_free_path(path);
6258
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006259 btrfs_inherit_iflags(inode, dir);
6260
Al Viro569254b2011-07-24 17:08:40 -04006261 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006262 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006263 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006264 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006265 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6266 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006267 }
6268
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006269 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006270
6271 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006272 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006273
Alexander Block8ea05e32012-07-25 17:35:53 +02006274 btrfs_update_root_times(trans, root);
6275
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006276 ret = btrfs_inode_inherit_props(trans, inode, dir);
6277 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006278 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006279 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006280 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006281
Chris Mason39279cc2007-06-12 06:35:45 -04006282 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006283
6284fail_unlock:
6285 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006286fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006287 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006288 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006289 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006290 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006291 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006292}
6293
6294static inline u8 btrfs_inode_type(struct inode *inode)
6295{
6296 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6297}
6298
Chris Masond352ac62008-09-29 15:18:18 -04006299/*
6300 * utility function to add 'inode' into 'parent_inode' with
6301 * a give name and a given sequence number.
6302 * if 'add_backref' is true, also insert a backref from the
6303 * inode to the parent directory.
6304 */
Chris Masone02119d2008-09-05 16:13:11 -04006305int btrfs_add_link(struct btrfs_trans_handle *trans,
6306 struct inode *parent_inode, struct inode *inode,
6307 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006308{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006309 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006310 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006311 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006312 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006313 u64 ino = btrfs_ino(BTRFS_I(inode));
6314 u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006315
Li Zefan33345d012011-04-20 10:31:50 +08006316 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006317 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6318 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006319 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006320 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006321 key.offset = 0;
6322 }
Chris Mason39279cc2007-06-12 06:35:45 -04006323
Li Zefan33345d012011-04-20 10:31:50 +08006324 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006325 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6326 root->root_key.objectid, parent_ino,
6327 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006328 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006329 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6330 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006331 }
6332
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006333 /* Nothing to clean up yet */
6334 if (ret)
6335 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006336
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006337 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6338 parent_inode, &key,
6339 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006340 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006341 goto fail_dir_item;
6342 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006343 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006344 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006345 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006346
6347 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6348 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006349 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006350 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006351 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006352 ret = btrfs_update_inode(trans, root, parent_inode);
6353 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006354 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006355 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006356
6357fail_dir_item:
6358 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6359 u64 local_index;
6360 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006361 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6362 root->root_key.objectid, parent_ino,
6363 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006364
6365 } else if (add_backref) {
6366 u64 local_index;
6367 int err;
6368
6369 err = btrfs_del_inode_ref(trans, root, name, name_len,
6370 ino, parent_ino, &local_index);
6371 }
6372 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006373}
6374
6375static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006376 struct inode *dir, struct dentry *dentry,
6377 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006378{
Josef Bacika1b075d2010-11-19 20:36:11 +00006379 int err = btrfs_add_link(trans, dir, inode,
6380 dentry->d_name.name, dentry->d_name.len,
6381 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006382 if (err > 0)
6383 err = -EEXIST;
6384 return err;
6385}
6386
Josef Bacik618e21d2007-07-11 10:18:17 -04006387static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006388 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006389{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006390 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006391 struct btrfs_trans_handle *trans;
6392 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006393 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006394 int err;
6395 int drop_inode = 0;
6396 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006397 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006398
Josef Bacik9ed74f22009-09-11 16:12:44 -04006399 /*
6400 * 2 for inode item and ref
6401 * 2 for dir items
6402 * 1 for xattr if selinux is on
6403 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006404 trans = btrfs_start_transaction(root, 5);
6405 if (IS_ERR(trans))
6406 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006407
Li Zefan581bb052011-04-20 10:06:11 +08006408 err = btrfs_find_free_ino(root, &objectid);
6409 if (err)
6410 goto out_unlock;
6411
Josef Bacikaec74772008-07-24 12:12:38 -04006412 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006413 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6414 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006415 if (IS_ERR(inode)) {
6416 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006417 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006418 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006419
Casey Schauflerad19db72011-12-15 10:09:07 -05006420 /*
6421 * If the active LSM wants to access the inode during
6422 * d_instantiate it needs these. Smack checks to see
6423 * if the filesystem supports xattrs by looking at the
6424 * ops vector.
6425 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006426 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006427 init_special_inode(inode, inode->i_mode, rdev);
6428
6429 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006430 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006431 goto out_unlock_inode;
6432
6433 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6434 if (err) {
6435 goto out_unlock_inode;
6436 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006437 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006438 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006439 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006440 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006441
Josef Bacik618e21d2007-07-11 10:18:17 -04006442out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006443 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006444 btrfs_balance_delayed_items(fs_info);
6445 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006446 if (drop_inode) {
6447 inode_dec_link_count(inode);
6448 iput(inode);
6449 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006450 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006451
6452out_unlock_inode:
6453 drop_inode = 1;
6454 unlock_new_inode(inode);
6455 goto out_unlock;
6456
Josef Bacik618e21d2007-07-11 10:18:17 -04006457}
6458
Chris Mason39279cc2007-06-12 06:35:45 -04006459static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006460 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006461{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006462 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006463 struct btrfs_trans_handle *trans;
6464 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006465 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006466 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006467 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006468 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006469 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006470
Josef Bacik9ed74f22009-09-11 16:12:44 -04006471 /*
6472 * 2 for inode item and ref
6473 * 2 for dir items
6474 * 1 for xattr if selinux is on
6475 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006476 trans = btrfs_start_transaction(root, 5);
6477 if (IS_ERR(trans))
6478 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006479
Li Zefan581bb052011-04-20 10:06:11 +08006480 err = btrfs_find_free_ino(root, &objectid);
6481 if (err)
6482 goto out_unlock;
6483
Josef Bacikaec74772008-07-24 12:12:38 -04006484 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006485 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6486 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006487 if (IS_ERR(inode)) {
6488 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006489 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006490 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006491 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006492 /*
6493 * If the active LSM wants to access the inode during
6494 * d_instantiate it needs these. Smack checks to see
6495 * if the filesystem supports xattrs by looking at the
6496 * ops vector.
6497 */
6498 inode->i_fop = &btrfs_file_operations;
6499 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006500 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006501
6502 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6503 if (err)
6504 goto out_unlock_inode;
6505
6506 err = btrfs_update_inode(trans, root, inode);
6507 if (err)
6508 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006509
Josef Bacika1b075d2010-11-19 20:36:11 +00006510 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006511 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006512 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006513
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006514 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006515 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006516 d_instantiate(dentry, inode);
6517
Chris Mason39279cc2007-06-12 06:35:45 -04006518out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006519 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006520 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006521 inode_dec_link_count(inode);
6522 iput(inode);
6523 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006524 btrfs_balance_delayed_items(fs_info);
6525 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006526 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006527
6528out_unlock_inode:
6529 unlock_new_inode(inode);
6530 goto out_unlock;
6531
Chris Mason39279cc2007-06-12 06:35:45 -04006532}
6533
6534static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6535 struct dentry *dentry)
6536{
Filipe Manana271dba452016-01-05 16:24:05 +00006537 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006538 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006539 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006540 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006541 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006542 int err;
6543 int drop_inode = 0;
6544
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006545 /* do not allow sys_link's with other subvols of the same device */
6546 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006547 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006548
Mark Fashehf1863732012-08-08 11:32:27 -07006549 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006550 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006551
Chris Mason3de45862008-11-17 21:02:50 -05006552 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006553 if (err)
6554 goto fail;
6555
Yan, Zhenga22285a2010-05-16 10:48:46 -04006556 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006557 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006558 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006559 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006560 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006561 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006562 if (IS_ERR(trans)) {
6563 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006564 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006565 goto fail;
6566 }
Chris Mason5f39d392007-10-15 16:14:19 -04006567
Miao Xie67de1172013-12-26 13:07:06 +08006568 /* There are several dir indexes for this inode, clear the cache. */
6569 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006570 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006571 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006572 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006573 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006574 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006575
Josef Bacika1b075d2010-11-19 20:36:11 +00006576 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006577
Yan, Zhenga5719522009-09-24 09:17:31 -04006578 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006579 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006580 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006581 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006582 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006583 if (err)
6584 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006585 if (inode->i_nlink == 1) {
6586 /*
6587 * If new hard link count is 1, it's a file created
6588 * with open(2) O_TMPFILE flag.
6589 */
6590 err = btrfs_orphan_del(trans, inode);
6591 if (err)
6592 goto fail;
6593 }
Al Viro08c422c2011-12-23 07:58:13 -05006594 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006595 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006596 }
Chris Mason39279cc2007-06-12 06:35:45 -04006597
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006598 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006599fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006600 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006601 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006602 if (drop_inode) {
6603 inode_dec_link_count(inode);
6604 iput(inode);
6605 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006606 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006607 return err;
6608}
6609
Al Viro18bb1db2011-07-26 01:41:39 -04006610static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006611{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006612 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006613 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006614 struct btrfs_trans_handle *trans;
6615 struct btrfs_root *root = BTRFS_I(dir)->root;
6616 int err = 0;
6617 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006618 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006619 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006620
Josef Bacik9ed74f22009-09-11 16:12:44 -04006621 /*
6622 * 2 items for inode and ref
6623 * 2 items for dir items
6624 * 1 for xattr if selinux is on
6625 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006626 trans = btrfs_start_transaction(root, 5);
6627 if (IS_ERR(trans))
6628 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006629
Li Zefan581bb052011-04-20 10:06:11 +08006630 err = btrfs_find_free_ino(root, &objectid);
6631 if (err)
6632 goto out_fail;
6633
Josef Bacikaec74772008-07-24 12:12:38 -04006634 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006635 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6636 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006637 if (IS_ERR(inode)) {
6638 err = PTR_ERR(inode);
6639 goto out_fail;
6640 }
Chris Mason5f39d392007-10-15 16:14:19 -04006641
Chris Mason39279cc2007-06-12 06:35:45 -04006642 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006643 /* these must be set before we unlock the inode */
6644 inode->i_op = &btrfs_dir_inode_operations;
6645 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006646
Eric Paris2a7dba32011-02-01 11:05:39 -05006647 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006648 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006649 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006650
Chris Masondbe674a2008-07-17 12:54:05 -04006651 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006652 err = btrfs_update_inode(trans, root, inode);
6653 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006654 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006655
Josef Bacika1b075d2010-11-19 20:36:11 +00006656 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6657 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006658 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006659 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006660
Chris Mason39279cc2007-06-12 06:35:45 -04006661 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006662 /*
6663 * mkdir is special. We're unlocking after we call d_instantiate
6664 * to avoid a race with nfsd calling d_instantiate.
6665 */
6666 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006667 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006668
6669out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006670 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006671 if (drop_on_err) {
6672 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006673 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006674 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006675 btrfs_balance_delayed_items(fs_info);
6676 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006677 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006678
6679out_fail_inode:
6680 unlock_new_inode(inode);
6681 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006682}
6683
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006684/* Find next extent map of a given extent map, caller needs to ensure locks */
6685static struct extent_map *next_extent_map(struct extent_map *em)
6686{
6687 struct rb_node *next;
6688
6689 next = rb_next(&em->rb_node);
6690 if (!next)
6691 return NULL;
6692 return container_of(next, struct extent_map, rb_node);
6693}
6694
6695static struct extent_map *prev_extent_map(struct extent_map *em)
6696{
6697 struct rb_node *prev;
6698
6699 prev = rb_prev(&em->rb_node);
6700 if (!prev)
6701 return NULL;
6702 return container_of(prev, struct extent_map, rb_node);
6703}
6704
Chris Masond352ac62008-09-29 15:18:18 -04006705/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006706 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006707 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006708 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006709 */
Chris Mason3b951512008-04-17 11:29:12 -04006710static int merge_extent_mapping(struct extent_map_tree *em_tree,
6711 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006712 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006713 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006714{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006715 struct extent_map *prev;
6716 struct extent_map *next;
6717 u64 start;
6718 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006719 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006720
Chris Masone6dcd2d2008-07-17 12:53:50 -04006721 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006722
6723 if (existing->start > map_start) {
6724 next = existing;
6725 prev = prev_extent_map(next);
6726 } else {
6727 prev = existing;
6728 next = next_extent_map(prev);
6729 }
6730
6731 start = prev ? extent_map_end(prev) : em->start;
6732 start = max_t(u64, start, em->start);
6733 end = next ? next->start : extent_map_end(em);
6734 end = min_t(u64, end, extent_map_end(em));
6735 start_diff = start - em->start;
6736 em->start = start;
6737 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006738 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6739 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006740 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006741 em->block_len -= start_diff;
6742 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006743 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006744}
6745
Chris Masonc8b97812008-10-29 14:49:59 -04006746static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006747 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006748 size_t pg_offset, u64 extent_offset,
6749 struct btrfs_file_extent_item *item)
6750{
6751 int ret;
6752 struct extent_buffer *leaf = path->nodes[0];
6753 char *tmp;
6754 size_t max_size;
6755 unsigned long inline_size;
6756 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006757 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006758
6759 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006760 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006761 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6762 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006763 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006764 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006765 if (!tmp)
6766 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006767 ptr = btrfs_file_extent_inline_start(item);
6768
6769 read_extent_buffer(leaf, tmp, ptr, inline_size);
6770
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006771 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006772 ret = btrfs_decompress(compress_type, tmp, page,
6773 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006774 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006775 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006776}
6777
Chris Masond352ac62008-09-29 15:18:18 -04006778/*
6779 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006780 * the ugly parts come from merging extents from the disk with the in-ram
6781 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006782 * where the in-ram extents might be locked pending data=ordered completion.
6783 *
6784 * This also copies inline extents directly into the page.
6785 */
Chris Masond3977122009-01-05 21:25:51 -05006786
Chris Masona52d9a82007-08-27 16:49:44 -04006787struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006788 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006789 int create)
6790{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006791 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006792 int ret;
6793 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006794 u64 extent_start = 0;
6795 u64 extent_end = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006796 u64 objectid = btrfs_ino(BTRFS_I(inode));
Chris Masona52d9a82007-08-27 16:49:44 -04006797 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006798 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006799 struct btrfs_root *root = BTRFS_I(inode)->root;
6800 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006801 struct extent_buffer *leaf;
6802 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006803 struct extent_map *em = NULL;
6804 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006805 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006806 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006807 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006808
Chris Masona52d9a82007-08-27 16:49:44 -04006809again:
Chris Mason890871b2009-09-02 16:24:52 -04006810 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006811 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006812 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006813 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006814 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006815
Chris Masona52d9a82007-08-27 16:49:44 -04006816 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006817 if (em->start > start || em->start + em->len <= start)
6818 free_extent_map(em);
6819 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006820 free_extent_map(em);
6821 else
6822 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006823 }
David Sterba172ddd62011-04-21 00:48:27 +02006824 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006825 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006826 err = -ENOMEM;
6827 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006828 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006829 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006830 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006831 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006832 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006833 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006834
6835 if (!path) {
6836 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006837 if (!path) {
6838 err = -ENOMEM;
6839 goto out;
6840 }
6841 /*
6842 * Chances are we'll be called again, so go ahead and do
6843 * readahead
6844 */
David Sterbae4058b52015-11-27 16:31:35 +01006845 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006846 }
6847
Chris Mason179e29e2007-11-01 11:28:41 -04006848 ret = btrfs_lookup_file_extent(trans, root, path,
6849 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006850 if (ret < 0) {
6851 err = ret;
6852 goto out;
6853 }
6854
6855 if (ret != 0) {
6856 if (path->slots[0] == 0)
6857 goto not_found;
6858 path->slots[0]--;
6859 }
6860
Chris Mason5f39d392007-10-15 16:14:19 -04006861 leaf = path->nodes[0];
6862 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006863 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006864 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006865 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006866 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006867 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006868 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006869 /*
6870 * If we backup past the first extent we want to move forward
6871 * and see if there is an extent in front of us, otherwise we'll
6872 * say there is a hole for our whole search range which can
6873 * cause problems.
6874 */
6875 extent_end = start;
6876 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006877 }
6878
Chris Mason5f39d392007-10-15 16:14:19 -04006879 found_type = btrfs_file_extent_type(leaf, item);
6880 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006881 if (found_type == BTRFS_FILE_EXTENT_REG ||
6882 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006883 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006884 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006885 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6886 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006887 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006888 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006889 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006890 }
Josef Bacik25a50342013-10-14 12:08:38 -04006891next:
Yan Zheng9036c102008-10-30 14:19:41 -04006892 if (start >= extent_end) {
6893 path->slots[0]++;
6894 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6895 ret = btrfs_next_leaf(root, path);
6896 if (ret < 0) {
6897 err = ret;
6898 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006899 }
Yan Zheng9036c102008-10-30 14:19:41 -04006900 if (ret > 0)
6901 goto not_found;
6902 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006903 }
Yan Zheng9036c102008-10-30 14:19:41 -04006904 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6905 if (found_key.objectid != objectid ||
6906 found_key.type != BTRFS_EXTENT_DATA_KEY)
6907 goto not_found;
6908 if (start + len <= found_key.offset)
6909 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006910 if (start > found_key.offset)
6911 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006912 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006913 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006914 em->len = found_key.offset - start;
6915 goto not_found_em;
6916 }
6917
Filipe Manana7ffbb592014-06-09 03:48:05 +01006918 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6919
Yan Zhengd899e052008-10-30 14:25:28 -04006920 if (found_type == BTRFS_FILE_EXTENT_REG ||
6921 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006922 goto insert;
6923 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006924 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006925 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006926 size_t size;
6927 size_t extent_offset;
6928 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006929
Filipe Manana7ffbb592014-06-09 03:48:05 +01006930 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006931 goto out;
Yan689f9342007-10-29 11:41:07 -04006932
Chris Mason514ac8a2014-01-03 21:07:00 -08006933 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006934 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006935 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6936 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006937 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006938 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006939 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006940 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006941 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006942 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006943 if (btrfs_file_extent_compression(leaf, item) !=
6944 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006945 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006946 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006947 if (ret) {
6948 err = ret;
6949 goto out;
6950 }
Chris Masonc8b97812008-10-29 14:49:59 -04006951 } else {
6952 map = kmap(page);
6953 read_extent_buffer(leaf, map + pg_offset, ptr,
6954 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006955 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006956 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006957 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006958 copy_size);
6959 }
Chris Masonc8b97812008-10-29 14:49:59 -04006960 kunmap(page);
6961 }
Chris Mason179e29e2007-11-01 11:28:41 -04006962 flush_dcache_page(page);
6963 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006964 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006965 if (!trans) {
6966 kunmap(page);
6967 free_extent_map(em);
6968 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006969
David Sterbab3b4aa72011-04-21 01:20:15 +02006970 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006971 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006972
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006973 if (IS_ERR(trans))
6974 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006975 goto again;
6976 }
Chris Masonc8b97812008-10-29 14:49:59 -04006977 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006978 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006979 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006980 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006981 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006982 }
Chris Masond1310b22008-01-24 16:13:08 -05006983 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006984 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006985 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006986 }
6987not_found:
6988 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006989 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006990 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006991not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006992 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006993 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006994insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006995 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006996 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006997 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006998 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6999 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007000 err = -EIO;
7001 goto out;
7002 }
Chris Masond1310b22008-01-24 16:13:08 -05007003
7004 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007005 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007006 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007007 /* it is possible that someone inserted the extent into the tree
7008 * while we had the lock dropped. It is also possible that
7009 * an overlapping map exists in the tree
7010 */
Chris Masona52d9a82007-08-27 16:49:44 -04007011 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007012 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007013
7014 ret = 0;
7015
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007016 existing = search_extent_mapping(em_tree, start, len);
7017 /*
7018 * existing will always be non-NULL, since there must be
7019 * extent causing the -EEXIST.
7020 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007021 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007022 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007023 em->block_start == existing->block_start) {
7024 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007025 * The existing extent map already encompasses the
7026 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007027 */
7028 free_extent_map(em);
7029 em = existing;
7030 err = 0;
7031
7032 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007033 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007034 /*
7035 * The existing extent map is the one nearest to
7036 * the [start, start + len) range which overlaps
7037 */
7038 err = merge_extent_mapping(em_tree, existing,
7039 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007040 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007041 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007042 free_extent_map(em);
7043 em = NULL;
7044 }
7045 } else {
7046 free_extent_map(em);
7047 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007048 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007049 }
Chris Masona52d9a82007-08-27 16:49:44 -04007050 }
Chris Mason890871b2009-09-02 16:24:52 -04007051 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007052out:
liubo1abe9b82011-03-24 11:18:59 +00007053
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007054 trace_btrfs_get_extent(root, BTRFS_I(inode), em);
liubo1abe9b82011-03-24 11:18:59 +00007055
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007056 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007057 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007058 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007059 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007060 err = ret;
7061 }
Chris Masona52d9a82007-08-27 16:49:44 -04007062 if (err) {
7063 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007064 return ERR_PTR(err);
7065 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007066 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007067 return em;
7068}
7069
Chris Masonec29ed52011-02-23 16:23:20 -05007070struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7071 size_t pg_offset, u64 start, u64 len,
7072 int create)
7073{
7074 struct extent_map *em;
7075 struct extent_map *hole_em = NULL;
7076 u64 range_start = start;
7077 u64 end;
7078 u64 found;
7079 u64 found_end;
7080 int err = 0;
7081
7082 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7083 if (IS_ERR(em))
7084 return em;
7085 if (em) {
7086 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007087 * if our em maps to
7088 * - a hole or
7089 * - a pre-alloc extent,
7090 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007091 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007092 if (em->block_start != EXTENT_MAP_HOLE &&
7093 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007094 return em;
7095 else
7096 hole_em = em;
7097 }
7098
7099 /* check to see if we've wrapped (len == -1 or similar) */
7100 end = start + len;
7101 if (end < start)
7102 end = (u64)-1;
7103 else
7104 end -= 1;
7105
7106 em = NULL;
7107
7108 /* ok, we didn't find anything, lets look for delalloc */
7109 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7110 end, len, EXTENT_DELALLOC, 1);
7111 found_end = range_start + found;
7112 if (found_end < range_start)
7113 found_end = (u64)-1;
7114
7115 /*
7116 * we didn't find anything useful, return
7117 * the original results from get_extent()
7118 */
7119 if (range_start > end || found_end <= start) {
7120 em = hole_em;
7121 hole_em = NULL;
7122 goto out;
7123 }
7124
7125 /* adjust the range_start to make sure it doesn't
7126 * go backwards from the start they passed in
7127 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307128 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007129 found = found_end - range_start;
7130
7131 if (found > 0) {
7132 u64 hole_start = start;
7133 u64 hole_len = len;
7134
David Sterba172ddd62011-04-21 00:48:27 +02007135 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007136 if (!em) {
7137 err = -ENOMEM;
7138 goto out;
7139 }
7140 /*
7141 * when btrfs_get_extent can't find anything it
7142 * returns one huge hole
7143 *
7144 * make sure what it found really fits our range, and
7145 * adjust to make sure it is based on the start from
7146 * the caller
7147 */
7148 if (hole_em) {
7149 u64 calc_end = extent_map_end(hole_em);
7150
7151 if (calc_end <= start || (hole_em->start > end)) {
7152 free_extent_map(hole_em);
7153 hole_em = NULL;
7154 } else {
7155 hole_start = max(hole_em->start, start);
7156 hole_len = calc_end - hole_start;
7157 }
7158 }
7159 em->bdev = NULL;
7160 if (hole_em && range_start > hole_start) {
7161 /* our hole starts before our delalloc, so we
7162 * have to return just the parts of the hole
7163 * that go until the delalloc starts
7164 */
7165 em->len = min(hole_len,
7166 range_start - hole_start);
7167 em->start = hole_start;
7168 em->orig_start = hole_start;
7169 /*
7170 * don't adjust block start at all,
7171 * it is fixed at EXTENT_MAP_HOLE
7172 */
7173 em->block_start = hole_em->block_start;
7174 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007175 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7176 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007177 } else {
7178 em->start = range_start;
7179 em->len = found;
7180 em->orig_start = range_start;
7181 em->block_start = EXTENT_MAP_DELALLOC;
7182 em->block_len = found;
7183 }
7184 } else if (hole_em) {
7185 return hole_em;
7186 }
7187out:
7188
7189 free_extent_map(hole_em);
7190 if (err) {
7191 free_extent_map(em);
7192 return ERR_PTR(err);
7193 }
7194 return em;
7195}
7196
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007197static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7198 const u64 start,
7199 const u64 len,
7200 const u64 orig_start,
7201 const u64 block_start,
7202 const u64 block_len,
7203 const u64 orig_block_len,
7204 const u64 ram_bytes,
7205 const int type)
7206{
7207 struct extent_map *em = NULL;
7208 int ret;
7209
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007210 if (type != BTRFS_ORDERED_NOCOW) {
7211 em = create_pinned_em(inode, start, len, orig_start,
7212 block_start, block_len, orig_block_len,
7213 ram_bytes, type);
7214 if (IS_ERR(em))
7215 goto out;
7216 }
7217 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7218 len, block_len, type);
7219 if (ret) {
7220 if (em) {
7221 free_extent_map(em);
7222 btrfs_drop_extent_cache(inode, start,
7223 start + len - 1, 0);
7224 }
7225 em = ERR_PTR(ret);
7226 }
7227 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007228
7229 return em;
7230}
7231
Josef Bacik4b46fce2010-05-23 11:00:55 -04007232static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7233 u64 start, u64 len)
7234{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007235 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007236 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007237 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007238 struct btrfs_key ins;
7239 u64 alloc_hint;
7240 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007241
Josef Bacik4b46fce2010-05-23 11:00:55 -04007242 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007243 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007244 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007245 if (ret)
7246 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007247
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007248 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7249 ins.objectid, ins.offset, ins.offset,
7250 ins.offset, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007251 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007252 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007253 btrfs_free_reserved_extent(fs_info, ins.objectid,
7254 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007255
Josef Bacik4b46fce2010-05-23 11:00:55 -04007256 return em;
7257}
7258
Chris Mason46bfbb52010-05-26 11:04:10 -04007259/*
7260 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7261 * block must be cow'd
7262 */
Josef Bacik00361582013-08-14 14:02:47 -04007263noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007264 u64 *orig_start, u64 *orig_block_len,
7265 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007266{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007267 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007268 struct btrfs_path *path;
7269 int ret;
7270 struct extent_buffer *leaf;
7271 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007272 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007273 struct btrfs_file_extent_item *fi;
7274 struct btrfs_key key;
7275 u64 disk_bytenr;
7276 u64 backref_offset;
7277 u64 extent_end;
7278 u64 num_bytes;
7279 int slot;
7280 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007281 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007282
Chris Mason46bfbb52010-05-26 11:04:10 -04007283 path = btrfs_alloc_path();
7284 if (!path)
7285 return -ENOMEM;
7286
David Sterbaf85b7372017-01-20 14:54:07 +01007287 ret = btrfs_lookup_file_extent(NULL, root, path,
7288 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007289 if (ret < 0)
7290 goto out;
7291
7292 slot = path->slots[0];
7293 if (ret == 1) {
7294 if (slot == 0) {
7295 /* can't find the item, must cow */
7296 ret = 0;
7297 goto out;
7298 }
7299 slot--;
7300 }
7301 ret = 0;
7302 leaf = path->nodes[0];
7303 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007304 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007305 key.type != BTRFS_EXTENT_DATA_KEY) {
7306 /* not our file or wrong item type, must cow */
7307 goto out;
7308 }
7309
7310 if (key.offset > offset) {
7311 /* Wrong offset, must cow */
7312 goto out;
7313 }
7314
7315 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7316 found_type = btrfs_file_extent_type(leaf, fi);
7317 if (found_type != BTRFS_FILE_EXTENT_REG &&
7318 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7319 /* not a regular extent, must cow */
7320 goto out;
7321 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007322
7323 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7324 goto out;
7325
Miao Xiee77751a2013-12-27 21:11:50 +08007326 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7327 if (extent_end <= offset)
7328 goto out;
7329
Chris Mason46bfbb52010-05-26 11:04:10 -04007330 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007331 if (disk_bytenr == 0)
7332 goto out;
7333
7334 if (btrfs_file_extent_compression(leaf, fi) ||
7335 btrfs_file_extent_encryption(leaf, fi) ||
7336 btrfs_file_extent_other_encoding(leaf, fi))
7337 goto out;
7338
Chris Mason46bfbb52010-05-26 11:04:10 -04007339 backref_offset = btrfs_file_extent_offset(leaf, fi);
7340
Josef Bacik7ee9e442013-06-21 16:37:03 -04007341 if (orig_start) {
7342 *orig_start = key.offset - backref_offset;
7343 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7344 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7345 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007346
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007347 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007348 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007349
7350 num_bytes = min(offset + *len, extent_end) - offset;
7351 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7352 u64 range_end;
7353
Jeff Mahoneyda170662016-06-15 09:22:56 -04007354 range_end = round_up(offset + num_bytes,
7355 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007356 ret = test_range_bit(io_tree, offset, range_end,
7357 EXTENT_DELALLOC, 0, NULL);
7358 if (ret) {
7359 ret = -EAGAIN;
7360 goto out;
7361 }
7362 }
7363
Josef Bacik1bda19e2013-10-18 12:10:36 -04007364 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007365
7366 /*
7367 * look for other files referencing this extent, if we
7368 * find any we must cow
7369 */
Josef Bacik00361582013-08-14 14:02:47 -04007370
Liu Boe4c3b2d2017-01-30 12:25:28 -08007371 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007372 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007373 if (ret) {
7374 ret = 0;
7375 goto out;
7376 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007377
7378 /*
7379 * adjust disk_bytenr and num_bytes to cover just the bytes
7380 * in this extent we are about to write. If there
7381 * are any csums in that range we have to cow in order
7382 * to keep the csums correct
7383 */
7384 disk_bytenr += backref_offset;
7385 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007386 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7387 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007388 /*
7389 * all of the above have passed, it is safe to overwrite this extent
7390 * without cow
7391 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007392 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007393 ret = 1;
7394out:
7395 btrfs_free_path(path);
7396 return ret;
7397}
7398
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007399bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7400{
7401 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7402 int found = false;
7403 void **pagep = NULL;
7404 struct page *page = NULL;
7405 int start_idx;
7406 int end_idx;
7407
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007408 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007409
7410 /*
7411 * end is the last byte in the last page. end == start is legal
7412 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007413 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007414
7415 rcu_read_lock();
7416
7417 /* Most of the code in this while loop is lifted from
7418 * find_get_page. It's been modified to begin searching from a
7419 * page and return just the first page found in that range. If the
7420 * found idx is less than or equal to the end idx then we know that
7421 * a page exists. If no pages are found or if those pages are
7422 * outside of the range then we're fine (yay!) */
7423 while (page == NULL &&
7424 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7425 page = radix_tree_deref_slot(pagep);
7426 if (unlikely(!page))
7427 break;
7428
7429 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007430 if (radix_tree_deref_retry(page)) {
7431 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007432 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007433 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007434 /*
7435 * Otherwise, shmem/tmpfs must be storing a swap entry
7436 * here as an exceptional entry: so return it without
7437 * attempting to raise page count.
7438 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007439 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007440 break; /* TODO: Is this relevant for this use case? */
7441 }
7442
Filipe Manana91405152014-06-05 13:22:24 +01007443 if (!page_cache_get_speculative(page)) {
7444 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007445 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007446 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007447
7448 /*
7449 * Has the page moved?
7450 * This is part of the lockless pagecache protocol. See
7451 * include/linux/pagemap.h for details.
7452 */
7453 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007454 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007455 page = NULL;
7456 }
7457 }
7458
7459 if (page) {
7460 if (page->index <= end_idx)
7461 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007462 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007463 }
7464
7465 rcu_read_unlock();
7466 return found;
7467}
7468
Josef Bacikeb838e72012-07-31 16:28:48 -04007469static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7470 struct extent_state **cached_state, int writing)
7471{
7472 struct btrfs_ordered_extent *ordered;
7473 int ret = 0;
7474
7475 while (1) {
7476 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007477 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007478 /*
7479 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007480 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007481 * extents in this range.
7482 */
7483 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7484 lockend - lockstart + 1);
7485
7486 /*
7487 * We need to make sure there are no buffered pages in this
7488 * range either, we could have raced between the invalidate in
7489 * generic_file_direct_write and locking the extent. The
7490 * invalidate needs to happen so that reads after a write do not
7491 * get stale data.
7492 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007493 if (!ordered &&
7494 (!writing ||
7495 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007496 break;
7497
7498 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7499 cached_state, GFP_NOFS);
7500
7501 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007502 /*
7503 * If we are doing a DIO read and the ordered extent we
7504 * found is for a buffered write, we can not wait for it
7505 * to complete and retry, because if we do so we can
7506 * deadlock with concurrent buffered writes on page
7507 * locks. This happens only if our DIO read covers more
7508 * than one extent map, if at this point has already
7509 * created an ordered extent for a previous extent map
7510 * and locked its range in the inode's io tree, and a
7511 * concurrent write against that previous extent map's
7512 * range and this range started (we unlock the ranges
7513 * in the io tree only when the bios complete and
7514 * buffered writes always lock pages before attempting
7515 * to lock range in the io tree).
7516 */
7517 if (writing ||
7518 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7519 btrfs_start_ordered_extent(inode, ordered, 1);
7520 else
7521 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007522 btrfs_put_ordered_extent(ordered);
7523 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007524 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007525 * We could trigger writeback for this range (and wait
7526 * for it to complete) and then invalidate the pages for
7527 * this range (through invalidate_inode_pages2_range()),
7528 * but that can lead us to a deadlock with a concurrent
7529 * call to readpages() (a buffered read or a defrag call
7530 * triggered a readahead) on a page lock due to an
7531 * ordered dio extent we created before but did not have
7532 * yet a corresponding bio submitted (whence it can not
7533 * complete), which makes readpages() wait for that
7534 * ordered extent to complete while holding a lock on
7535 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007536 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007537 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007538 }
7539
Filipe Mananaade77022016-02-18 14:28:55 +00007540 if (ret)
7541 break;
7542
Josef Bacikeb838e72012-07-31 16:28:48 -04007543 cond_resched();
7544 }
7545
7546 return ret;
7547}
7548
Josef Bacik69ffb542012-09-11 15:40:07 -04007549static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7550 u64 len, u64 orig_start,
7551 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007552 u64 orig_block_len, u64 ram_bytes,
7553 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007554{
7555 struct extent_map_tree *em_tree;
7556 struct extent_map *em;
7557 struct btrfs_root *root = BTRFS_I(inode)->root;
7558 int ret;
7559
7560 em_tree = &BTRFS_I(inode)->extent_tree;
7561 em = alloc_extent_map();
7562 if (!em)
7563 return ERR_PTR(-ENOMEM);
7564
7565 em->start = start;
7566 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007567 em->mod_start = start;
7568 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007569 em->len = len;
7570 em->block_len = block_len;
7571 em->block_start = block_start;
7572 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007573 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007574 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007575 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007576 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7577 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007578 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007579
7580 do {
7581 btrfs_drop_extent_cache(inode, em->start,
7582 em->start + em->len - 1, 0);
7583 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007584 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007585 write_unlock(&em_tree->lock);
7586 } while (ret == -EEXIST);
7587
7588 if (ret) {
7589 free_extent_map(em);
7590 return ERR_PTR(ret);
7591 }
7592
7593 return em;
7594}
7595
Filipe Manana9c9464c2015-11-04 09:52:04 +00007596static void adjust_dio_outstanding_extents(struct inode *inode,
7597 struct btrfs_dio_data *dio_data,
7598 const u64 len)
7599{
David Sterba823bb202017-01-04 11:09:51 +01007600 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007601
Filipe Manana9c9464c2015-11-04 09:52:04 +00007602 /*
7603 * If we have an outstanding_extents count still set then we're
7604 * within our reservation, otherwise we need to adjust our inode
7605 * counter appropriately.
7606 */
Liu Boc2931662016-12-22 17:13:54 -08007607 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007608 dio_data->outstanding_extents -= num_extents;
7609 } else {
Liu Boc2931662016-12-22 17:13:54 -08007610 /*
7611 * If dio write length has been split due to no large enough
7612 * contiguous space, we need to compensate our inode counter
7613 * appropriately.
7614 */
7615 u64 num_needed = num_extents - dio_data->outstanding_extents;
7616
Filipe Manana9c9464c2015-11-04 09:52:04 +00007617 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007618 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007619 spin_unlock(&BTRFS_I(inode)->lock);
7620 }
7621}
7622
Josef Bacik4b46fce2010-05-23 11:00:55 -04007623static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7624 struct buffer_head *bh_result, int create)
7625{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007626 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007627 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007628 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307629 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007630 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007631 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007632 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007633 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007634 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007635
Miao Xie172a5042013-02-21 02:48:22 -07007636 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007637 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007638 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007639 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007640
Josef Bacikc3298612012-08-03 16:49:19 -04007641 lockstart = start;
7642 lockend = start + len - 1;
7643
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007644 if (current->journal_info) {
7645 /*
7646 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007647 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007648 * confused.
7649 */
chandan50745b02015-08-28 21:10:13 +05307650 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007651 current->journal_info = NULL;
7652 }
7653
Josef Bacikeb838e72012-07-31 16:28:48 -04007654 /*
7655 * If this errors out it's because we couldn't invalidate pagecache for
7656 * this range and we need to fallback to buffered.
7657 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007658 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7659 create)) {
7660 ret = -ENOTBLK;
7661 goto err;
7662 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007663
Josef Bacik4b46fce2010-05-23 11:00:55 -04007664 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007665 if (IS_ERR(em)) {
7666 ret = PTR_ERR(em);
7667 goto unlock_err;
7668 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007669
7670 /*
7671 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7672 * io. INLINE is special, and we could probably kludge it in here, but
7673 * it's still buffered so for safety lets just fall back to the generic
7674 * buffered path.
7675 *
7676 * For COMPRESSED we _have_ to read the entire extent in so we can
7677 * decompress it, so there will be buffering required no matter what we
7678 * do, so go ahead and fallback to buffered.
7679 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007680 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681 * to buffered IO. Don't blame me, this is the price we pay for using
7682 * the generic code.
7683 */
7684 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7685 em->block_start == EXTENT_MAP_INLINE) {
7686 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007687 ret = -ENOTBLK;
7688 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007689 }
7690
7691 /* Just a good old fashioned hole, return */
7692 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7693 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7694 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007695 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007696 }
7697
7698 /*
7699 * We don't allocate a new extent in the following cases
7700 *
7701 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7702 * existing extent.
7703 * 2) The extent is marked as PREALLOC. We're good to go here and can
7704 * just use the extent.
7705 *
7706 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007707 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007708 len = min(len, em->len - (start - em->start));
7709 lockstart = start + len;
7710 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007711 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007712
7713 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7714 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7715 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007716 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007717 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007718
7719 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7720 type = BTRFS_ORDERED_PREALLOC;
7721 else
7722 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007723 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007724 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007725
Josef Bacik00361582013-08-14 14:02:47 -04007726 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007727 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007728 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007729 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007730
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007731 em2 = btrfs_create_dio_extent(inode, start, len,
7732 orig_start, block_start,
7733 len, orig_block_len,
7734 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007735 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007736 if (type == BTRFS_ORDERED_PREALLOC) {
7737 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007738 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007739 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007740 if (em2 && IS_ERR(em2)) {
7741 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007742 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007743 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007744 /*
7745 * For inode marked NODATACOW or extent marked PREALLOC,
7746 * use the existing or preallocated extent, so does not
7747 * need to adjust btrfs_space_info's bytes_may_use.
7748 */
7749 btrfs_free_reserved_data_space_noquota(inode,
7750 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007751 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007752 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007753 }
Josef Bacik00361582013-08-14 14:02:47 -04007754
Chris Mason46bfbb52010-05-26 11:04:10 -04007755 /*
7756 * this will cow the extent, reset the len in case we changed
7757 * it above
7758 */
7759 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007760 free_extent_map(em);
7761 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007762 if (IS_ERR(em)) {
7763 ret = PTR_ERR(em);
7764 goto unlock_err;
7765 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007766 len = min(len, em->len - (start - em->start));
7767unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007768 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7769 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007770 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007771 bh_result->b_bdev = em->bdev;
7772 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007773 if (create) {
7774 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7775 set_buffer_new(bh_result);
7776
7777 /*
7778 * Need to update the i_size under the extent lock so buffered
7779 * readers will get the updated i_size when we unlock.
7780 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007781 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007782 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007783
Filipe Manana9c9464c2015-11-04 09:52:04 +00007784 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307785 WARN_ON(dio_data->reserve < len);
7786 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007787 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307788 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007789 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007790
Josef Bacikeb838e72012-07-31 16:28:48 -04007791 /*
7792 * In the case of write we need to clear and unlock the entire range,
7793 * in the case of read we need to unlock only the end area that we
7794 * aren't using if there is any left over space.
7795 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007796 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007797 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7798 lockend, unlock_bits, 1, 0,
7799 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007800 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007801 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007802 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007803
Josef Bacik4b46fce2010-05-23 11:00:55 -04007804 free_extent_map(em);
7805
7806 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007807
7808unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007809 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7810 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007811err:
chandan50745b02015-08-28 21:10:13 +05307812 if (dio_data)
7813 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007814 /*
7815 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7816 * write less data then expected, so that we don't underflow our inode's
7817 * outstanding extents counter.
7818 */
7819 if (create && dio_data)
7820 adjust_dio_outstanding_extents(inode, dio_data, len);
7821
Josef Bacikeb838e72012-07-31 16:28:48 -04007822 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007823}
7824
Miao Xie8b110e32014-09-12 18:44:03 +08007825static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007826 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007827{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007828 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007829 int ret;
7830
Mike Christie37226b22016-06-05 14:31:52 -05007831 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007832
7833 bio_get(bio);
7834
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007835 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007836 if (ret)
7837 goto err;
7838
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007839 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007840err:
7841 bio_put(bio);
7842 return ret;
7843}
7844
7845static int btrfs_check_dio_repairable(struct inode *inode,
7846 struct bio *failed_bio,
7847 struct io_failure_record *failrec,
7848 int failed_mirror)
7849{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007850 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007851 int num_copies;
7852
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007853 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007854 if (num_copies == 1) {
7855 /*
7856 * we only have a single copy of the data, so don't bother with
7857 * all the retry and error correction code that follows. no
7858 * matter what the error is, it is very likely to persist.
7859 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007860 btrfs_debug(fs_info,
7861 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7862 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007863 return 0;
7864 }
7865
7866 failrec->failed_mirror = failed_mirror;
7867 failrec->this_mirror++;
7868 if (failrec->this_mirror == failed_mirror)
7869 failrec->this_mirror++;
7870
7871 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007872 btrfs_debug(fs_info,
7873 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7874 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007875 return 0;
7876 }
7877
7878 return 1;
7879}
7880
7881static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307882 struct page *page, unsigned int pgoff,
7883 u64 start, u64 end, int failed_mirror,
7884 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007885{
7886 struct io_failure_record *failrec;
7887 struct bio *bio;
7888 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007889 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007890 int ret;
7891
Mike Christie37226b22016-06-05 14:31:52 -05007892 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007893
7894 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7895 if (ret)
7896 return ret;
7897
7898 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7899 failed_mirror);
7900 if (!ret) {
7901 free_io_failure(inode, failrec);
7902 return -EIO;
7903 }
7904
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307905 if ((failed_bio->bi_vcnt > 1)
7906 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007907 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007908 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007909
7910 isector = start - btrfs_io_bio(failed_bio)->logical;
7911 isector >>= inode->i_sb->s_blocksize_bits;
7912 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307913 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007914 if (!bio) {
7915 free_io_failure(inode, failrec);
7916 return -EIO;
7917 }
Mike Christie37226b22016-06-05 14:31:52 -05007918 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007919
7920 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7921 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7922 read_mode, failrec->this_mirror, failrec->in_validation);
7923
Mike Christie81a75f672016-06-05 14:31:54 -05007924 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007925 if (ret) {
7926 free_io_failure(inode, failrec);
7927 bio_put(bio);
7928 }
7929
7930 return ret;
7931}
7932
7933struct btrfs_retry_complete {
7934 struct completion done;
7935 struct inode *inode;
7936 u64 start;
7937 int uptodate;
7938};
7939
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007940static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007941{
7942 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307943 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007944 struct bio_vec *bvec;
7945 int i;
7946
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007947 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007948 goto end;
7949
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307950 ASSERT(bio->bi_vcnt == 1);
7951 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007952 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307953
Miao Xie8b110e32014-09-12 18:44:03 +08007954 done->uptodate = 1;
7955 bio_for_each_segment_all(bvec, bio, i)
7956 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7957end:
7958 complete(&done->done);
7959 bio_put(bio);
7960}
7961
7962static int __btrfs_correct_data_nocsum(struct inode *inode,
7963 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007964{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307965 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007966 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007967 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007968 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307969 unsigned int pgoff;
7970 u32 sectorsize;
7971 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007972 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007973 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007974
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307975 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007976 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307977
Miao Xiec1dc0892014-09-12 18:43:56 +08007978 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007979 done.inode = inode;
7980
7981 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307982 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7983 pgoff = bvec->bv_offset;
7984
7985next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007986 done.uptodate = 0;
7987 done.start = start;
7988 init_completion(&done.done);
7989
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307990 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7991 pgoff, start, start + sectorsize - 1,
7992 io_bio->mirror_num,
7993 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007994 if (ret)
7995 return ret;
7996
7997 wait_for_completion(&done.done);
7998
7999 if (!done.uptodate) {
8000 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308001 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008002 }
8003
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308004 start += sectorsize;
8005
8006 if (nr_sectors--) {
8007 pgoff += sectorsize;
8008 goto next_block_or_try_again;
8009 }
Miao Xie8b110e32014-09-12 18:44:03 +08008010 }
8011
8012 return 0;
8013}
8014
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008015static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008016{
8017 struct btrfs_retry_complete *done = bio->bi_private;
8018 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308019 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008020 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308021 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008022 int uptodate;
8023 int ret;
8024 int i;
8025
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008026 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008027 goto end;
8028
8029 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308030
8031 start = done->start;
8032
8033 ASSERT(bio->bi_vcnt == 1);
8034 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008035 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308036
Miao Xie8b110e32014-09-12 18:44:03 +08008037 bio_for_each_segment_all(bvec, bio, i) {
8038 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308039 bvec->bv_page, bvec->bv_offset,
8040 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008041 if (!ret)
8042 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308043 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008044 else
8045 uptodate = 0;
8046 }
8047
8048 done->uptodate = uptodate;
8049end:
8050 complete(&done->done);
8051 bio_put(bio);
8052}
8053
8054static int __btrfs_subio_endio_read(struct inode *inode,
8055 struct btrfs_io_bio *io_bio, int err)
8056{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308057 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008058 struct bio_vec *bvec;
8059 struct btrfs_retry_complete done;
8060 u64 start;
8061 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062 u32 sectorsize;
8063 int nr_sectors;
8064 unsigned int pgoff;
8065 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008066 int i;
8067 int ret;
8068
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308069 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008070 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308071
Miao Xie8b110e32014-09-12 18:44:03 +08008072 err = 0;
8073 start = io_bio->logical;
8074 done.inode = inode;
8075
Miao Xiec1dc0892014-09-12 18:43:56 +08008076 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308077 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8078
8079 pgoff = bvec->bv_offset;
8080next_block:
8081 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8082 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8083 bvec->bv_page, pgoff, start,
8084 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008085 if (likely(!ret))
8086 goto next;
8087try_again:
8088 done.uptodate = 0;
8089 done.start = start;
8090 init_completion(&done.done);
8091
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308092 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8093 pgoff, start, start + sectorsize - 1,
8094 io_bio->mirror_num,
8095 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008096 if (ret) {
8097 err = ret;
8098 goto next;
8099 }
8100
8101 wait_for_completion(&done.done);
8102
8103 if (!done.uptodate) {
8104 /* We might have another mirror, so try again */
8105 goto try_again;
8106 }
8107next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308108 offset += sectorsize;
8109 start += sectorsize;
8110
8111 ASSERT(nr_sectors);
8112
8113 if (--nr_sectors) {
8114 pgoff += sectorsize;
8115 goto next_block;
8116 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008117 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008118
8119 return err;
8120}
8121
Miao Xie8b110e32014-09-12 18:44:03 +08008122static int btrfs_subio_endio_read(struct inode *inode,
8123 struct btrfs_io_bio *io_bio, int err)
8124{
8125 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8126
8127 if (skip_csum) {
8128 if (unlikely(err))
8129 return __btrfs_correct_data_nocsum(inode, io_bio);
8130 else
8131 return 0;
8132 } else {
8133 return __btrfs_subio_endio_read(inode, io_bio, err);
8134 }
8135}
8136
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008137static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008138{
8139 struct btrfs_dio_private *dip = bio->bi_private;
8140 struct inode *inode = dip->inode;
8141 struct bio *dio_bio;
8142 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008143 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008144
Miao Xie8b110e32014-09-12 18:44:03 +08008145 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8146 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008147
Josef Bacik4b46fce2010-05-23 11:00:55 -04008148 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008149 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008150 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008151
Josef Bacik4b46fce2010-05-23 11:00:55 -04008152 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008153
Filipe Manana1636d1d2016-02-15 16:20:26 +00008154 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008155 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008156
8157 if (io_bio->end_io)
8158 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008159 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008160}
8161
Filipe Manana14543772015-11-24 16:23:54 +00008162static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8163 const u64 offset,
8164 const u64 bytes,
8165 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008166{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008167 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008168 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008169 u64 ordered_offset = offset;
8170 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008171 int ret;
8172
Chris Mason163cf092010-11-28 19:56:33 -05008173again:
8174 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8175 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008176 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008177 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008178 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008179 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008180
Liu Bo9e0af232014-08-15 23:36:53 +08008181 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8182 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008183 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008184out_test:
8185 /*
8186 * our bio might span multiple ordered extents. If we haven't
8187 * completed the accounting for the whole dio, go back and try again
8188 */
Filipe Manana14543772015-11-24 16:23:54 +00008189 if (ordered_offset < offset + bytes) {
8190 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008191 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008192 goto again;
8193 }
Filipe Manana14543772015-11-24 16:23:54 +00008194}
8195
8196static void btrfs_endio_direct_write(struct bio *bio)
8197{
8198 struct btrfs_dio_private *dip = bio->bi_private;
8199 struct bio *dio_bio = dip->dio_bio;
8200
8201 btrfs_endio_direct_write_update_ordered(dip->inode,
8202 dip->logical_offset,
8203 dip->bytes,
8204 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008205
Josef Bacik4b46fce2010-05-23 11:00:55 -04008206 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008207
Filipe Manana1636d1d2016-02-15 16:20:26 +00008208 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008209 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008210 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008211}
8212
Mike Christie81a75f672016-06-05 14:31:54 -05008213static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008214 struct bio *bio, int mirror_num,
8215 unsigned long bio_flags, u64 offset)
8216{
8217 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008218 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008219 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008220 return 0;
8221}
8222
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008223static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008224{
8225 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008226 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008227
Miao Xie8b110e32014-09-12 18:44:03 +08008228 if (err)
8229 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008230 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008231 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8232 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008233 (unsigned long long)bio->bi_iter.bi_sector,
8234 bio->bi_iter.bi_size, err);
8235
8236 if (dip->subio_endio)
8237 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008238
8239 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008240 dip->errors = 1;
8241
8242 /*
8243 * before atomic variable goto zero, we must make sure
8244 * dip->errors is perceived to be set.
8245 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008246 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008247 }
8248
8249 /* if there are more bios still pending for this dio, just exit */
8250 if (!atomic_dec_and_test(&dip->pending_bios))
8251 goto out;
8252
Chris Mason9be33952013-05-17 18:30:14 -04008253 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008254 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008255 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008256 dip->dio_bio->bi_error = 0;
8257 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008258 }
8259out:
8260 bio_put(bio);
8261}
8262
8263static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8264 u64 first_sector, gfp_t gfp_flags)
8265{
Chris Masonda2f0f72015-07-02 13:57:22 -07008266 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008267 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008268 if (bio)
8269 bio_associate_current(bio);
8270 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008271}
8272
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008273static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008274 struct btrfs_dio_private *dip,
8275 struct bio *bio,
8276 u64 file_offset)
8277{
8278 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8279 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8280 int ret;
8281
8282 /*
8283 * We load all the csum data we need when we submit
8284 * the first bio to reduce the csum tree search and
8285 * contention.
8286 */
8287 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008288 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008289 file_offset);
8290 if (ret)
8291 return ret;
8292 }
8293
8294 if (bio == dip->orig_bio)
8295 return 0;
8296
8297 file_offset -= dip->logical_offset;
8298 file_offset >>= inode->i_sb->s_blocksize_bits;
8299 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8300
8301 return 0;
8302}
8303
Miao Xiee65e1532010-11-22 03:04:43 +00008304static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008305 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008306 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008307{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008308 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008309 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008310 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008311 int ret;
8312
Josef Bacikb812ce22012-11-16 13:56:32 -05008313 if (async_submit)
8314 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8315
Miao Xiee65e1532010-11-22 03:04:43 +00008316 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008317
8318 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008319 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008320 if (ret)
8321 goto err;
8322 }
Miao Xiee65e1532010-11-22 03:04:43 +00008323
Josef Bacik1ae39932011-04-06 14:41:34 -04008324 if (skip_sum)
8325 goto map;
8326
8327 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008328 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8329 file_offset,
8330 __btrfs_submit_bio_start_direct_io,
8331 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008332 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008333 } else if (write) {
8334 /*
8335 * If we aren't doing async submit, calculate the csum of the
8336 * bio now.
8337 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008338 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008339 if (ret)
8340 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008341 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008342 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008343 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008344 if (ret)
8345 goto err;
8346 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008347map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008348 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008349err:
8350 bio_put(bio);
8351 return ret;
8352}
8353
Mike Christie81a75f672016-06-05 14:31:54 -05008354static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008355 int skip_sum)
8356{
8357 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008358 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008359 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008360 struct bio *bio;
8361 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008362 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008363 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008364 u64 file_offset = dip->logical_offset;
8365 u64 submit_len = 0;
8366 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008367 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008368 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308369 int nr_sectors;
8370 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008371 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008372
Kent Overstreet4f024f32013-10-11 15:44:27 -07008373 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008374 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8375 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008376 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008377 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008378
Kent Overstreet4f024f32013-10-11 15:44:27 -07008379 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008380 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008381 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008382 goto submit;
8383 }
8384
David Woodhouse53b381b2013-01-29 18:40:14 -05008385 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008386 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008387 async_submit = 0;
8388 else
8389 async_submit = 1;
8390
Josef Bacik02f57c72011-04-06 14:25:44 -04008391 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8392 if (!bio)
8393 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008394
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008395 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008396 bio->bi_private = dip;
8397 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008398 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008399 atomic_inc(&dip->pending_bios);
8400
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008401 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008402 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308403 i = 0;
8404next_block:
8405 if (unlikely(map_length < submit_len + blocksize ||
8406 bio_add_page(bio, bvec->bv_page, blocksize,
8407 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008408 /*
8409 * inc the count before we submit the bio so
8410 * we know the end IO handler won't happen before
8411 * we inc the count. Otherwise, the dip might get freed
8412 * before we're done setting it up
8413 */
8414 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008415 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008416 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008417 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008418 if (ret) {
8419 bio_put(bio);
8420 atomic_dec(&dip->pending_bios);
8421 goto out_err;
8422 }
8423
Miao Xiee65e1532010-11-22 03:04:43 +00008424 start_sector += submit_len >> 9;
8425 file_offset += submit_len;
8426
8427 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008428
8429 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8430 start_sector, GFP_NOFS);
8431 if (!bio)
8432 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008433 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008434 bio->bi_private = dip;
8435 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008436 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008437
Kent Overstreet4f024f32013-10-11 15:44:27 -07008438 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008439 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008440 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008441 &map_length, NULL, 0);
8442 if (ret) {
8443 bio_put(bio);
8444 goto out_err;
8445 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308446
8447 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008448 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308449 submit_len += blocksize;
8450 if (--nr_sectors) {
8451 i++;
8452 goto next_block;
8453 }
Miao Xiee65e1532010-11-22 03:04:43 +00008454 }
8455 }
8456
Josef Bacik02f57c72011-04-06 14:25:44 -04008457submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008458 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008459 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008460 if (!ret)
8461 return 0;
8462
8463 bio_put(bio);
8464out_err:
8465 dip->errors = 1;
8466 /*
8467 * before atomic variable goto zero, we must
8468 * make sure dip->errors is perceived to be set.
8469 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008470 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008471 if (atomic_dec_and_test(&dip->pending_bios))
8472 bio_io_error(dip->orig_bio);
8473
8474 /* bio_end_io() will handle error, so we needn't return it */
8475 return 0;
8476}
8477
Mike Christie8a4c1e42016-06-05 14:31:50 -05008478static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8479 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008480{
Filipe Manana61de7182015-07-01 12:13:10 +01008481 struct btrfs_dio_private *dip = NULL;
8482 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008483 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008484 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008485 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008486 int ret = 0;
8487
8488 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8489
Chris Mason9be33952013-05-17 18:30:14 -04008490 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008491 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008492 ret = -ENOMEM;
8493 goto free_ordered;
8494 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008495
Miao Xiec1dc0892014-09-12 18:43:56 +08008496 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008497 if (!dip) {
8498 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008499 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008500 }
8501
8502 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008503 dip->inode = inode;
8504 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008505 dip->bytes = dio_bio->bi_iter.bi_size;
8506 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008507 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008508 dip->orig_bio = io_bio;
8509 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008510 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008511 btrfs_bio = btrfs_io_bio(io_bio);
8512 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008513
Miao Xiec1dc0892014-09-12 18:43:56 +08008514 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008515 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008516 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008517 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008518 dip->subio_endio = btrfs_subio_endio_read;
8519 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008520
Filipe Mananaf28a4922015-12-08 19:23:20 +00008521 /*
8522 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8523 * even if we fail to submit a bio, because in such case we do the
8524 * corresponding error handling below and it must not be done a second
8525 * time by btrfs_direct_IO().
8526 */
8527 if (write) {
8528 struct btrfs_dio_data *dio_data = current->journal_info;
8529
8530 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8531 dip->bytes;
8532 dio_data->unsubmitted_oe_range_start =
8533 dio_data->unsubmitted_oe_range_end;
8534 }
8535
Mike Christie81a75f672016-06-05 14:31:54 -05008536 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008537 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008538 return;
Chris Mason9be33952013-05-17 18:30:14 -04008539
Miao Xie23ea8e52014-09-12 18:43:54 +08008540 if (btrfs_bio->end_io)
8541 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008542
Josef Bacik4b46fce2010-05-23 11:00:55 -04008543free_ordered:
8544 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008545 * If we arrived here it means either we failed to submit the dip
8546 * or we either failed to clone the dio_bio or failed to allocate the
8547 * dip. If we cloned the dio_bio and allocated the dip, we can just
8548 * call bio_endio against our io_bio so that we get proper resource
8549 * cleanup if we fail to submit the dip, otherwise, we must do the
8550 * same as btrfs_endio_direct_[write|read] because we can't call these
8551 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008552 */
Filipe Manana61de7182015-07-01 12:13:10 +01008553 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008554 io_bio->bi_error = -EIO;
8555 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008556 /*
8557 * The end io callbacks free our dip, do the final put on io_bio
8558 * and all the cleanup and final put for dio_bio (through
8559 * dio_end_io()).
8560 */
8561 dip = NULL;
8562 io_bio = NULL;
8563 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008564 if (write)
8565 btrfs_endio_direct_write_update_ordered(inode,
8566 file_offset,
8567 dio_bio->bi_iter.bi_size,
8568 0);
8569 else
Filipe Manana61de7182015-07-01 12:13:10 +01008570 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8571 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008572
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008573 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008574 /*
8575 * Releases and cleans up our dio_bio, no need to bio_put()
8576 * nor bio_endio()/bio_io_error() against dio_bio.
8577 */
8578 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008579 }
Filipe Manana61de7182015-07-01 12:13:10 +01008580 if (io_bio)
8581 bio_put(io_bio);
8582 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008583}
8584
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008585static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8586 struct kiocb *iocb,
8587 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008588{
8589 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008590 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008591 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008592 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008593
8594 if (offset & blocksize_mask)
8595 goto out;
8596
Al Viro28060d52014-03-22 05:15:17 -04008597 if (iov_iter_alignment(iter) & blocksize_mask)
8598 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008599
Al Viro28060d52014-03-22 05:15:17 -04008600 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008601 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008602 return 0;
8603 /*
8604 * Check to make sure we don't have duplicate iov_base's in this
8605 * iovec, if so return EINVAL, otherwise we'll get csum errors
8606 * when reading back.
8607 */
8608 for (seg = 0; seg < iter->nr_segs; seg++) {
8609 for (i = seg + 1; i < iter->nr_segs; i++) {
8610 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008611 goto out;
8612 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008613 }
8614 retval = 0;
8615out:
8616 return retval;
8617}
Josef Bacikeb838e72012-07-31 16:28:48 -04008618
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008619static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008620{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008621 struct file *file = iocb->ki_filp;
8622 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008623 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308624 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008625 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008626 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008627 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008628 bool wakeup = true;
8629 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008630 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008631
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008632 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008633 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008634
Jens Axboefe0f07d2015-04-15 17:05:48 -06008635 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008636 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008637
Josef Bacik0e267c42013-07-02 10:38:02 -04008638 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008639 * The generic stuff only does filemap_write_and_wait_range, which
8640 * isn't enough if we've written compressed pages to this area, so
8641 * we need to flush the dirty pages again to make absolutely sure
8642 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008643 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008644 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008645 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8646 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008647 filemap_fdatawrite_range(inode->i_mapping, offset,
8648 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008649
Omar Sandoval6f673762015-03-16 04:33:52 -07008650 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008651 /*
8652 * If the write DIO is beyond the EOF, we need update
8653 * the isize, but it is protected by i_mutex. So we can
8654 * not unlock the i_mutex at this case.
8655 */
8656 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008657 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008658 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008659 relock = true;
8660 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008661 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008662 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008663 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008664 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008665
8666 /*
8667 * We need to know how many extents we reserved so that we can
8668 * do the accounting properly if we go over the number we
8669 * originally calculated. Abuse current->journal_info for this.
8670 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008671 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008672 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008673 dio_data.unsubmitted_oe_range_start = (u64)offset;
8674 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308675 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308676 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008677 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8678 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008679 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008680 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8681 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008682 }
8683
Omar Sandoval17f8c842015-03-16 04:33:50 -07008684 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008685 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008686 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008687 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008688 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308689 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008690 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008691 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308692 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008693 btrfs_delalloc_release_space(inode, offset,
8694 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008695 /*
8696 * On error we might have left some ordered extents
8697 * without submitting corresponding bios for them, so
8698 * cleanup them up to avoid other tasks getting them
8699 * and waiting for them to complete forever.
8700 */
8701 if (dio_data.unsubmitted_oe_range_start <
8702 dio_data.unsubmitted_oe_range_end)
8703 btrfs_endio_direct_write_update_ordered(inode,
8704 dio_data.unsubmitted_oe_range_start,
8705 dio_data.unsubmitted_oe_range_end -
8706 dio_data.unsubmitted_oe_range_start,
8707 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008708 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008709 btrfs_delalloc_release_space(inode, offset,
8710 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008711 }
Miao Xie38851cc2013-02-08 07:04:11 +00008712out:
Miao Xie2e60a512013-02-08 07:01:08 +00008713 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008714 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008715 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008716 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008717
8718 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008719}
8720
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008721#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8722
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008723static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8724 __u64 start, __u64 len)
8725{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008726 int ret;
8727
8728 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8729 if (ret)
8730 return ret;
8731
Chris Masonec29ed52011-02-23 16:23:20 -05008732 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008733}
8734
Chris Mason9ebefb182007-06-15 13:50:00 -04008735int btrfs_readpage(struct file *file, struct page *page)
8736{
Chris Masond1310b22008-01-24 16:13:08 -05008737 struct extent_io_tree *tree;
8738 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008739 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008740}
Chris Mason1832a6d2007-12-21 16:27:21 -05008741
Chris Mason39279cc2007-06-12 06:35:45 -04008742static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8743{
Chris Masond1310b22008-01-24 16:13:08 -05008744 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008745 struct inode *inode = page->mapping->host;
8746 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008747
8748 if (current->flags & PF_MEMALLOC) {
8749 redirty_page_for_writepage(wbc, page);
8750 unlock_page(page);
8751 return 0;
8752 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008753
8754 /*
8755 * If we are under memory pressure we will call this directly from the
8756 * VM, we need to make sure we have the inode referenced for the ordered
8757 * extent. If not just return like we didn't do anything.
8758 */
8759 if (!igrab(inode)) {
8760 redirty_page_for_writepage(wbc, page);
8761 return AOP_WRITEPAGE_ACTIVATE;
8762 }
Chris Masond1310b22008-01-24 16:13:08 -05008763 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008764 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8765 btrfs_add_delayed_iput(inode);
8766 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008767}
Chris Mason39279cc2007-06-12 06:35:45 -04008768
Eric Sandeen48a3b632013-04-25 20:41:01 +00008769static int btrfs_writepages(struct address_space *mapping,
8770 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008771{
Chris Masond1310b22008-01-24 16:13:08 -05008772 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008773
Chris Masond1310b22008-01-24 16:13:08 -05008774 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008775 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8776}
8777
Chris Mason3ab2fb52007-11-08 10:59:22 -05008778static int
8779btrfs_readpages(struct file *file, struct address_space *mapping,
8780 struct list_head *pages, unsigned nr_pages)
8781{
Chris Masond1310b22008-01-24 16:13:08 -05008782 struct extent_io_tree *tree;
8783 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008784 return extent_readpages(tree, mapping, pages, nr_pages,
8785 btrfs_get_extent);
8786}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008787static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008788{
Chris Masond1310b22008-01-24 16:13:08 -05008789 struct extent_io_tree *tree;
8790 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008791 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008792
Chris Masond1310b22008-01-24 16:13:08 -05008793 tree = &BTRFS_I(page->mapping->host)->io_tree;
8794 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008795 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008796 if (ret == 1) {
8797 ClearPagePrivate(page);
8798 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008799 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008800 }
8801 return ret;
8802}
Chris Mason39279cc2007-06-12 06:35:45 -04008803
Chris Masone6dcd2d2008-07-17 12:53:50 -04008804static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8805{
Chris Mason98509cf2008-09-11 15:51:43 -04008806 if (PageWriteback(page) || PageDirty(page))
8807 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008808 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008809}
8810
Lukas Czernerd47992f2013-05-21 23:17:23 -04008811static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8812 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008813{
Josef Bacik5fd02042012-05-02 14:00:54 -04008814 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008815 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008816 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008817 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008818 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008819 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308820 u64 start;
8821 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008822 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008823
Chris Mason8b62b722009-09-02 16:53:46 -04008824 /*
8825 * we have the page locked, so new writeback can't start,
8826 * and the dirty bit won't be cleared while we are here.
8827 *
8828 * Wait for IO on this page so that we can safely clear
8829 * the PagePrivate2 bit and do ordered accounting
8830 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008831 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008832
Josef Bacik5fd02042012-05-02 14:00:54 -04008833 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008834 if (offset) {
8835 btrfs_releasepage(page, GFP_NOFS);
8836 return;
8837 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008838
8839 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008840 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308841again:
8842 start = page_start;
8843 ordered = btrfs_lookup_ordered_range(inode, start,
8844 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008845 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308846 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008847 /*
8848 * IO on this page will never be started, so we need
8849 * to account for any ordered extents now
8850 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008851 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308852 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008853 EXTENT_DIRTY | EXTENT_DELALLOC |
8854 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8855 EXTENT_DEFRAG, 1, 0, &cached_state,
8856 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008857 /*
8858 * whoever cleared the private bit is responsible
8859 * for the finish_ordered_io
8860 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008861 if (TestClearPagePrivate2(page)) {
8862 struct btrfs_ordered_inode_tree *tree;
8863 u64 new_len;
8864
8865 tree = &BTRFS_I(inode)->ordered_tree;
8866
8867 spin_lock_irq(&tree->lock);
8868 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308869 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008870 if (new_len < ordered->truncated_len)
8871 ordered->truncated_len = new_len;
8872 spin_unlock_irq(&tree->lock);
8873
8874 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308875 start,
8876 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008877 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008878 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008879 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008880 if (!inode_evicting) {
8881 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308882 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008883 &cached_state);
8884 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308885
8886 start = end + 1;
8887 if (start < page_end)
8888 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008889 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008890
Qu Wenruob9d0b382015-09-29 10:35:16 +08008891 /*
8892 * Qgroup reserved space handler
8893 * Page here will be either
8894 * 1) Already written to disk
8895 * In this case, its reserved space is released from data rsv map
8896 * and will be freed by delayed_ref handler finally.
8897 * So even we call qgroup_free_data(), it won't decrease reserved
8898 * space.
8899 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008900 * This means the reserved space should be freed here. However,
8901 * if a truncate invalidates the page (by clearing PageDirty)
8902 * and the page is accounted for while allocating extent
8903 * in btrfs_check_data_free_space() we let delayed_ref to
8904 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008905 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008906 if (PageDirty(page))
8907 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008908 if (!inode_evicting) {
8909 clear_extent_bit(tree, page_start, page_end,
8910 EXTENT_LOCKED | EXTENT_DIRTY |
8911 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8912 EXTENT_DEFRAG, 1, 1,
8913 &cached_state, GFP_NOFS);
8914
8915 __btrfs_releasepage(page, GFP_NOFS);
8916 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008917
Chris Mason4a096752008-07-21 10:29:44 -04008918 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008919 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008920 ClearPagePrivate(page);
8921 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008922 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008923 }
Chris Mason39279cc2007-06-12 06:35:45 -04008924}
8925
Chris Mason9ebefb182007-06-15 13:50:00 -04008926/*
8927 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8928 * called from a page fault handler when a page is first dirtied. Hence we must
8929 * be careful to check for EOF conditions here. We set the page up correctly
8930 * for a written page which means we get ENOSPC checking when writing into
8931 * holes and correct delalloc and unwritten extent mapping on filesystems that
8932 * support these features.
8933 *
8934 * We are not allowed to take the i_mutex here so we have to play games to
8935 * protect against truncate races as the page could now be beyond EOF. Because
8936 * vmtruncate() writes the inode size before removing pages, once we have the
8937 * page lock we can determine safely if the page is beyond EOF. If it is not
8938 * beyond EOF, then the page is guaranteed safe against truncation until we
8939 * unlock the page.
8940 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008941int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008942{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008943 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008944 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008946 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8947 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008948 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008949 char *kaddr;
8950 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008951 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008952 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008953 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308954 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008955 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008956 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308957 u64 end;
8958
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008959 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008960
Jan Karab2b5ef52012-06-12 16:20:45 +02008961 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008962 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008963 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308964 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008965
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308966 /*
8967 * Reserving delalloc space after obtaining the page lock can lead to
8968 * deadlock. For example, if a dirty page is locked by this function
8969 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8970 * dirty page write out, then the btrfs_writepage() function could
8971 * end up waiting indefinitely to get a lock on the page currently
8972 * being processed by btrfs_page_mkwrite() function.
8973 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008974 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308975 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008976 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008977 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008978 reserved = 1;
8979 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008980 if (ret) {
8981 if (ret == -ENOMEM)
8982 ret = VM_FAULT_OOM;
8983 else /* -ENOSPC, -EIO, etc */
8984 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008985 if (reserved)
8986 goto out;
8987 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008988 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008989
Nick Piggin56a76f82009-03-31 15:23:23 -07008990 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008991again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008992 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008993 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008994
Chris Mason9ebefb182007-06-15 13:50:00 -04008995 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008996 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008997 /* page got truncated out from underneath us */
8998 goto out_unlock;
8999 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009000 wait_on_page_writeback(page);
9001
David Sterbaff13db42015-12-03 14:30:40 +01009002 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009003 set_page_extent_mapped(page);
9004
Chris Masoneb84ae02008-07-17 13:53:27 -04009005 /*
9006 * we can't set the delalloc bits if there are pending ordered
9007 * extents. Drop our locks and wait for them to finish
9008 */
Liu Bo9a9239a2017-01-24 15:58:51 -08009009 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009010 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009011 unlock_extent_cached(io_tree, page_start, page_end,
9012 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009013 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009014 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009015 btrfs_put_ordered_extent(ordered);
9016 goto again;
9017 }
9018
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009019 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009020 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009021 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009022 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309023 end = page_start + reserved_space - 1;
9024 spin_lock(&BTRFS_I(inode)->lock);
9025 BTRFS_I(inode)->outstanding_extents++;
9026 spin_unlock(&BTRFS_I(inode)->lock);
9027 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009028 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309029 }
9030 }
9031
Josef Bacikfbf19082009-10-01 17:10:23 -04009032 /*
Liu Bo54160342016-12-13 12:15:19 -08009033 * page_mkwrite gets called when the page is firstly dirtied after it's
9034 * faulted in, but write(2) could also dirty a page and set delalloc
9035 * bits, thus in this case for space account reason, we still need to
9036 * clear any delalloc bits within this page range since we have to
9037 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009038 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309039 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009040 EXTENT_DIRTY | EXTENT_DELALLOC |
9041 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009042 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009043
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309044 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009045 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009046 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009047 unlock_extent_cached(io_tree, page_start, page_end,
9048 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009049 ret = VM_FAULT_SIGBUS;
9050 goto out_unlock;
9051 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009052 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009053
9054 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009055 if (page_start + PAGE_SIZE > size)
9056 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009057 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009058 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009059
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009060 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009061 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009062 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009063 flush_dcache_page(page);
9064 kunmap(page);
9065 }
Chris Mason247e7432008-07-17 12:53:51 -04009066 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009067 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009068 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009069
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009070 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009071 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009072 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009073
Josef Bacik2ac55d42010-02-03 19:33:23 +00009074 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009075
9076out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009077 if (!ret) {
9078 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009079 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009080 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009081 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009082out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309083 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009084out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009085 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009086 return ret;
9087}
9088
Josef Bacika41ad392011-01-31 15:30:16 -05009089static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009090{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009091 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009092 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009093 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009094 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009095 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009096 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009097 u64 mask = fs_info->sectorsize - 1;
9098 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009099
Josef Bacik0ef8b722013-10-25 16:13:35 -04009100 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9101 (u64)-1);
9102 if (ret)
9103 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009104
Josef Bacikfcb80c22011-05-03 10:40:22 -04009105 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009106 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009107 * 3 things going on here
9108 *
9109 * 1) We need to reserve space for our orphan item and the space to
9110 * delete our orphan item. Lord knows we don't want to have a dangling
9111 * orphan item because we didn't reserve space to remove it.
9112 *
9113 * 2) We need to reserve space to update our inode.
9114 *
9115 * 3) We need to have something to cache all the space that is going to
9116 * be free'd up by the truncate operation, but also have some slack
9117 * space reserved in case it uses space during the truncate (thank you
9118 * very much snapshotting).
9119 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009120 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009121 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009122 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009123 * doesn't end up using space reserved for updating the inode or
9124 * removing the orphan item. We also need to be able to stop the
9125 * transaction and start a new one, which means we need to be able to
9126 * update the inode several times, and we have no idea of knowing how
9127 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009128 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009129 * Then there is the orphan item, which does indeed need to be held on
9130 * to for the whole operation, and we need nobody to touch this reserved
9131 * space except the orphan code.
9132 *
9133 * So that leaves us with
9134 *
9135 * 1) root->orphan_block_rsv - for the orphan deletion.
9136 * 2) rsv - for the truncate reservation, which we will steal from the
9137 * transaction reservation.
9138 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9139 * updating the inode.
9140 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009141 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009142 if (!rsv)
9143 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009144 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009145 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009146
Josef Bacik907cbce2011-08-08 13:46:15 -04009147 /*
Josef Bacik07127182011-08-19 10:29:59 -04009148 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009149 * 1 for updating the inode.
9150 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009151 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009152 if (IS_ERR(trans)) {
9153 err = PTR_ERR(trans);
9154 goto out;
9155 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009156
Josef Bacik907cbce2011-08-08 13:46:15 -04009157 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009158 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009159 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009160 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009161
Chris Mason5a3f23d2009-03-31 13:27:11 -04009162 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009163 * So if we truncate and then write and fsync we normally would just
9164 * write the extents that changed, which is a problem if we need to
9165 * first truncate that entire inode. So set this flag so we write out
9166 * all of the extents in the inode to the sync log so we're completely
9167 * safe.
9168 */
9169 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009170 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009171
Yan, Zheng80825102009-11-12 09:35:36 +00009172 while (1) {
9173 ret = btrfs_truncate_inode_items(trans, root, inode,
9174 inode->i_size,
9175 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009176 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009177 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009178 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009179 }
Chris Mason39279cc2007-06-12 06:35:45 -04009180
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009181 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009182 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009183 if (ret) {
9184 err = ret;
9185 break;
9186 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009187
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009188 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009189 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009190
9191 trans = btrfs_start_transaction(root, 2);
9192 if (IS_ERR(trans)) {
9193 ret = err = PTR_ERR(trans);
9194 trans = NULL;
9195 break;
9196 }
9197
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009198 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009199 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009200 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009201 BUG_ON(ret); /* shouldn't happen */
9202 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009203 }
9204
9205 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009206 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009207 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009208 if (ret)
9209 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009210 }
9211
Chris Mason917c16b2011-11-08 14:49:59 -05009212 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009213 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009214 ret = btrfs_update_inode(trans, root, inode);
9215 if (ret && !err)
9216 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009217
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009218 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009219 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009220 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009221out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009222 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009223
Josef Bacik3893e332011-01-31 16:03:11 -05009224 if (ret && !err)
9225 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009226
Josef Bacik3893e332011-01-31 16:03:11 -05009227 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009228}
9229
Sven Wegener3b963622008-06-09 21:57:42 -04009230/*
Chris Masond352ac62008-09-29 15:18:18 -04009231 * create a new subvolume directory/inode (helper for the ioctl).
9232 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009233int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009234 struct btrfs_root *new_root,
9235 struct btrfs_root *parent_root,
9236 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009237{
Chris Mason39279cc2007-06-12 06:35:45 -04009238 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009239 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009240 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009241
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009242 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9243 new_dirid, new_dirid,
9244 S_IFDIR | (~current_umask() & S_IRWXUGO),
9245 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009246 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009247 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009248 inode->i_op = &btrfs_dir_inode_operations;
9249 inode->i_fop = &btrfs_dir_file_operations;
9250
Miklos Szeredibfe86842011-10-28 14:13:29 +02009251 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009252 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009253 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009254
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009255 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9256 if (err)
9257 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009258 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009259 new_root->root_key.objectid, err);
9260
Yan, Zheng76dda932009-09-21 16:00:26 -04009261 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009262
Yan, Zheng76dda932009-09-21 16:00:26 -04009263 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009264 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009265}
9266
Chris Mason39279cc2007-06-12 06:35:45 -04009267struct inode *btrfs_alloc_inode(struct super_block *sb)
9268{
9269 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009270 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009271
9272 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9273 if (!ei)
9274 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009275
9276 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009277 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009278 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009279 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009280 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009281 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009282 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009283 ei->disk_i_size = 0;
9284 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009285 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009286 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009287 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009288 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009289 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009290 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009291
Josef Bacik9e0baf62011-07-15 15:16:44 +00009292 spin_lock_init(&ei->lock);
9293 ei->outstanding_extents = 0;
9294 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009295
Josef Bacik72ac3c02012-05-23 14:13:11 -04009296 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009297 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009298
Miao Xie16cdcec2011-04-22 18:12:22 +08009299 ei->delayed_node = NULL;
9300
chandan r9cc97d62012-07-04 12:48:07 +05309301 ei->i_otime.tv_sec = 0;
9302 ei->i_otime.tv_nsec = 0;
9303
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009304 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009305 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009306 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9307 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009308 ei->io_tree.track_uptodate = 1;
9309 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009310 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009311 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009312 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009313 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009314 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009315 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009316 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009317 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009318
9319 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009320}
9321
Josef Bacikaaedb552013-10-11 14:44:09 -04009322#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9323void btrfs_test_destroy_inode(struct inode *inode)
9324{
9325 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9326 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9327}
9328#endif
9329
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009330static void btrfs_i_callback(struct rcu_head *head)
9331{
9332 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009333 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9334}
9335
Chris Mason39279cc2007-06-12 06:35:45 -04009336void btrfs_destroy_inode(struct inode *inode)
9337{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009338 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009339 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009340 struct btrfs_root *root = BTRFS_I(inode)->root;
9341
Al Virob3d9b7a2012-06-09 13:51:19 -04009342 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009343 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009344 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9345 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009346 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9347 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009348 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009349
Chris Mason5a3f23d2009-03-31 13:27:11 -04009350 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009351 * This can happen where we create an inode, but somebody else also
9352 * created the same inode and we need to destroy the one we already
9353 * created.
9354 */
9355 if (!root)
9356 goto free;
9357
Josef Bacik8a35d952012-05-23 14:26:42 -04009358 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9359 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009360 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009361 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009362 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009363 }
Josef Bacik7b128762008-07-24 12:17:14 -04009364
Chris Masond3977122009-01-05 21:25:51 -05009365 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009366 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9367 if (!ordered)
9368 break;
9369 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009370 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009371 "found ordered extent %llu %llu on inode cleanup",
9372 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009373 btrfs_remove_ordered_extent(inode, ordered);
9374 btrfs_put_ordered_extent(ordered);
9375 btrfs_put_ordered_extent(ordered);
9376 }
9377 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009378 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009379 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009380 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009381free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009382 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009383}
9384
Al Viro45321ac2010-06-07 13:43:19 -04009385int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009386{
9387 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009388
Naohiro Aota6379ef92013-06-06 09:56:34 +00009389 if (root == NULL)
9390 return 1;
9391
Liu Bofa6ac872013-02-20 14:10:23 +00009392 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009393 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009394 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009395 else
Al Viro45321ac2010-06-07 13:43:19 -04009396 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009397}
9398
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009399static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009400{
9401 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9402
9403 inode_init_once(&ei->vfs_inode);
9404}
9405
9406void btrfs_destroy_cachep(void)
9407{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009408 /*
9409 * Make sure all delayed rcu free inodes are flushed before we
9410 * destroy cache.
9411 */
9412 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009413 kmem_cache_destroy(btrfs_inode_cachep);
9414 kmem_cache_destroy(btrfs_trans_handle_cachep);
9415 kmem_cache_destroy(btrfs_transaction_cachep);
9416 kmem_cache_destroy(btrfs_path_cachep);
9417 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009418}
9419
9420int btrfs_init_cachep(void)
9421{
David Sterba837e1972012-09-07 03:00:48 -06009422 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009423 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009424 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9425 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009426 if (!btrfs_inode_cachep)
9427 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009428
David Sterba837e1972012-09-07 03:00:48 -06009429 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009430 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009431 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009432 if (!btrfs_trans_handle_cachep)
9433 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009434
David Sterba837e1972012-09-07 03:00:48 -06009435 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009436 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009437 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009438 if (!btrfs_transaction_cachep)
9439 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009440
David Sterba837e1972012-09-07 03:00:48 -06009441 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009442 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009443 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009444 if (!btrfs_path_cachep)
9445 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009446
David Sterba837e1972012-09-07 03:00:48 -06009447 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009448 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009449 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009450 if (!btrfs_free_space_cachep)
9451 goto fail;
9452
Chris Mason39279cc2007-06-12 06:35:45 -04009453 return 0;
9454fail:
9455 btrfs_destroy_cachep();
9456 return -ENOMEM;
9457}
9458
9459static int btrfs_getattr(struct vfsmount *mnt,
9460 struct dentry *dentry, struct kstat *stat)
9461{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009462 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009463 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009464 u32 blocksize = inode->i_sb->s_blocksize;
9465
Chris Mason39279cc2007-06-12 06:35:45 -04009466 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009467 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009468
9469 spin_lock(&BTRFS_I(inode)->lock);
9470 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9471 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009472 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009473 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009474 return 0;
9475}
9476
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009477static int btrfs_rename_exchange(struct inode *old_dir,
9478 struct dentry *old_dentry,
9479 struct inode *new_dir,
9480 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009481{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009482 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009483 struct btrfs_trans_handle *trans;
9484 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009485 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009486 struct inode *new_inode = new_dentry->d_inode;
9487 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009488 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009489 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009490 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9491 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009492 u64 old_idx = 0;
9493 u64 new_idx = 0;
9494 u64 root_objectid;
9495 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009496 bool root_log_pinned = false;
9497 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009498
9499 /* we only allow rename subvolume link between subvolumes */
9500 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9501 return -EXDEV;
9502
9503 /* close the race window with snapshot create/destroy ioctl */
9504 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009505 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009506 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009507 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508
9509 /*
9510 * We want to reserve the absolute worst case amount of items. So if
9511 * both inodes are subvols and we need to unlink them then that would
9512 * require 4 item modifications, but if they are both normal inodes it
9513 * would require 5 item modifications, so we'll assume their normal
9514 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9515 * should cover the worst case number of items we'll modify.
9516 */
9517 trans = btrfs_start_transaction(root, 12);
9518 if (IS_ERR(trans)) {
9519 ret = PTR_ERR(trans);
9520 goto out_notrans;
9521 }
9522
9523 /*
9524 * We need to find a free sequence number both in the source and
9525 * in the destination directory for the exchange.
9526 */
9527 ret = btrfs_set_inode_index(new_dir, &old_idx);
9528 if (ret)
9529 goto out_fail;
9530 ret = btrfs_set_inode_index(old_dir, &new_idx);
9531 if (ret)
9532 goto out_fail;
9533
9534 BTRFS_I(old_inode)->dir_index = 0ULL;
9535 BTRFS_I(new_inode)->dir_index = 0ULL;
9536
9537 /* Reference for the source. */
9538 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9539 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009540 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009541 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009542 btrfs_pin_log_trans(root);
9543 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009544 ret = btrfs_insert_inode_ref(trans, dest,
9545 new_dentry->d_name.name,
9546 new_dentry->d_name.len,
9547 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009548 btrfs_ino(BTRFS_I(new_dir)),
9549 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009550 if (ret)
9551 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009552 }
9553
9554 /* And now for the dest. */
9555 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9556 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009557 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009558 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009559 btrfs_pin_log_trans(dest);
9560 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009561 ret = btrfs_insert_inode_ref(trans, root,
9562 old_dentry->d_name.name,
9563 old_dentry->d_name.len,
9564 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009565 btrfs_ino(BTRFS_I(old_dir)),
9566 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567 if (ret)
9568 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 }
9570
9571 /* Update inode version and ctime/mtime. */
9572 inode_inc_iversion(old_dir);
9573 inode_inc_iversion(new_dir);
9574 inode_inc_iversion(old_inode);
9575 inode_inc_iversion(new_inode);
9576 old_dir->i_ctime = old_dir->i_mtime = ctime;
9577 new_dir->i_ctime = new_dir->i_mtime = ctime;
9578 old_inode->i_ctime = ctime;
9579 new_inode->i_ctime = ctime;
9580
9581 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009582 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9583 BTRFS_I(old_inode), 1);
9584 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9585 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009586 }
9587
9588 /* src is a subvolume */
9589 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9590 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9591 ret = btrfs_unlink_subvol(trans, root, old_dir,
9592 root_objectid,
9593 old_dentry->d_name.name,
9594 old_dentry->d_name.len);
9595 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009596 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9597 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598 old_dentry->d_name.name,
9599 old_dentry->d_name.len);
9600 if (!ret)
9601 ret = btrfs_update_inode(trans, root, old_inode);
9602 }
9603 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009604 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009605 goto out_fail;
9606 }
9607
9608 /* dest is a subvolume */
9609 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9610 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9611 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9612 root_objectid,
9613 new_dentry->d_name.name,
9614 new_dentry->d_name.len);
9615 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009616 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9617 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618 new_dentry->d_name.name,
9619 new_dentry->d_name.len);
9620 if (!ret)
9621 ret = btrfs_update_inode(trans, dest, new_inode);
9622 }
9623 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009624 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009625 goto out_fail;
9626 }
9627
9628 ret = btrfs_add_link(trans, new_dir, old_inode,
9629 new_dentry->d_name.name,
9630 new_dentry->d_name.len, 0, old_idx);
9631 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009632 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009633 goto out_fail;
9634 }
9635
9636 ret = btrfs_add_link(trans, old_dir, new_inode,
9637 old_dentry->d_name.name,
9638 old_dentry->d_name.len, 0, new_idx);
9639 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009640 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009641 goto out_fail;
9642 }
9643
9644 if (old_inode->i_nlink == 1)
9645 BTRFS_I(old_inode)->dir_index = old_idx;
9646 if (new_inode->i_nlink == 1)
9647 BTRFS_I(new_inode)->dir_index = new_idx;
9648
Filipe Manana86e8aa02016-05-05 02:02:27 +01009649 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009651 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9652 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009653 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009654 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009655 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009656 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009658 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9659 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009660 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009661 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009662 }
9663out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009664 /*
9665 * If we have pinned a log and an error happened, we unpin tasks
9666 * trying to sync the log and force them to fallback to a transaction
9667 * commit if the log currently contains any of the inodes involved in
9668 * this rename operation (to ensure we do not persist a log with an
9669 * inconsistent state for any of these inodes or leading to any
9670 * inconsistencies when replayed). If the transaction was aborted, the
9671 * abortion reason is propagated to userspace when attempting to commit
9672 * the transaction. If the log does not contain any of these inodes, we
9673 * allow the tasks to sync it.
9674 */
9675 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009676 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9677 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9678 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009679 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009680 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009681 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009682
9683 if (root_log_pinned) {
9684 btrfs_end_log_trans(root);
9685 root_log_pinned = false;
9686 }
9687 if (dest_log_pinned) {
9688 btrfs_end_log_trans(dest);
9689 dest_log_pinned = false;
9690 }
9691 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009692 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009693out_notrans:
9694 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009695 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009696 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009697 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698
9699 return ret;
9700}
9701
9702static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9703 struct btrfs_root *root,
9704 struct inode *dir,
9705 struct dentry *dentry)
9706{
9707 int ret;
9708 struct inode *inode;
9709 u64 objectid;
9710 u64 index;
9711
9712 ret = btrfs_find_free_ino(root, &objectid);
9713 if (ret)
9714 return ret;
9715
9716 inode = btrfs_new_inode(trans, root, dir,
9717 dentry->d_name.name,
9718 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009719 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009720 objectid,
9721 S_IFCHR | WHITEOUT_MODE,
9722 &index);
9723
9724 if (IS_ERR(inode)) {
9725 ret = PTR_ERR(inode);
9726 return ret;
9727 }
9728
9729 inode->i_op = &btrfs_special_inode_operations;
9730 init_special_inode(inode, inode->i_mode,
9731 WHITEOUT_DEV);
9732
9733 ret = btrfs_init_inode_security(trans, inode, dir,
9734 &dentry->d_name);
9735 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009736 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009737
9738 ret = btrfs_add_nondir(trans, dir, dentry,
9739 inode, 0, index);
9740 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009741 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009742
9743 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009744out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009745 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009746 if (ret)
9747 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009748 iput(inode);
9749
Filipe Mananac9901612016-05-05 01:41:57 +01009750 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009751}
9752
Chris Mason39279cc2007-06-12 06:35:45 -04009753static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009754 struct inode *new_dir, struct dentry *new_dentry,
9755 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009756{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009757 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009758 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009759 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009760 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9761 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009762 struct inode *new_inode = d_inode(new_dentry);
9763 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009764 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009765 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009766 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009767 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009768 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009769
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009770 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009771 return -EPERM;
9772
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009773 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009774 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009775 return -EXDEV;
9776
Li Zefan33345d012011-04-20 10:31:50 +08009777 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009778 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009779 return -ENOTEMPTY;
9780
Chris Mason39279cc2007-06-12 06:35:45 -04009781 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009782 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009783 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009784
9785
9786 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009787 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009788 new_dentry->d_name.name,
9789 new_dentry->d_name.len);
9790
9791 if (ret) {
9792 if (ret == -EEXIST) {
9793 /* we shouldn't get
9794 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309795 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009796 return ret;
9797 }
9798 } else {
9799 /* maybe -EOVERFLOW */
9800 return ret;
9801 }
9802 }
9803 ret = 0;
9804
Chris Mason5a3f23d2009-03-31 13:27:11 -04009805 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009806 * we're using rename to replace one file with another. Start IO on it
9807 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009808 */
Chris Mason8d875f92014-08-12 10:47:42 -07009809 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009810 filemap_flush(old_inode->i_mapping);
9811
Yan, Zheng76dda932009-09-21 16:00:26 -04009812 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009813 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009814 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009815 /*
9816 * We want to reserve the absolute worst case amount of items. So if
9817 * both inodes are subvols and we need to unlink them then that would
9818 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009819 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009820 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9821 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009822 * If our rename has the whiteout flag, we need more 5 units for the
9823 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9824 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009825 */
Filipe Manana5062af32016-05-05 10:26:26 +01009826 trans_num_items = 11;
9827 if (flags & RENAME_WHITEOUT)
9828 trans_num_items += 5;
9829 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009830 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009831 ret = PTR_ERR(trans);
9832 goto out_notrans;
9833 }
Chris Mason5f39d392007-10-15 16:14:19 -04009834
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009835 if (dest != root)
9836 btrfs_record_root_in_trans(trans, dest);
9837
Yan, Zhenga5719522009-09-24 09:17:31 -04009838 ret = btrfs_set_inode_index(new_dir, &index);
9839 if (ret)
9840 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009841
Miao Xie67de1172013-12-26 13:07:06 +08009842 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009843 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009844 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009845 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009846 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009847 btrfs_pin_log_trans(root);
9848 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009849 ret = btrfs_insert_inode_ref(trans, dest,
9850 new_dentry->d_name.name,
9851 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009852 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009853 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009854 if (ret)
9855 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009856 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009857
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009858 inode_inc_iversion(old_dir);
9859 inode_inc_iversion(new_dir);
9860 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009861 old_dir->i_ctime = old_dir->i_mtime =
9862 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009863 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009864
Chris Mason12fcfd22009-03-24 10:24:20 -04009865 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009866 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9867 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009868
Li Zefan33345d012011-04-20 10:31:50 +08009869 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009870 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9871 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9872 old_dentry->d_name.name,
9873 old_dentry->d_name.len);
9874 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009875 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9876 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009877 old_dentry->d_name.name,
9878 old_dentry->d_name.len);
9879 if (!ret)
9880 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009881 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009882 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009883 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009884 goto out_fail;
9885 }
Chris Mason39279cc2007-06-12 06:35:45 -04009886
9887 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009888 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009889 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009890 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009891 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9892 root_objectid = BTRFS_I(new_inode)->location.objectid;
9893 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9894 root_objectid,
9895 new_dentry->d_name.name,
9896 new_dentry->d_name.len);
9897 BUG_ON(new_inode->i_nlink == 0);
9898 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009899 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9900 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009901 new_dentry->d_name.name,
9902 new_dentry->d_name.len);
9903 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009904 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009905 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009906 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009907 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009908 goto out_fail;
9909 }
Chris Mason39279cc2007-06-12 06:35:45 -04009910 }
Josef Bacikaec74772008-07-24 12:12:38 -04009911
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009912 ret = btrfs_add_link(trans, new_dir, old_inode,
9913 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009914 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009915 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009916 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009917 goto out_fail;
9918 }
Chris Mason39279cc2007-06-12 06:35:45 -04009919
Miao Xie67de1172013-12-26 13:07:06 +08009920 if (old_inode->i_nlink == 1)
9921 BTRFS_I(old_inode)->dir_index = index;
9922
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009923 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009924 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009925
David Sterbaf85b7372017-01-20 14:54:07 +01009926 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9927 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009928 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009929 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009930 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009931
9932 if (flags & RENAME_WHITEOUT) {
9933 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9934 old_dentry);
9935
9936 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009937 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009938 goto out_fail;
9939 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009940 }
Chris Mason39279cc2007-06-12 06:35:45 -04009941out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009942 /*
9943 * If we have pinned the log and an error happened, we unpin tasks
9944 * trying to sync the log and force them to fallback to a transaction
9945 * commit if the log currently contains any of the inodes involved in
9946 * this rename operation (to ensure we do not persist a log with an
9947 * inconsistent state for any of these inodes or leading to any
9948 * inconsistencies when replayed). If the transaction was aborted, the
9949 * abortion reason is propagated to userspace when attempting to commit
9950 * the transaction. If the log does not contain any of these inodes, we
9951 * allow the tasks to sync it.
9952 */
9953 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009954 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9955 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9956 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009957 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009958 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009959 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009960
9961 btrfs_end_log_trans(root);
9962 log_pinned = false;
9963 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009964 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009965out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009966 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009967 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009968
Chris Mason39279cc2007-06-12 06:35:45 -04009969 return ret;
9970}
9971
Miklos Szeredi80ace852014-07-23 15:15:32 +02009972static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9973 struct inode *new_dir, struct dentry *new_dentry,
9974 unsigned int flags)
9975{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009976 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009977 return -EINVAL;
9978
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009979 if (flags & RENAME_EXCHANGE)
9980 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9981 new_dentry);
9982
9983 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009984}
9985
Miao Xie8ccf6f192012-10-25 09:28:04 +00009986static void btrfs_run_delalloc_work(struct btrfs_work *work)
9987{
9988 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009989 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009990
9991 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9992 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009993 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009994 filemap_flush(inode->i_mapping);
9995 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9996 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009997 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009998
9999 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010000 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010001 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010002 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010003 complete(&delalloc_work->completion);
10004}
10005
10006struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010007 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010008{
10009 struct btrfs_delalloc_work *work;
10010
David Sterba100d5702015-12-08 14:39:32 +010010011 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010012 if (!work)
10013 return NULL;
10014
10015 init_completion(&work->completion);
10016 INIT_LIST_HEAD(&work->list);
10017 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010018 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010019 WARN_ON_ONCE(!inode);
10020 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10021 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010022
10023 return work;
10024}
10025
10026void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10027{
10028 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010029 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010030}
10031
Chris Masond352ac62008-09-29 15:18:18 -040010032/*
10033 * some fairly slow code that needs optimization. This walks the list
10034 * of all the inodes with pending delalloc and forces them to disk.
10035 */
Miao Xie6c255e62014-03-06 13:55:01 +080010036static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10037 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010038{
Chris Masonea8c2812008-08-04 23:17:27 -040010039 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010040 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010041 struct btrfs_delalloc_work *work, *next;
10042 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010043 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010044 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010045
Miao Xie8ccf6f192012-10-25 09:28:04 +000010046 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010047 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010048
Miao Xie573bfb72014-03-06 13:55:03 +080010049 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010050 spin_lock(&root->delalloc_lock);
10051 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010052 while (!list_empty(&splice)) {
10053 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010054 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010055
Miao Xieeb73c1b2013-05-15 07:48:22 +000010056 list_move_tail(&binode->delalloc_inodes,
10057 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010058 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010059 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010060 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010061 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010062 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010063 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010064
David Sterba651d4942015-11-27 19:24:16 +010010065 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010066 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010067 if (delay_iput)
10068 btrfs_add_delayed_iput(inode);
10069 else
10070 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010071 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010072 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010073 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010074 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010075 btrfs_queue_work(root->fs_info->flush_workers,
10076 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010077 ret++;
10078 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010079 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010080 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010081 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010082 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010083 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010084
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010085out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010086 list_for_each_entry_safe(work, next, &works, list) {
10087 list_del_init(&work->list);
10088 btrfs_wait_and_free_delalloc_work(work);
10089 }
10090
Miao Xieeb73c1b2013-05-15 07:48:22 +000010091 if (!list_empty_careful(&splice)) {
10092 spin_lock(&root->delalloc_lock);
10093 list_splice_tail(&splice, &root->delalloc_inodes);
10094 spin_unlock(&root->delalloc_lock);
10095 }
Miao Xie573bfb72014-03-06 13:55:03 +080010096 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010097 return ret;
10098}
10099
10100int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10101{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010102 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010103 int ret;
10104
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010105 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010106 return -EROFS;
10107
Miao Xie6c255e62014-03-06 13:55:01 +080010108 ret = __start_delalloc_inodes(root, delay_iput, -1);
10109 if (ret > 0)
10110 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010111 /*
10112 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010113 * we have to make sure the IO is actually started and that
10114 * ordered extents get created before we return
10115 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010116 atomic_inc(&fs_info->async_submit_draining);
10117 while (atomic_read(&fs_info->nr_async_submits) ||
10118 atomic_read(&fs_info->async_delalloc_pages)) {
10119 wait_event(fs_info->async_submit_wait,
10120 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10121 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010122 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010123 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010124 return ret;
10125}
10126
Miao Xie6c255e62014-03-06 13:55:01 +080010127int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10128 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010129{
10130 struct btrfs_root *root;
10131 struct list_head splice;
10132 int ret;
10133
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010134 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010135 return -EROFS;
10136
10137 INIT_LIST_HEAD(&splice);
10138
Miao Xie573bfb72014-03-06 13:55:03 +080010139 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010140 spin_lock(&fs_info->delalloc_root_lock);
10141 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010142 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010143 root = list_first_entry(&splice, struct btrfs_root,
10144 delalloc_root);
10145 root = btrfs_grab_fs_root(root);
10146 BUG_ON(!root);
10147 list_move_tail(&root->delalloc_root,
10148 &fs_info->delalloc_roots);
10149 spin_unlock(&fs_info->delalloc_root_lock);
10150
Miao Xie6c255e62014-03-06 13:55:01 +080010151 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010152 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010153 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010154 goto out;
10155
Miao Xie6c255e62014-03-06 13:55:01 +080010156 if (nr != -1) {
10157 nr -= ret;
10158 WARN_ON(nr < 0);
10159 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010160 spin_lock(&fs_info->delalloc_root_lock);
10161 }
10162 spin_unlock(&fs_info->delalloc_root_lock);
10163
Miao Xie6c255e62014-03-06 13:55:01 +080010164 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010165 atomic_inc(&fs_info->async_submit_draining);
10166 while (atomic_read(&fs_info->nr_async_submits) ||
10167 atomic_read(&fs_info->async_delalloc_pages)) {
10168 wait_event(fs_info->async_submit_wait,
10169 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10170 atomic_read(&fs_info->async_delalloc_pages) == 0));
10171 }
10172 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010173out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010174 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010175 spin_lock(&fs_info->delalloc_root_lock);
10176 list_splice_tail(&splice, &fs_info->delalloc_roots);
10177 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010178 }
Miao Xie573bfb72014-03-06 13:55:03 +080010179 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010180 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010181}
10182
Chris Mason39279cc2007-06-12 06:35:45 -040010183static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10184 const char *symname)
10185{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010186 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010187 struct btrfs_trans_handle *trans;
10188 struct btrfs_root *root = BTRFS_I(dir)->root;
10189 struct btrfs_path *path;
10190 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010191 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010192 int err;
10193 int drop_inode = 0;
10194 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010195 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010196 int name_len;
10197 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010198 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010199 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010200 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010201
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010202 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010203 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010204 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010205
Josef Bacik9ed74f22009-09-11 16:12:44 -040010206 /*
10207 * 2 items for inode item and ref
10208 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010209 * 1 item for updating parent inode item
10210 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010211 * 1 item for xattr if selinux is on
10212 */
Filipe Manana9269d122015-12-31 18:16:29 +000010213 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010214 if (IS_ERR(trans))
10215 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010216
Li Zefan581bb052011-04-20 10:06:11 +080010217 err = btrfs_find_free_ino(root, &objectid);
10218 if (err)
10219 goto out_unlock;
10220
Josef Bacikaec74772008-07-24 12:12:38 -040010221 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010222 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10223 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010224 if (IS_ERR(inode)) {
10225 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010226 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010227 }
Chris Mason39279cc2007-06-12 06:35:45 -040010228
Casey Schauflerad19db72011-12-15 10:09:07 -050010229 /*
10230 * If the active LSM wants to access the inode during
10231 * d_instantiate it needs these. Smack checks to see
10232 * if the filesystem supports xattrs by looking at the
10233 * ops vector.
10234 */
10235 inode->i_fop = &btrfs_file_operations;
10236 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010237 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010238 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10239
10240 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10241 if (err)
10242 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010243
Chris Mason39279cc2007-06-12 06:35:45 -040010244 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010245 if (!path) {
10246 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010247 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010248 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010249 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010250 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010251 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010252 datasize = btrfs_file_extent_calc_inline_size(name_len);
10253 err = btrfs_insert_empty_item(trans, root, path, &key,
10254 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010255 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010256 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010257 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010258 }
Chris Mason5f39d392007-10-15 16:14:19 -040010259 leaf = path->nodes[0];
10260 ei = btrfs_item_ptr(leaf, path->slots[0],
10261 struct btrfs_file_extent_item);
10262 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10263 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010264 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010265 btrfs_set_file_extent_encryption(leaf, ei, 0);
10266 btrfs_set_file_extent_compression(leaf, ei, 0);
10267 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10268 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10269
Chris Mason39279cc2007-06-12 06:35:45 -040010270 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010271 write_extent_buffer(leaf, symname, ptr, name_len);
10272 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010273 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010274
Chris Mason39279cc2007-06-12 06:35:45 -040010275 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010276 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010277 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010278 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010279 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010280 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010281 /*
10282 * Last step, add directory indexes for our symlink inode. This is the
10283 * last step to avoid extra cleanup of these indexes if an error happens
10284 * elsewhere above.
10285 */
10286 if (!err)
10287 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010288 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010289 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010290 goto out_unlock_inode;
10291 }
10292
10293 unlock_new_inode(inode);
10294 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010295
10296out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010297 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010298 if (drop_inode) {
10299 inode_dec_link_count(inode);
10300 iput(inode);
10301 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010302 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010303 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010304
10305out_unlock_inode:
10306 drop_inode = 1;
10307 unlock_new_inode(inode);
10308 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010309}
Chris Mason16432982008-04-10 10:23:21 -040010310
Josef Bacik0af3d002010-06-21 14:48:16 -040010311static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10312 u64 start, u64 num_bytes, u64 min_size,
10313 loff_t actual_len, u64 *alloc_hint,
10314 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010315{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010316 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010317 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10318 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010319 struct btrfs_root *root = BTRFS_I(inode)->root;
10320 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010321 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010322 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010323 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010324 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010325 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010326 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010327 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010328
Josef Bacik0af3d002010-06-21 14:48:16 -040010329 if (trans)
10330 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010331 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010332 if (own_trans) {
10333 trans = btrfs_start_transaction(root, 3);
10334 if (IS_ERR(trans)) {
10335 ret = PTR_ERR(trans);
10336 break;
10337 }
Yan Zhengd899e052008-10-30 14:25:28 -040010338 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010339
Byongho Leeee221842015-12-15 01:42:10 +090010340 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010341 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010342 /*
10343 * If we are severely fragmented we could end up with really
10344 * small allocations, so if the allocator is returning small
10345 * chunks lets make its job easier by only searching for those
10346 * sized chunks.
10347 */
10348 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010349 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10350 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010351 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010352 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010353 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010354 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010355 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010356 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010357
Josef Bacik0b670dc2015-09-23 17:11:16 -040010358 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010359 ret = insert_reserved_file_extent(trans, inode,
10360 cur_offset, ins.objectid,
10361 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010362 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010363 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010364 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010365 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010366 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010367 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010368 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010369 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010370 break;
10371 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010372
Chris Masona1ed8352009-09-11 12:27:37 -040010373 btrfs_drop_extent_cache(inode, cur_offset,
10374 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010375
Josef Bacik5dc562c2012-08-17 13:14:17 -040010376 em = alloc_extent_map();
10377 if (!em) {
10378 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10379 &BTRFS_I(inode)->runtime_flags);
10380 goto next;
10381 }
10382
10383 em->start = cur_offset;
10384 em->orig_start = cur_offset;
10385 em->len = ins.offset;
10386 em->block_start = ins.objectid;
10387 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010388 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010389 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010390 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010391 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10392 em->generation = trans->transid;
10393
10394 while (1) {
10395 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010396 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010397 write_unlock(&em_tree->lock);
10398 if (ret != -EEXIST)
10399 break;
10400 btrfs_drop_extent_cache(inode, cur_offset,
10401 cur_offset + ins.offset - 1,
10402 0);
10403 }
10404 free_extent_map(em);
10405next:
Yan Zhengd899e052008-10-30 14:25:28 -040010406 num_bytes -= ins.offset;
10407 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010408 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010409
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010410 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010411 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010412 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010413 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010414 (actual_len > inode->i_size) &&
10415 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010416 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010417 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010418 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010419 i_size = cur_offset;
10420 i_size_write(inode, i_size);
10421 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010422 }
10423
Yan Zhengd899e052008-10-30 14:25:28 -040010424 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010425
10426 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010427 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010428 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010429 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010430 break;
10431 }
Yan Zhengd899e052008-10-30 14:25:28 -040010432
Josef Bacik0af3d002010-06-21 14:48:16 -040010433 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010434 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010435 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010436 if (cur_offset < end)
10437 btrfs_free_reserved_data_space(inode, cur_offset,
10438 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010439 return ret;
10440}
10441
Josef Bacik0af3d002010-06-21 14:48:16 -040010442int btrfs_prealloc_file_range(struct inode *inode, int mode,
10443 u64 start, u64 num_bytes, u64 min_size,
10444 loff_t actual_len, u64 *alloc_hint)
10445{
10446 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10447 min_size, actual_len, alloc_hint,
10448 NULL);
10449}
10450
10451int btrfs_prealloc_file_range_trans(struct inode *inode,
10452 struct btrfs_trans_handle *trans, int mode,
10453 u64 start, u64 num_bytes, u64 min_size,
10454 loff_t actual_len, u64 *alloc_hint)
10455{
10456 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10457 min_size, actual_len, alloc_hint, trans);
10458}
10459
Chris Masone6dcd2d2008-07-17 12:53:50 -040010460static int btrfs_set_page_dirty(struct page *page)
10461{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010462 return __set_page_dirty_nobuffers(page);
10463}
10464
Al Viro10556cb2011-06-20 19:28:19 -040010465static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010466{
Li Zefanb83cc962010-12-20 16:04:08 +080010467 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010468 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010469
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010470 if (mask & MAY_WRITE &&
10471 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10472 if (btrfs_root_readonly(root))
10473 return -EROFS;
10474 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10475 return -EACCES;
10476 }
Al Viro2830ba72011-06-20 19:16:29 -040010477 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010478}
Chris Mason39279cc2007-06-12 06:35:45 -040010479
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010480static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10481{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010482 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010483 struct btrfs_trans_handle *trans;
10484 struct btrfs_root *root = BTRFS_I(dir)->root;
10485 struct inode *inode = NULL;
10486 u64 objectid;
10487 u64 index;
10488 int ret = 0;
10489
10490 /*
10491 * 5 units required for adding orphan entry
10492 */
10493 trans = btrfs_start_transaction(root, 5);
10494 if (IS_ERR(trans))
10495 return PTR_ERR(trans);
10496
10497 ret = btrfs_find_free_ino(root, &objectid);
10498 if (ret)
10499 goto out;
10500
10501 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010502 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010503 if (IS_ERR(inode)) {
10504 ret = PTR_ERR(inode);
10505 inode = NULL;
10506 goto out;
10507 }
10508
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010509 inode->i_fop = &btrfs_file_operations;
10510 inode->i_op = &btrfs_file_inode_operations;
10511
10512 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010513 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10514
Chris Masonb0d5d102014-09-08 13:08:51 -070010515 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10516 if (ret)
10517 goto out_inode;
10518
10519 ret = btrfs_update_inode(trans, root, inode);
10520 if (ret)
10521 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010522 ret = btrfs_orphan_add(trans, inode);
10523 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010524 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010525
Filipe Manana5762b5c2014-08-01 00:10:32 +010010526 /*
10527 * We set number of links to 0 in btrfs_new_inode(), and here we set
10528 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10529 * through:
10530 *
10531 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10532 */
10533 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010534 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010535 d_tmpfile(dentry, inode);
10536 mark_inode_dirty(inode);
10537
10538out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010539 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010540 if (ret)
10541 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010542 btrfs_balance_delayed_items(fs_info);
10543 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010544 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010545
10546out_inode:
10547 unlock_new_inode(inode);
10548 goto out;
10549
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010550}
10551
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010552static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010553 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010554 .lookup = btrfs_lookup,
10555 .create = btrfs_create,
10556 .unlink = btrfs_unlink,
10557 .link = btrfs_link,
10558 .mkdir = btrfs_mkdir,
10559 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010560 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010561 .symlink = btrfs_symlink,
10562 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010563 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010564 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010565 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010566 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010567 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010568 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010569 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010570};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010571static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010572 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010573 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010574 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010575};
Yan, Zheng76dda932009-09-21 16:00:26 -040010576
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010577static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010578 .llseek = generic_file_llseek,
10579 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010580 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010581 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010582#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010583 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010584#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010585 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010586 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010587};
10588
David Sterba20e55062015-11-19 11:42:28 +010010589static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010590 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010591 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010592 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010593 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010594 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010595 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010596 .set_bit_hook = btrfs_set_bit_hook,
10597 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010598 .merge_extent_hook = btrfs_merge_extent_hook,
10599 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010600};
10601
Chris Mason35054392009-01-21 13:11:13 -050010602/*
10603 * btrfs doesn't support the bmap operation because swapfiles
10604 * use bmap to make a mapping of extents in the file. They assume
10605 * these extents won't change over the life of the file and they
10606 * use the bmap result to do IO directly to the drive.
10607 *
10608 * the btrfs bmap call would return logical addresses that aren't
10609 * suitable for IO and they also will change frequently as COW
10610 * operations happen. So, swapfile + btrfs == corruption.
10611 *
10612 * For now we're avoiding this by dropping bmap.
10613 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010614static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010615 .readpage = btrfs_readpage,
10616 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010617 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010618 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010619 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010620 .invalidatepage = btrfs_invalidatepage,
10621 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010622 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010623 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010624};
10625
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010626static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010627 .readpage = btrfs_readpage,
10628 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010629 .invalidatepage = btrfs_invalidatepage,
10630 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010631};
10632
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010633static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010634 .getattr = btrfs_getattr,
10635 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010636 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010637 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010638 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010639 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010640 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010641 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010642};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010643static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010644 .getattr = btrfs_getattr,
10645 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010646 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010647 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010648 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010649 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010650 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010651};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010652static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010653 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010654 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010655 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010656 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010657 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010658 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010659};
Yan, Zheng76dda932009-09-21 16:00:26 -040010660
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010661const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010662 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010663 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010664};