blob: 4e9407a014d9a49de3868711586d58325f4b4816 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800112static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Eric Sandeen48a3b632013-04-25 20:41:01 +0000118static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400119
Josef Bacik6a3891c2015-03-16 17:38:52 -0400120#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121void btrfs_test_inode_set_ops(struct inode *inode)
122{
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124}
125#endif
126
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000127static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500130{
131 int err;
132
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000133 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500136 return err;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/*
Chris Masonc8b97812008-10-29 14:49:59 -0400140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
143 */
Chris Mason40f76582014-05-21 13:35:51 -0700144static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000145 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000148 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct page **compressed_pages)
150{
Chris Masonc8b97812008-10-29 14:49:59 -0400151 struct extent_buffer *leaf;
152 struct page *page = NULL;
153 char *kaddr;
154 unsigned long ptr;
155 struct btrfs_file_extent_item *ei;
156 int err = 0;
157 int ret;
158 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400160
Li Zefanfe3f5662011-03-28 08:30:38 +0000161 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400163
Chris Masonc8b97812008-10-29 14:49:59 -0400164 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 if (!extent_inserted) {
167 struct btrfs_key key;
168 size_t datasize;
169
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200170 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000171 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200172 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000173
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 datasize);
178 if (ret) {
179 err = ret;
180 goto fail;
181 }
Chris Masonc8b97812008-10-29 14:49:59 -0400182 }
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
192
Li Zefan261507a02010-12-17 14:21:50 +0800193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 struct page *cpage;
195 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500196 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500198 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400200
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204
205 i++;
206 ptr += cur_size;
207 compressed_size -= cur_size;
208 }
209 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800210 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 } else {
212 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800215 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400220 }
221 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000222 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Yan, Zhengc2167752009-11-12 09:34:21 +0000224 /*
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
228 *
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
232 */
Chris Masonc8b97812008-10-29 14:49:59 -0400233 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000235
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100236 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400237fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400238 return err;
239}
240
241
242/*
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
246 */
Josef Bacik00361582013-08-14 14:02:47 -0400247static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
250 int compress_type,
251 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400253 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400254 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400255 u64 isize = i_size_read(inode);
256 u64 actual_end = min(end + 1, isize);
257 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400258 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400259 u64 data_len = inline_len;
260 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 struct btrfs_path *path;
262 int extent_inserted = 0;
263 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400264
265 if (compressed_size)
266 data_len = compressed_size;
267
268 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400269 actual_end > fs_info->sectorsize ||
270 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400271 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400272 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400273 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400274 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400275 return 1;
276 }
277
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000278 path = btrfs_alloc_path();
279 if (!path)
280 return -ENOMEM;
281
Josef Bacik00361582013-08-14 14:02:47 -0400282 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000283 if (IS_ERR(trans)) {
284 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400285 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000286 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400287 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400288
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 if (compressed_size && compressed_pages)
290 extent_item_size = btrfs_file_extent_calc_inline_size(
291 compressed_size);
292 else
293 extent_item_size = btrfs_file_extent_calc_inline_size(
294 inline_len);
295
296 ret = __btrfs_drop_extents(trans, root, inode, path,
297 start, aligned_end, NULL,
298 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400299 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400300 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400301 goto out;
302 }
Chris Masonc8b97812008-10-29 14:49:59 -0400303
304 if (isize > actual_end)
305 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000306 ret = insert_inline_extent(trans, path, extent_inserted,
307 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400308 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000309 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400310 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400311 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400312 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400313 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400314 ret = 1;
315 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100316 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400317
Josef Bacikbdc20e62013-02-28 13:23:38 -0500318 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
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;
Chris Mason771ed682008-11-06 22:02:51 -0500700 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500701 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500702
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500703again:
Chris Masond3977122009-01-05 21:25:51 -0500704 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500705 async_extent = list_entry(async_cow->extents.next,
706 struct async_extent, list);
707 list_del(&async_extent->list);
708
709 io_tree = &BTRFS_I(inode)->io_tree;
710
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500711retry:
Chris Mason771ed682008-11-06 22:02:51 -0500712 /* did the compression code fall back to uncompressed IO? */
713 if (!async_extent->pages) {
714 int page_started = 0;
715 unsigned long nr_written = 0;
716
717 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000718 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100719 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500720
721 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500722 ret = cow_file_range(inode, async_cow->locked_page,
723 async_extent->start,
724 async_extent->start +
725 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800726 async_extent->start +
727 async_extent->ram_size - 1,
728 &page_started, &nr_written, 0,
729 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500730
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100731 /* JDM XXX */
732
Chris Mason771ed682008-11-06 22:02:51 -0500733 /*
734 * if page_started, cow_file_range inserted an
735 * inline extent and took care of all the unlocking
736 * and IO for us. Otherwise, we need to submit
737 * all those pages down to the drive.
738 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500740 extent_write_locked_range(io_tree,
741 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500742 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500743 async_extent->ram_size - 1,
744 btrfs_get_extent,
745 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500746 else if (ret)
747 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500748 kfree(async_extent);
749 cond_resched();
750 continue;
751 }
752
753 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100754 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500755
Wang Xiaoguang18513092016-07-25 15:51:40 +0800756 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500757 async_extent->compressed_size,
758 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800759 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500760 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100761 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400763 if (ret == -ENOSPC) {
764 unlock_extent(io_tree, async_extent->start,
765 async_extent->start +
766 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800767
768 /*
769 * we need to redirty the pages if we decide to
770 * fallback to uncompressed IO, otherwise we
771 * will not submit these pages down to lower
772 * layers.
773 */
774 extent_range_redirty_for_io(inode,
775 async_extent->start,
776 async_extent->start +
777 async_extent->ram_size - 1);
778
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100779 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400780 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500782 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000783 /*
784 * here we're doing allocation and writeback of the
785 * compressed pages
786 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800787 em = create_io_em(inode, async_extent->start,
788 async_extent->ram_size, /* len */
789 async_extent->start, /* orig_start */
790 ins.objectid, /* block_start */
791 ins.offset, /* block_len */
792 ins.offset, /* orig_block_len */
793 async_extent->ram_size, /* ram_bytes */
794 async_extent->compress_type,
795 BTRFS_ORDERED_COMPRESSED);
796 if (IS_ERR(em))
797 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800799 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500800
Li Zefan261507a02010-12-17 14:21:50 +0800801 ret = btrfs_add_ordered_extent_compress(inode,
802 async_extent->start,
803 ins.objectid,
804 async_extent->ram_size,
805 ins.offset,
806 BTRFS_ORDERED_COMPRESSED,
807 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100808 if (ret) {
809 btrfs_drop_extent_cache(inode, async_extent->start,
810 async_extent->start +
811 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100813 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400814 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500815
Chris Mason771ed682008-11-06 22:02:51 -0500816 /*
817 * clear dirty, set writeback and unlock the pages.
818 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400819 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400820 async_extent->start +
821 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800822 async_extent->start +
823 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400824 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
825 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400826 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500827 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500828 async_extent->start,
829 async_extent->ram_size,
830 ins.objectid,
831 ins.offset, async_extent->pages,
832 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100833 if (ret) {
834 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
835 struct page *p = async_extent->pages[0];
836 const u64 start = async_extent->start;
837 const u64 end = start + async_extent->ram_size - 1;
838
839 p->mapping = inode->i_mapping;
840 tree->ops->writepage_end_io_hook(p, start, end,
841 NULL, 0);
842 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800843 extent_clear_unlock_delalloc(inode, start, end, end,
844 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100845 PAGE_END_WRITEBACK |
846 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100847 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100848 }
Chris Mason771ed682008-11-06 22:02:51 -0500849 alloc_hint = ins.objectid + ins.offset;
850 kfree(async_extent);
851 cond_resched();
852 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100853 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500854out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400855 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400856 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100857out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400858 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500859 async_extent->start +
860 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800861 async_extent->start +
862 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400863 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400864 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
865 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100866 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
867 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100868 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100869 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500870 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500871}
872
Josef Bacik4b46fce2010-05-23 11:00:55 -0400873static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
874 u64 num_bytes)
875{
876 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
877 struct extent_map *em;
878 u64 alloc_hint = 0;
879
880 read_lock(&em_tree->lock);
881 em = search_extent_mapping(em_tree, start, num_bytes);
882 if (em) {
883 /*
884 * if block start isn't an actual block number then find the
885 * first block in this inode and use that as a hint. If that
886 * block is also bogus then just don't worry about it.
887 */
888 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
889 free_extent_map(em);
890 em = search_extent_mapping(em_tree, 0, 0);
891 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
892 alloc_hint = em->block_start;
893 if (em)
894 free_extent_map(em);
895 } else {
896 alloc_hint = em->block_start;
897 free_extent_map(em);
898 }
899 }
900 read_unlock(&em_tree->lock);
901
902 return alloc_hint;
903}
904
Chris Mason771ed682008-11-06 22:02:51 -0500905/*
906 * when extent_io.c finds a delayed allocation range in the file,
907 * the call backs end up in this code. The basic idea is to
908 * allocate extents on disk for the range, and create ordered data structs
909 * in ram to track those extents.
910 *
911 * locked_page is the page that writepage had locked already. We use
912 * it to make sure we don't do extra locks or unlocks.
913 *
914 * *page_started is set to one if we unlock locked_page and do everything
915 * required to start IO on it. It may be clean and already done with
916 * IO when we return.
917 */
Josef Bacik00361582013-08-14 14:02:47 -0400918static noinline int cow_file_range(struct inode *inode,
919 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800920 u64 start, u64 end, u64 delalloc_end,
921 int *page_started, unsigned long *nr_written,
922 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500923{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400924 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400925 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500926 u64 alloc_hint = 0;
927 u64 num_bytes;
928 unsigned long ram_size;
929 u64 disk_num_bytes;
930 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400931 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500932 struct btrfs_key ins;
933 struct extent_map *em;
Chris Mason771ed682008-11-06 22:02:51 -0500934 int ret = 0;
935
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400936 if (btrfs_is_free_space_inode(inode)) {
937 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500938 ret = -EINVAL;
939 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400940 }
Chris Mason771ed682008-11-06 22:02:51 -0500941
Qu Wenruofda28322013-02-26 08:10:22 +0000942 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500943 num_bytes = max(blocksize, num_bytes);
944 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500945
Anand Jain26d30f82016-12-19 19:09:06 +0800946 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400947
Chris Mason771ed682008-11-06 22:02:51 -0500948 if (start == 0) {
949 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800950 ret = cow_file_range_inline(root, inode, start, end, 0,
951 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500952 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800953 extent_clear_unlock_delalloc(inode, start, end,
954 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400955 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400956 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400957 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
958 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800959 btrfs_free_reserved_data_space_noquota(inode, start,
960 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500961 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300962 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500963 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500964 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100965 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500967 }
968 }
Chris Masonc8b97812008-10-29 14:49:59 -0400969
970 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400971 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400972
Josef Bacik4b46fce2010-05-23 11:00:55 -0400973 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400974 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400975
Chris Masond3977122009-01-05 21:25:51 -0500976 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400977 unsigned long op;
978
Josef Bacik287a0ab2010-03-19 18:07:23 +0000979 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800980 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400981 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800982 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400983 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100984 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500985
Chris Mason771ed682008-11-06 22:02:51 -0500986 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -0800987 em = create_io_em(inode, start, ins.offset, /* len */
988 start, /* orig_start */
989 ins.objectid, /* block_start */
990 ins.offset, /* block_len */
991 ins.offset, /* orig_block_len */
992 ram_size, /* ram_bytes */
993 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -0800994 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -0800995 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +0000996 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800997 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400998
Chris Mason98d20f62008-04-14 09:46:10 -0400999 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001000 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001001 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001002 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001003 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001004
Yan Zheng17d217f2008-12-12 10:03:38 -05001005 if (root->root_key.objectid ==
1006 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1007 ret = btrfs_reloc_clone_csums(inode, start,
1008 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001009 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001010 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001011 }
1012
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001013 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001014
Chris Masond3977122009-01-05 21:25:51 -05001015 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001016 break;
Chris Masond3977122009-01-05 21:25:51 -05001017
Chris Masonc8b97812008-10-29 14:49:59 -04001018 /* we're not doing compressed IO, don't unlock the first
1019 * page (which the caller expects to stay locked), don't
1020 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001021 *
1022 * Do set the Private2 bit so we know this page was properly
1023 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001024 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001025 op = unlock ? PAGE_UNLOCK : 0;
1026 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001027
Josef Bacikc2790a22013-07-29 11:20:47 -04001028 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001029 start + ram_size - 1,
1030 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001031 EXTENT_LOCKED | EXTENT_DELALLOC,
1032 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001033 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001034 num_bytes -= cur_alloc_size;
1035 alloc_hint = ins.objectid + ins.offset;
1036 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -04001037 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001038out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001039 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001040
Filipe Mananad9f85962014-08-25 10:43:00 +01001041out_drop_extent_cache:
1042 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001043out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001044 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001045 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001046out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001047 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1048 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001049 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1050 EXTENT_DELALLOC | EXTENT_DEFRAG,
1051 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1052 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001053 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001054}
Chris Masonc8b97812008-10-29 14:49:59 -04001055
Chris Mason771ed682008-11-06 22:02:51 -05001056/*
1057 * work queue call back to started compression on a file and pages
1058 */
1059static noinline void async_cow_start(struct btrfs_work *work)
1060{
1061 struct async_cow *async_cow;
1062 int num_added = 0;
1063 async_cow = container_of(work, struct async_cow, work);
1064
1065 compress_file_range(async_cow->inode, async_cow->locked_page,
1066 async_cow->start, async_cow->end, async_cow,
1067 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001068 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001069 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001070 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001071 }
Chris Mason771ed682008-11-06 22:02:51 -05001072}
1073
1074/*
1075 * work queue call back to submit previously compressed pages
1076 */
1077static noinline void async_cow_submit(struct btrfs_work *work)
1078{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001079 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001080 struct async_cow *async_cow;
1081 struct btrfs_root *root;
1082 unsigned long nr_pages;
1083
1084 async_cow = container_of(work, struct async_cow, work);
1085
1086 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001087 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001088 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1089 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001090
David Sterbaee863952015-02-16 19:41:40 +01001091 /*
1092 * atomic_sub_return implies a barrier for waitqueue_active
1093 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001094 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001095 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001096 waitqueue_active(&fs_info->async_submit_wait))
1097 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001098
Chris Masond3977122009-01-05 21:25:51 -05001099 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001100 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001101}
Chris Masonc8b97812008-10-29 14:49:59 -04001102
Chris Mason771ed682008-11-06 22:02:51 -05001103static noinline void async_cow_free(struct btrfs_work *work)
1104{
1105 struct async_cow *async_cow;
1106 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001107 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001108 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001109 kfree(async_cow);
1110}
1111
1112static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1113 u64 start, u64 end, int *page_started,
1114 unsigned long *nr_written)
1115{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001116 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001117 struct async_cow *async_cow;
1118 struct btrfs_root *root = BTRFS_I(inode)->root;
1119 unsigned long nr_pages;
1120 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001121
Chris Masona3429ab2009-10-08 12:30:20 -04001122 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1123 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001124 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001125 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001126 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001127 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001128 async_cow->root = root;
1129 async_cow->locked_page = locked_page;
1130 async_cow->start = start;
1131
Wang Shilongf79707b2014-07-17 11:44:09 +08001132 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001133 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001134 cur_end = end;
1135 else
Byongho Leeee221842015-12-15 01:42:10 +09001136 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001137
1138 async_cow->end = cur_end;
1139 INIT_LIST_HEAD(&async_cow->extents);
1140
Liu Bo9e0af232014-08-15 23:36:53 +08001141 btrfs_init_work(&async_cow->work,
1142 btrfs_delalloc_helper,
1143 async_cow_start, async_cow_submit,
1144 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001145
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001146 nr_pages = (cur_end - start + PAGE_SIZE) >>
1147 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001148 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001149
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001150 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001151
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001152 while (atomic_read(&fs_info->async_submit_draining) &&
1153 atomic_read(&fs_info->async_delalloc_pages)) {
1154 wait_event(fs_info->async_submit_wait,
1155 (atomic_read(&fs_info->async_delalloc_pages) ==
1156 0));
Chris Mason771ed682008-11-06 22:02:51 -05001157 }
1158
1159 *nr_written += nr_pages;
1160 start = cur_end + 1;
1161 }
1162 *page_started = 1;
1163 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001164}
1165
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001166static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001167 u64 bytenr, u64 num_bytes)
1168{
1169 int ret;
1170 struct btrfs_ordered_sum *sums;
1171 LIST_HEAD(list);
1172
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001173 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001174 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001175 if (ret == 0 && list_empty(&list))
1176 return 0;
1177
1178 while (!list_empty(&list)) {
1179 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1180 list_del(&sums->list);
1181 kfree(sums);
1182 }
1183 return 1;
1184}
1185
Chris Masond352ac62008-09-29 15:18:18 -04001186/*
1187 * when nowcow writeback call back. This checks for snapshots or COW copies
1188 * of the extents that exist in the file, and COWs the file as required.
1189 *
1190 * If no cow copies or snapshots exist, we write directly to the existing
1191 * blocks on disk
1192 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001193static noinline int run_delalloc_nocow(struct inode *inode,
1194 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001195 u64 start, u64 end, int *page_started, int force,
1196 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001197{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001198 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001199 struct btrfs_root *root = BTRFS_I(inode)->root;
1200 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001201 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001202 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001203 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001204 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001205 u64 cow_start;
1206 u64 cur_offset;
1207 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001208 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 u64 disk_bytenr;
1210 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001211 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001212 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001213 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001214 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001215 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 int nocow;
1217 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001218 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001219 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001220
1221 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001222 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001223 extent_clear_unlock_delalloc(inode, start, end, end,
1224 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001225 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001226 EXTENT_DO_ACCOUNTING |
1227 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001228 PAGE_CLEAR_DIRTY |
1229 PAGE_SET_WRITEBACK |
1230 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001231 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001232 }
Li Zefan82d59022011-04-20 10:33:24 +08001233
Liu Bo83eea1f2012-07-10 05:28:39 -06001234 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001235
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 cow_start = (u64)-1;
1237 cur_offset = start;
1238 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001239 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001241 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001242 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1244 leaf = path->nodes[0];
1245 btrfs_item_key_to_cpu(leaf, &found_key,
1246 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001247 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001248 found_key.type == BTRFS_EXTENT_DATA_KEY)
1249 path->slots[0]--;
1250 }
1251 check_prev = 0;
1252next_slot:
1253 leaf = path->nodes[0];
1254 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1255 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001256 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001257 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 if (ret > 0)
1259 break;
1260 leaf = path->nodes[0];
1261 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001262
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 nocow = 0;
1264 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001265 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001267
Filipe Manana1d512cb2015-11-09 00:33:58 +00001268 if (found_key.objectid > ino)
1269 break;
1270 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1271 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1272 path->slots[0]++;
1273 goto next_slot;
1274 }
1275 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 found_key.offset > end)
1277 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001278
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 if (found_key.offset > cur_offset) {
1280 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001281 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 goto out_check;
1283 }
Chris Masonc31f8832008-01-08 15:46:31 -05001284
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 fi = btrfs_item_ptr(leaf, path->slots[0],
1286 struct btrfs_file_extent_item);
1287 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001288
Josef Bacikcc95bef2013-04-04 14:31:27 -04001289 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001290 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1291 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001293 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 extent_end = found_key.offset +
1295 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001296 disk_num_bytes =
1297 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 if (extent_end <= start) {
1299 path->slots[0]++;
1300 goto next_slot;
1301 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001302 if (disk_bytenr == 0)
1303 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 if (btrfs_file_extent_compression(leaf, fi) ||
1305 btrfs_file_extent_encryption(leaf, fi) ||
1306 btrfs_file_extent_other_encoding(leaf, fi))
1307 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001308 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1309 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001310 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001312 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001313 found_key.offset -
1314 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001315 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001316 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001317 disk_bytenr += cur_offset - found_key.offset;
1318 num_bytes = min(end + 1, extent_end) - cur_offset;
1319 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001320 * if there are pending snapshots for this root,
1321 * we fall into common COW way.
1322 */
1323 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001324 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001325 if (!err)
1326 goto out_check;
1327 }
1328 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001329 * force cow if csum exists in the range.
1330 * this ensure that csum for a given extent are
1331 * either valid or do not exist.
1332 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001333 if (csum_exist_in_range(fs_info, disk_bytenr,
1334 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001335 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001336 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001337 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 nocow = 1;
1339 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1340 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001341 btrfs_file_extent_inline_len(leaf,
1342 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001343 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001344 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 } else {
1346 BUG_ON(1);
1347 }
1348out_check:
1349 if (extent_end <= start) {
1350 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001351 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001352 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001353 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001354 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 goto next_slot;
1356 }
1357 if (!nocow) {
1358 if (cow_start == (u64)-1)
1359 cow_start = cur_offset;
1360 cur_offset = extent_end;
1361 if (cur_offset > end)
1362 break;
1363 path->slots[0]++;
1364 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001365 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001366
David Sterbab3b4aa72011-04-21 01:20:15 +02001367 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001369 ret = cow_file_range(inode, locked_page,
1370 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001371 end, page_started, nr_written, 1,
1372 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001373 if (ret) {
1374 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001375 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001376 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001377 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001378 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001379 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001380 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001382 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001383
Yan Zhengd899e052008-10-30 14:25:28 -04001384 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001385 u64 orig_start = found_key.offset - extent_offset;
1386
1387 em = create_io_em(inode, cur_offset, num_bytes,
1388 orig_start,
1389 disk_bytenr, /* block_start */
1390 num_bytes, /* block_len */
1391 disk_num_bytes, /* orig_block_len */
1392 ram_bytes, BTRFS_COMPRESS_NONE,
1393 BTRFS_ORDERED_PREALLOC);
1394 if (IS_ERR(em)) {
1395 if (!nolock && nocow)
1396 btrfs_end_write_no_snapshoting(root);
1397 if (nocow)
1398 btrfs_dec_nocow_writers(fs_info,
1399 disk_bytenr);
1400 ret = PTR_ERR(em);
1401 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001402 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001403 free_extent_map(em);
1404 }
1405
1406 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001407 type = BTRFS_ORDERED_PREALLOC;
1408 } else {
1409 type = BTRFS_ORDERED_NOCOW;
1410 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001411
1412 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001413 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001414 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001415 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001416 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001417
Yan, Zhengefa56462010-05-16 10:49:59 -04001418 if (root->root_key.objectid ==
1419 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1420 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1421 num_bytes);
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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001425 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001426 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001427 }
1428
Josef Bacikc2790a22013-07-29 11:20:47 -04001429 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001430 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001431 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001432 EXTENT_DELALLOC |
1433 EXTENT_CLEAR_DATA_RESV,
1434 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1435
Wang Shilonge9894fd2014-03-27 11:12:25 +08001436 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001437 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001438 cur_offset = extent_end;
1439 if (cur_offset > end)
1440 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001441 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001442 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001443
Josef Bacik17ca04a2012-05-31 15:58:55 -04001444 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001445 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001446 cur_offset = end;
1447 }
1448
Yan Zheng80ff3852008-10-30 14:20:02 -04001449 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001450 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1451 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001452 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001453 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 }
1455
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001456error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001457 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001458 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001459 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001460 EXTENT_DELALLOC | EXTENT_DEFRAG |
1461 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1462 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001463 PAGE_SET_WRITEBACK |
1464 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001465 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001467}
1468
Wang Shilong47059d92014-07-03 18:22:07 +08001469static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1470{
1471
1472 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1473 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1474 return 0;
1475
1476 /*
1477 * @defrag_bytes is a hint value, no spinlock held here,
1478 * if is not zero, it means the file is defragging.
1479 * Force cow if given extent needs to be defragged.
1480 */
1481 if (BTRFS_I(inode)->defrag_bytes &&
1482 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1483 EXTENT_DEFRAG, 0, NULL))
1484 return 1;
1485
1486 return 0;
1487}
1488
Chris Masond352ac62008-09-29 15:18:18 -04001489/*
1490 * extent_io.c call back to do delayed allocation processing
1491 */
Chris Masonc8b97812008-10-29 14:49:59 -04001492static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001493 u64 start, u64 end, int *page_started,
1494 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001495{
Chris Masonbe20aa92007-12-17 20:14:01 -05001496 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001497 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001498
Wang Shilong47059d92014-07-03 18:22:07 +08001499 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001500 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001501 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001502 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001503 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001504 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001505 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001506 ret = cow_file_range(inode, locked_page, start, end, end,
1507 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001508 } else {
1509 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1510 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001511 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001512 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001513 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001514 return ret;
1515}
1516
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001517static void btrfs_split_extent_hook(struct inode *inode,
1518 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001519{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001520 u64 size;
1521
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001522 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001523 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001524 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001525
Josef Bacikdcab6a32015-02-11 15:08:59 -05001526 size = orig->end - orig->start + 1;
1527 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001528 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001529 u64 new_size;
1530
1531 /*
Josef Bacikba117212015-03-13 15:01:24 -04001532 * See the explanation in btrfs_merge_extent_hook, the same
1533 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001534 */
1535 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001536 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001537 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001538 num_extents += count_max_extents(new_size);
1539 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001540 return;
1541 }
1542
Josef Bacik9e0baf62011-07-15 15:16:44 +00001543 spin_lock(&BTRFS_I(inode)->lock);
1544 BTRFS_I(inode)->outstanding_extents++;
1545 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001546}
1547
1548/*
1549 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1550 * extents so we can keep track of new extents that are just merged onto old
1551 * extents, such as when we are doing sequential writes, so we can properly
1552 * account for the metadata space we'll need.
1553 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001554static void btrfs_merge_extent_hook(struct inode *inode,
1555 struct extent_state *new,
1556 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001557{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001558 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001559 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001560
Josef Bacik9ed74f22009-09-11 16:12:44 -04001561 /* not delalloc, ignore it */
1562 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001563 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001564
Josef Bacik8461a3d2015-03-13 15:12:08 -04001565 if (new->start > other->start)
1566 new_size = new->end - other->start + 1;
1567 else
1568 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001569
1570 /* we're not bigger than the max, unreserve the space and go */
1571 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1572 spin_lock(&BTRFS_I(inode)->lock);
1573 BTRFS_I(inode)->outstanding_extents--;
1574 spin_unlock(&BTRFS_I(inode)->lock);
1575 return;
1576 }
1577
1578 /*
Josef Bacikba117212015-03-13 15:01:24 -04001579 * We have to add up either side to figure out how many extents were
1580 * accounted for before we merged into one big extent. If the number of
1581 * extents we accounted for is <= the amount we need for the new range
1582 * then we can return, otherwise drop. Think of it like this
1583 *
1584 * [ 4k][MAX_SIZE]
1585 *
1586 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1587 * need 2 outstanding extents, on one side we have 1 and the other side
1588 * we have 1 so they are == and we can return. But in this case
1589 *
1590 * [MAX_SIZE+4k][MAX_SIZE+4k]
1591 *
1592 * Each range on their own accounts for 2 extents, but merged together
1593 * they are only 3 extents worth of accounting, so we need to drop in
1594 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001595 */
Josef Bacikba117212015-03-13 15:01:24 -04001596 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001597 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001598 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001599 num_extents += count_max_extents(old_size);
1600 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001601 return;
1602
Josef Bacik9e0baf62011-07-15 15:16:44 +00001603 spin_lock(&BTRFS_I(inode)->lock);
1604 BTRFS_I(inode)->outstanding_extents--;
1605 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001606}
1607
Miao Xieeb73c1b2013-05-15 07:48:22 +00001608static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1609 struct inode *inode)
1610{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001611 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1612
Miao Xieeb73c1b2013-05-15 07:48:22 +00001613 spin_lock(&root->delalloc_lock);
1614 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1615 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1616 &root->delalloc_inodes);
1617 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1618 &BTRFS_I(inode)->runtime_flags);
1619 root->nr_delalloc_inodes++;
1620 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001621 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001622 BUG_ON(!list_empty(&root->delalloc_root));
1623 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001624 &fs_info->delalloc_roots);
1625 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001626 }
1627 }
1628 spin_unlock(&root->delalloc_lock);
1629}
1630
1631static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1632 struct inode *inode)
1633{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001634 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1635
Miao Xieeb73c1b2013-05-15 07:48:22 +00001636 spin_lock(&root->delalloc_lock);
1637 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1638 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1639 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1640 &BTRFS_I(inode)->runtime_flags);
1641 root->nr_delalloc_inodes--;
1642 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001643 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001644 BUG_ON(list_empty(&root->delalloc_root));
1645 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001646 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001647 }
1648 }
1649 spin_unlock(&root->delalloc_lock);
1650}
1651
Chris Masond352ac62008-09-29 15:18:18 -04001652/*
1653 * extent_io.c set_bit_hook, used to track delayed allocation
1654 * bytes in this file, and to maintain the list of inodes that
1655 * have pending delalloc work to be done.
1656 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001657static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001658 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001659{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001660
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001661 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1662
Wang Shilong47059d92014-07-03 18:22:07 +08001663 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1664 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001665 /*
1666 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001667 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001668 * bit, which is only set or cleared with irqs on
1669 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001670 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001671 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001672 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001673 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001674
Josef Bacik9e0baf62011-07-15 15:16:44 +00001675 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001676 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001677 } else {
1678 spin_lock(&BTRFS_I(inode)->lock);
1679 BTRFS_I(inode)->outstanding_extents++;
1680 spin_unlock(&BTRFS_I(inode)->lock);
1681 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001682
Josef Bacik6a3891c2015-03-16 17:38:52 -04001683 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001684 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001685 return;
1686
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001687 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1688 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001689 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001690 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001691 if (*bits & EXTENT_DEFRAG)
1692 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001693 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001694 &BTRFS_I(inode)->runtime_flags))
1695 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001696 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001697 }
Chris Mason291d6732008-01-29 15:55:23 -05001698}
1699
Chris Masond352ac62008-09-29 15:18:18 -04001700/*
1701 * extent_io.c clear_bit_hook, see set_bit_hook for why
1702 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001703static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001704 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001705 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001706{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001707 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001708 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001709 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001710
1711 spin_lock(&BTRFS_I(inode)->lock);
1712 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1713 BTRFS_I(inode)->defrag_bytes -= len;
1714 spin_unlock(&BTRFS_I(inode)->lock);
1715
Chris Mason75eff682008-12-15 15:54:40 -05001716 /*
1717 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001718 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001719 * bit, which is only set or cleared with irqs on
1720 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001721 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001722 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001723 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001724
Josef Bacik9e0baf62011-07-15 15:16:44 +00001725 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001726 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001727 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1728 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001729 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001730 spin_unlock(&BTRFS_I(inode)->lock);
1731 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001732
Josef Bacikb6d08f02013-09-27 14:57:43 -04001733 /*
1734 * We don't reserve metadata space for space cache inodes so we
1735 * don't need to call dellalloc_release_metadata if there is an
1736 * error.
1737 */
1738 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001739 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001740 btrfs_delalloc_release_metadata(inode, len);
1741
Josef Bacik6a3891c2015-03-16 17:38:52 -04001742 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001743 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001744 return;
1745
Josef Bacik0cb59c92010-07-02 12:14:14 -04001746 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001747 && do_list && !(state->state & EXTENT_NORESERVE)
1748 && (*bits & (EXTENT_DO_ACCOUNTING |
1749 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001750 btrfs_free_reserved_data_space_noquota(inode,
1751 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001752
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001753 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1754 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001755 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001756 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001757 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001758 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001759 &BTRFS_I(inode)->runtime_flags))
1760 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001761 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001762 }
Chris Mason291d6732008-01-29 15:55:23 -05001763}
1764
Chris Masond352ac62008-09-29 15:18:18 -04001765/*
1766 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1767 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001768 *
1769 * return 1 if page cannot be merged to bio
1770 * return 0 if page can be merged to bio
1771 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001772 */
Mike Christie81a75f672016-06-05 14:31:54 -05001773int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001774 size_t size, struct bio *bio,
1775 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001776{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001777 struct inode *inode = page->mapping->host;
1778 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001779 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001780 u64 length = 0;
1781 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001782 int ret;
1783
Chris Mason771ed682008-11-06 22:02:51 -05001784 if (bio_flags & EXTENT_BIO_COMPRESSED)
1785 return 0;
1786
Kent Overstreet4f024f32013-10-11 15:44:27 -07001787 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001788 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001789 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1790 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001791 if (ret < 0)
1792 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001793 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001794 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001795 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001796}
1797
Chris Masond352ac62008-09-29 15:18:18 -04001798/*
1799 * in order to insert checksums into the metadata in large chunks,
1800 * we wait until bio submission time. All the pages in the bio are
1801 * checksummed and sums are attached onto the ordered extent record.
1802 *
1803 * At IO completion time the cums attached on the ordered extent record
1804 * are inserted into the btree
1805 */
Mike Christie81a75f672016-06-05 14:31:54 -05001806static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1807 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001808 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001809{
Chris Mason065631f2008-02-20 12:07:25 -05001810 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001811
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001812 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001813 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001814 return 0;
1815}
Chris Masone0156402008-04-16 11:15:20 -04001816
Chris Mason4a69a412008-11-06 22:03:00 -05001817/*
1818 * in order to insert checksums into the metadata in large chunks,
1819 * we wait until bio submission time. All the pages in the bio are
1820 * checksummed and sums are attached onto the ordered extent record.
1821 *
1822 * At IO completion time the cums attached on the ordered extent record
1823 * are inserted into the btree
1824 */
Mike Christie81a75f672016-06-05 14:31:54 -05001825static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001826 int mirror_num, unsigned long bio_flags,
1827 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001828{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001829 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001830 int ret;
1831
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001832 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001833 if (ret) {
1834 bio->bi_error = ret;
1835 bio_endio(bio);
1836 }
Stefan Behrens61891922012-11-05 18:51:52 +01001837 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001838}
1839
Chris Masond352ac62008-09-29 15:18:18 -04001840/*
Chris Masoncad321a2008-12-17 14:51:42 -05001841 * extent_io.c submission hook. This does the right thing for csum calculation
1842 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001843 */
Mike Christie81a75f672016-06-05 14:31:54 -05001844static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001845 int mirror_num, unsigned long bio_flags,
1846 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001847{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001849 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301850 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001851 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001852 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001853 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001854
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001855 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001856
Liu Bo83eea1f2012-07-10 05:28:39 -06001857 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301858 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001859
Mike Christie37226b22016-06-05 14:31:52 -05001860 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001861 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001862 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001863 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001864
Chris Masond20f7042008-12-08 16:58:54 -05001865 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001866 ret = btrfs_submit_compressed_read(inode, bio,
1867 mirror_num,
1868 bio_flags);
1869 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001870 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001871 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001872 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001873 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001874 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001875 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001876 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001877 /* csum items have already been cloned */
1878 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1879 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001880 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001881 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1882 bio_flags, bio_offset,
1883 __btrfs_submit_bio_start,
1884 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001885 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001886 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001887 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001888 if (ret)
1889 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001890 }
1891
Chris Mason0b86a832008-03-24 15:01:56 -04001892mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001893 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001894
1895out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001896 if (ret < 0) {
1897 bio->bi_error = ret;
1898 bio_endio(bio);
1899 }
Stefan Behrens61891922012-11-05 18:51:52 +01001900 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001901}
Chris Mason6885f302008-02-20 16:11:05 -05001902
Chris Masond352ac62008-09-29 15:18:18 -04001903/*
1904 * given a list of ordered sums record them in the inode. This happens
1905 * at IO completion time based on sums calculated at bio submission time.
1906 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001907static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01001908 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001909{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001910 struct btrfs_ordered_sum *sum;
1911
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001912 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001913 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001914 btrfs_csum_file_blocks(trans,
1915 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001916 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001917 }
1918 return 0;
1919}
1920
Josef Bacik2ac55d42010-02-03 19:33:23 +00001921int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001922 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04001923{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001924 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001925 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001926 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001927}
1928
Chris Masond352ac62008-09-29 15:18:18 -04001929/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001930struct btrfs_writepage_fixup {
1931 struct page *page;
1932 struct btrfs_work work;
1933};
1934
Christoph Hellwigb2950862008-12-02 09:54:17 -05001935static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001936{
1937 struct btrfs_writepage_fixup *fixup;
1938 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001939 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001940 struct page *page;
1941 struct inode *inode;
1942 u64 page_start;
1943 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001944 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001945
1946 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1947 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001948again:
Chris Mason247e7432008-07-17 12:53:51 -04001949 lock_page(page);
1950 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1951 ClearPageChecked(page);
1952 goto out_page;
1953 }
1954
1955 inode = page->mapping->host;
1956 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001957 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04001958
David Sterbaff13db42015-12-03 14:30:40 +01001959 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001960 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001961
1962 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001963 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001964 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001965
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05301966 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001967 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04001968 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001969 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1970 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001971 unlock_page(page);
1972 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001973 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001974 goto again;
1975 }
Chris Mason247e7432008-07-17 12:53:51 -04001976
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001977 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001978 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001979 if (ret) {
1980 mapping_set_error(page->mapping, ret);
1981 end_extent_writepage(page, ret, page_start, page_end);
1982 ClearPageChecked(page);
1983 goto out;
1984 }
1985
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001986 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
1987 0);
Chris Mason247e7432008-07-17 12:53:51 -04001988 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001989 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001990out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001991 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1992 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001993out_page:
1994 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001995 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001996 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001997}
1998
1999/*
2000 * There are a few paths in the higher layers of the kernel that directly
2001 * set the page dirty bit without asking the filesystem if it is a
2002 * good idea. This causes problems because we want to make sure COW
2003 * properly happens and the data=ordered rules are followed.
2004 *
Chris Masonc8b97812008-10-29 14:49:59 -04002005 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002006 * hasn't been properly setup for IO. We kick off an async process
2007 * to fix it up. The async helper will wait for ordered extents, set
2008 * the delalloc bit and make it safe to write the page.
2009 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002010static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002011{
2012 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002013 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002014 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002015
Chris Mason8b62b722009-09-02 16:53:46 -04002016 /* this page is properly in the ordered list */
2017 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002018 return 0;
2019
2020 if (PageChecked(page))
2021 return -EAGAIN;
2022
2023 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2024 if (!fixup)
2025 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002026
Chris Mason247e7432008-07-17 12:53:51 -04002027 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002028 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002029 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2030 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002031 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002032 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002033 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002034}
2035
Yan Zhengd899e052008-10-30 14:25:28 -04002036static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2037 struct inode *inode, u64 file_pos,
2038 u64 disk_bytenr, u64 disk_num_bytes,
2039 u64 num_bytes, u64 ram_bytes,
2040 u8 compression, u8 encryption,
2041 u16 other_encoding, int extent_type)
2042{
2043 struct btrfs_root *root = BTRFS_I(inode)->root;
2044 struct btrfs_file_extent_item *fi;
2045 struct btrfs_path *path;
2046 struct extent_buffer *leaf;
2047 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002048 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002049 int ret;
2050
2051 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002052 if (!path)
2053 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002054
Chris Masona1ed8352009-09-11 12:27:37 -04002055 /*
2056 * we may be replacing one extent in the tree with another.
2057 * The new extent is pinned in the extent map, and we don't want
2058 * to drop it from the cache until it is completely in the btree.
2059 *
2060 * So, tell btrfs_drop_extents to leave this extent in the cache.
2061 * the caller is expected to unpin it and allow it to be merged
2062 * with the others.
2063 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002064 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2065 file_pos + num_bytes, NULL, 0,
2066 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002067 if (ret)
2068 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002069
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002070 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002071 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002072 ins.offset = file_pos;
2073 ins.type = BTRFS_EXTENT_DATA_KEY;
2074
2075 path->leave_spinning = 1;
2076 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2077 sizeof(*fi));
2078 if (ret)
2079 goto out;
2080 }
Yan Zhengd899e052008-10-30 14:25:28 -04002081 leaf = path->nodes[0];
2082 fi = btrfs_item_ptr(leaf, path->slots[0],
2083 struct btrfs_file_extent_item);
2084 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2085 btrfs_set_file_extent_type(leaf, fi, extent_type);
2086 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2087 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2088 btrfs_set_file_extent_offset(leaf, fi, 0);
2089 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2090 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2091 btrfs_set_file_extent_compression(leaf, fi, compression);
2092 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2093 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002094
Yan Zhengd899e052008-10-30 14:25:28 -04002095 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002096 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002097
2098 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002099
2100 ins.objectid = disk_bytenr;
2101 ins.offset = disk_num_bytes;
2102 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002103 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002104 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002105 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002106 * Release the reserved range from inode dirty range map, as it is
2107 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002108 */
2109 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002110out:
Yan Zhengd899e052008-10-30 14:25:28 -04002111 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002112
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002113 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002114}
2115
Liu Bo38c227d2013-01-29 03:18:40 +00002116/* snapshot-aware defrag */
2117struct sa_defrag_extent_backref {
2118 struct rb_node node;
2119 struct old_sa_defrag_extent *old;
2120 u64 root_id;
2121 u64 inum;
2122 u64 file_pos;
2123 u64 extent_offset;
2124 u64 num_bytes;
2125 u64 generation;
2126};
2127
2128struct old_sa_defrag_extent {
2129 struct list_head list;
2130 struct new_sa_defrag_extent *new;
2131
2132 u64 extent_offset;
2133 u64 bytenr;
2134 u64 offset;
2135 u64 len;
2136 int count;
2137};
2138
2139struct new_sa_defrag_extent {
2140 struct rb_root root;
2141 struct list_head head;
2142 struct btrfs_path *path;
2143 struct inode *inode;
2144 u64 file_pos;
2145 u64 len;
2146 u64 bytenr;
2147 u64 disk_len;
2148 u8 compress_type;
2149};
2150
2151static int backref_comp(struct sa_defrag_extent_backref *b1,
2152 struct sa_defrag_extent_backref *b2)
2153{
2154 if (b1->root_id < b2->root_id)
2155 return -1;
2156 else if (b1->root_id > b2->root_id)
2157 return 1;
2158
2159 if (b1->inum < b2->inum)
2160 return -1;
2161 else if (b1->inum > b2->inum)
2162 return 1;
2163
2164 if (b1->file_pos < b2->file_pos)
2165 return -1;
2166 else if (b1->file_pos > b2->file_pos)
2167 return 1;
2168
2169 /*
2170 * [------------------------------] ===> (a range of space)
2171 * |<--->| |<---->| =============> (fs/file tree A)
2172 * |<---------------------------->| ===> (fs/file tree B)
2173 *
2174 * A range of space can refer to two file extents in one tree while
2175 * refer to only one file extent in another tree.
2176 *
2177 * So we may process a disk offset more than one time(two extents in A)
2178 * and locate at the same extent(one extent in B), then insert two same
2179 * backrefs(both refer to the extent in B).
2180 */
2181 return 0;
2182}
2183
2184static void backref_insert(struct rb_root *root,
2185 struct sa_defrag_extent_backref *backref)
2186{
2187 struct rb_node **p = &root->rb_node;
2188 struct rb_node *parent = NULL;
2189 struct sa_defrag_extent_backref *entry;
2190 int ret;
2191
2192 while (*p) {
2193 parent = *p;
2194 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2195
2196 ret = backref_comp(backref, entry);
2197 if (ret < 0)
2198 p = &(*p)->rb_left;
2199 else
2200 p = &(*p)->rb_right;
2201 }
2202
2203 rb_link_node(&backref->node, parent, p);
2204 rb_insert_color(&backref->node, root);
2205}
2206
2207/*
2208 * Note the backref might has changed, and in this case we just return 0.
2209 */
2210static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2211 void *ctx)
2212{
2213 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002214 struct old_sa_defrag_extent *old = ctx;
2215 struct new_sa_defrag_extent *new = old->new;
2216 struct btrfs_path *path = new->path;
2217 struct btrfs_key key;
2218 struct btrfs_root *root;
2219 struct sa_defrag_extent_backref *backref;
2220 struct extent_buffer *leaf;
2221 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002222 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002223 int slot;
2224 int ret;
2225 u64 extent_offset;
2226 u64 num_bytes;
2227
2228 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002229 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002230 return 0;
2231
2232 key.objectid = root_id;
2233 key.type = BTRFS_ROOT_ITEM_KEY;
2234 key.offset = (u64)-1;
2235
Liu Bo38c227d2013-01-29 03:18:40 +00002236 root = btrfs_read_fs_root_no_name(fs_info, &key);
2237 if (IS_ERR(root)) {
2238 if (PTR_ERR(root) == -ENOENT)
2239 return 0;
2240 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002241 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002242 inum, offset, root_id);
2243 return PTR_ERR(root);
2244 }
2245
2246 key.objectid = inum;
2247 key.type = BTRFS_EXTENT_DATA_KEY;
2248 if (offset > (u64)-1 << 32)
2249 key.offset = 0;
2250 else
2251 key.offset = offset;
2252
2253 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302254 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002255 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002256 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002257
2258 while (1) {
2259 cond_resched();
2260
2261 leaf = path->nodes[0];
2262 slot = path->slots[0];
2263
2264 if (slot >= btrfs_header_nritems(leaf)) {
2265 ret = btrfs_next_leaf(root, path);
2266 if (ret < 0) {
2267 goto out;
2268 } else if (ret > 0) {
2269 ret = 0;
2270 goto out;
2271 }
2272 continue;
2273 }
2274
2275 path->slots[0]++;
2276
2277 btrfs_item_key_to_cpu(leaf, &key, slot);
2278
2279 if (key.objectid > inum)
2280 goto out;
2281
2282 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2283 continue;
2284
2285 extent = btrfs_item_ptr(leaf, slot,
2286 struct btrfs_file_extent_item);
2287
2288 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2289 continue;
2290
Liu Boe68afa42013-07-01 22:13:26 +08002291 /*
2292 * 'offset' refers to the exact key.offset,
2293 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2294 * (key.offset - extent_offset).
2295 */
2296 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002297 continue;
2298
Liu Boe68afa42013-07-01 22:13:26 +08002299 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002300 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002301
Liu Bo38c227d2013-01-29 03:18:40 +00002302 if (extent_offset >= old->extent_offset + old->offset +
2303 old->len || extent_offset + num_bytes <=
2304 old->extent_offset + old->offset)
2305 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002306 break;
2307 }
2308
2309 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2310 if (!backref) {
2311 ret = -ENOENT;
2312 goto out;
2313 }
2314
2315 backref->root_id = root_id;
2316 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002317 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002318 backref->num_bytes = num_bytes;
2319 backref->extent_offset = extent_offset;
2320 backref->generation = btrfs_file_extent_generation(leaf, extent);
2321 backref->old = old;
2322 backref_insert(&new->root, backref);
2323 old->count++;
2324out:
2325 btrfs_release_path(path);
2326 WARN_ON(ret);
2327 return ret;
2328}
2329
2330static noinline bool record_extent_backrefs(struct btrfs_path *path,
2331 struct new_sa_defrag_extent *new)
2332{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002333 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002334 struct old_sa_defrag_extent *old, *tmp;
2335 int ret;
2336
2337 new->path = path;
2338
2339 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002340 ret = iterate_inodes_from_logical(old->bytenr +
2341 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002342 path, record_one_backref,
2343 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002344 if (ret < 0 && ret != -ENOENT)
2345 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002346
2347 /* no backref to be processed for this extent */
2348 if (!old->count) {
2349 list_del(&old->list);
2350 kfree(old);
2351 }
2352 }
2353
2354 if (list_empty(&new->head))
2355 return false;
2356
2357 return true;
2358}
2359
2360static int relink_is_mergable(struct extent_buffer *leaf,
2361 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002362 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002363{
Liu Bo116e0022013-08-02 16:30:40 +08002364 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002365 return 0;
2366
2367 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2368 return 0;
2369
Liu Bo116e0022013-08-02 16:30:40 +08002370 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2371 return 0;
2372
2373 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002374 btrfs_file_extent_other_encoding(leaf, fi))
2375 return 0;
2376
2377 return 1;
2378}
2379
2380/*
2381 * Note the backref might has changed, and in this case we just return 0.
2382 */
2383static noinline int relink_extent_backref(struct btrfs_path *path,
2384 struct sa_defrag_extent_backref *prev,
2385 struct sa_defrag_extent_backref *backref)
2386{
2387 struct btrfs_file_extent_item *extent;
2388 struct btrfs_file_extent_item *item;
2389 struct btrfs_ordered_extent *ordered;
2390 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002391 struct btrfs_root *root;
2392 struct btrfs_key key;
2393 struct extent_buffer *leaf;
2394 struct old_sa_defrag_extent *old = backref->old;
2395 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002396 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002397 struct inode *inode;
2398 struct extent_state *cached = NULL;
2399 int ret = 0;
2400 u64 start;
2401 u64 len;
2402 u64 lock_start;
2403 u64 lock_end;
2404 bool merge = false;
2405 int index;
2406
2407 if (prev && prev->root_id == backref->root_id &&
2408 prev->inum == backref->inum &&
2409 prev->file_pos + prev->num_bytes == backref->file_pos)
2410 merge = true;
2411
2412 /* step 1: get root */
2413 key.objectid = backref->root_id;
2414 key.type = BTRFS_ROOT_ITEM_KEY;
2415 key.offset = (u64)-1;
2416
Liu Bo38c227d2013-01-29 03:18:40 +00002417 index = srcu_read_lock(&fs_info->subvol_srcu);
2418
2419 root = btrfs_read_fs_root_no_name(fs_info, &key);
2420 if (IS_ERR(root)) {
2421 srcu_read_unlock(&fs_info->subvol_srcu, index);
2422 if (PTR_ERR(root) == -ENOENT)
2423 return 0;
2424 return PTR_ERR(root);
2425 }
Liu Bo38c227d2013-01-29 03:18:40 +00002426
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002427 if (btrfs_root_readonly(root)) {
2428 srcu_read_unlock(&fs_info->subvol_srcu, index);
2429 return 0;
2430 }
2431
Liu Bo38c227d2013-01-29 03:18:40 +00002432 /* step 2: get inode */
2433 key.objectid = backref->inum;
2434 key.type = BTRFS_INODE_ITEM_KEY;
2435 key.offset = 0;
2436
2437 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2438 if (IS_ERR(inode)) {
2439 srcu_read_unlock(&fs_info->subvol_srcu, index);
2440 return 0;
2441 }
2442
2443 srcu_read_unlock(&fs_info->subvol_srcu, index);
2444
2445 /* step 3: relink backref */
2446 lock_start = backref->file_pos;
2447 lock_end = backref->file_pos + backref->num_bytes - 1;
2448 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002449 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002450
2451 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2452 if (ordered) {
2453 btrfs_put_ordered_extent(ordered);
2454 goto out_unlock;
2455 }
2456
2457 trans = btrfs_join_transaction(root);
2458 if (IS_ERR(trans)) {
2459 ret = PTR_ERR(trans);
2460 goto out_unlock;
2461 }
2462
2463 key.objectid = backref->inum;
2464 key.type = BTRFS_EXTENT_DATA_KEY;
2465 key.offset = backref->file_pos;
2466
2467 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2468 if (ret < 0) {
2469 goto out_free_path;
2470 } else if (ret > 0) {
2471 ret = 0;
2472 goto out_free_path;
2473 }
2474
2475 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2476 struct btrfs_file_extent_item);
2477
2478 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2479 backref->generation)
2480 goto out_free_path;
2481
2482 btrfs_release_path(path);
2483
2484 start = backref->file_pos;
2485 if (backref->extent_offset < old->extent_offset + old->offset)
2486 start += old->extent_offset + old->offset -
2487 backref->extent_offset;
2488
2489 len = min(backref->extent_offset + backref->num_bytes,
2490 old->extent_offset + old->offset + old->len);
2491 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2492
2493 ret = btrfs_drop_extents(trans, root, inode, start,
2494 start + len, 1);
2495 if (ret)
2496 goto out_free_path;
2497again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002498 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002499 key.type = BTRFS_EXTENT_DATA_KEY;
2500 key.offset = start;
2501
Liu Boa09a0a72013-03-11 09:20:58 +00002502 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002503 if (merge) {
2504 struct btrfs_file_extent_item *fi;
2505 u64 extent_len;
2506 struct btrfs_key found_key;
2507
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002508 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002509 if (ret < 0)
2510 goto out_free_path;
2511
2512 path->slots[0]--;
2513 leaf = path->nodes[0];
2514 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2515
2516 fi = btrfs_item_ptr(leaf, path->slots[0],
2517 struct btrfs_file_extent_item);
2518 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2519
Liu Bo116e0022013-08-02 16:30:40 +08002520 if (extent_len + found_key.offset == start &&
2521 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002522 btrfs_set_file_extent_num_bytes(leaf, fi,
2523 extent_len + len);
2524 btrfs_mark_buffer_dirty(leaf);
2525 inode_add_bytes(inode, len);
2526
2527 ret = 1;
2528 goto out_free_path;
2529 } else {
2530 merge = false;
2531 btrfs_release_path(path);
2532 goto again;
2533 }
2534 }
2535
2536 ret = btrfs_insert_empty_item(trans, root, path, &key,
2537 sizeof(*extent));
2538 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002539 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002540 goto out_free_path;
2541 }
2542
2543 leaf = path->nodes[0];
2544 item = btrfs_item_ptr(leaf, path->slots[0],
2545 struct btrfs_file_extent_item);
2546 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2547 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2548 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2549 btrfs_set_file_extent_num_bytes(leaf, item, len);
2550 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2551 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2552 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2553 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2554 btrfs_set_file_extent_encryption(leaf, item, 0);
2555 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2556
2557 btrfs_mark_buffer_dirty(leaf);
2558 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002559 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002560
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002561 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002562 new->disk_len, 0,
2563 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002564 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002565 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002566 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002567 goto out_free_path;
2568 }
2569
2570 ret = 1;
2571out_free_path:
2572 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002573 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002574 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002575out_unlock:
2576 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2577 &cached, GFP_NOFS);
2578 iput(inode);
2579 return ret;
2580}
2581
Liu Bo6f519562013-10-29 10:45:05 +08002582static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2583{
2584 struct old_sa_defrag_extent *old, *tmp;
2585
2586 if (!new)
2587 return;
2588
2589 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002590 kfree(old);
2591 }
2592 kfree(new);
2593}
2594
Liu Bo38c227d2013-01-29 03:18:40 +00002595static void relink_file_extents(struct new_sa_defrag_extent *new)
2596{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002597 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002598 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002599 struct sa_defrag_extent_backref *backref;
2600 struct sa_defrag_extent_backref *prev = NULL;
2601 struct inode *inode;
2602 struct btrfs_root *root;
2603 struct rb_node *node;
2604 int ret;
2605
2606 inode = new->inode;
2607 root = BTRFS_I(inode)->root;
2608
2609 path = btrfs_alloc_path();
2610 if (!path)
2611 return;
2612
2613 if (!record_extent_backrefs(path, new)) {
2614 btrfs_free_path(path);
2615 goto out;
2616 }
2617 btrfs_release_path(path);
2618
2619 while (1) {
2620 node = rb_first(&new->root);
2621 if (!node)
2622 break;
2623 rb_erase(node, &new->root);
2624
2625 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2626
2627 ret = relink_extent_backref(path, prev, backref);
2628 WARN_ON(ret < 0);
2629
2630 kfree(prev);
2631
2632 if (ret == 1)
2633 prev = backref;
2634 else
2635 prev = NULL;
2636 cond_resched();
2637 }
2638 kfree(prev);
2639
2640 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002641out:
Liu Bo6f519562013-10-29 10:45:05 +08002642 free_sa_defrag_extent(new);
2643
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002644 atomic_dec(&fs_info->defrag_running);
2645 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002646}
2647
2648static struct new_sa_defrag_extent *
2649record_old_file_extents(struct inode *inode,
2650 struct btrfs_ordered_extent *ordered)
2651{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002652 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002653 struct btrfs_root *root = BTRFS_I(inode)->root;
2654 struct btrfs_path *path;
2655 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002656 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002657 struct new_sa_defrag_extent *new;
2658 int ret;
2659
2660 new = kmalloc(sizeof(*new), GFP_NOFS);
2661 if (!new)
2662 return NULL;
2663
2664 new->inode = inode;
2665 new->file_pos = ordered->file_offset;
2666 new->len = ordered->len;
2667 new->bytenr = ordered->start;
2668 new->disk_len = ordered->disk_len;
2669 new->compress_type = ordered->compress_type;
2670 new->root = RB_ROOT;
2671 INIT_LIST_HEAD(&new->head);
2672
2673 path = btrfs_alloc_path();
2674 if (!path)
2675 goto out_kfree;
2676
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002677 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002678 key.type = BTRFS_EXTENT_DATA_KEY;
2679 key.offset = new->file_pos;
2680
2681 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2682 if (ret < 0)
2683 goto out_free_path;
2684 if (ret > 0 && path->slots[0] > 0)
2685 path->slots[0]--;
2686
2687 /* find out all the old extents for the file range */
2688 while (1) {
2689 struct btrfs_file_extent_item *extent;
2690 struct extent_buffer *l;
2691 int slot;
2692 u64 num_bytes;
2693 u64 offset;
2694 u64 end;
2695 u64 disk_bytenr;
2696 u64 extent_offset;
2697
2698 l = path->nodes[0];
2699 slot = path->slots[0];
2700
2701 if (slot >= btrfs_header_nritems(l)) {
2702 ret = btrfs_next_leaf(root, path);
2703 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002704 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002705 else if (ret > 0)
2706 break;
2707 continue;
2708 }
2709
2710 btrfs_item_key_to_cpu(l, &key, slot);
2711
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002712 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002713 break;
2714 if (key.type != BTRFS_EXTENT_DATA_KEY)
2715 break;
2716 if (key.offset >= new->file_pos + new->len)
2717 break;
2718
2719 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2720
2721 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2722 if (key.offset + num_bytes < new->file_pos)
2723 goto next;
2724
2725 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2726 if (!disk_bytenr)
2727 goto next;
2728
2729 extent_offset = btrfs_file_extent_offset(l, extent);
2730
2731 old = kmalloc(sizeof(*old), GFP_NOFS);
2732 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002733 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002734
2735 offset = max(new->file_pos, key.offset);
2736 end = min(new->file_pos + new->len, key.offset + num_bytes);
2737
2738 old->bytenr = disk_bytenr;
2739 old->extent_offset = extent_offset;
2740 old->offset = offset - key.offset;
2741 old->len = end - offset;
2742 old->new = new;
2743 old->count = 0;
2744 list_add_tail(&old->list, &new->head);
2745next:
2746 path->slots[0]++;
2747 cond_resched();
2748 }
2749
2750 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002751 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002752
2753 return new;
2754
Liu Bo38c227d2013-01-29 03:18:40 +00002755out_free_path:
2756 btrfs_free_path(path);
2757out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002758 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002759 return NULL;
2760}
2761
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002762static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002763 u64 start, u64 len)
2764{
2765 struct btrfs_block_group_cache *cache;
2766
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002767 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002768 ASSERT(cache);
2769
2770 spin_lock(&cache->lock);
2771 cache->delalloc_bytes -= len;
2772 spin_unlock(&cache->lock);
2773
2774 btrfs_put_block_group(cache);
2775}
2776
Chris Masond352ac62008-09-29 15:18:18 -04002777/* as ordered data IO finishes, this gets called so we can finish
2778 * an ordered extent if the range of bytes in the file it covers are
2779 * fully written.
2780 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002781static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002782{
Josef Bacik5fd02042012-05-02 14:00:54 -04002783 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002784 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002785 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002786 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002787 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002788 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002789 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002790 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002791 int ret = 0;
2792 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002793 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002794 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002795
Liu Bo83eea1f2012-07-10 05:28:39 -06002796 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002797
Josef Bacik5fd02042012-05-02 14:00:54 -04002798 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2799 ret = -EIO;
2800 goto out;
2801 }
2802
Miao Xief6124962014-09-12 18:44:04 +08002803 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2804 ordered_extent->file_offset +
2805 ordered_extent->len - 1);
2806
Josef Bacik77cef2e2013-08-29 13:57:21 -04002807 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2808 truncated = true;
2809 logical_len = ordered_extent->truncated_len;
2810 /* Truncated the entire extent, don't bother adding */
2811 if (!logical_len)
2812 goto out;
2813 }
2814
Yan, Zhengc2167752009-11-12 09:34:21 +00002815 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002816 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002817
2818 /*
2819 * For mwrite(mmap + memset to write) case, we still reserve
2820 * space for NOCOW range.
2821 * As NOCOW won't cause a new delayed ref, just free the space
2822 */
2823 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2824 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002825 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2826 if (nolock)
2827 trans = btrfs_join_transaction_nolock(root);
2828 else
2829 trans = btrfs_join_transaction(root);
2830 if (IS_ERR(trans)) {
2831 ret = PTR_ERR(trans);
2832 trans = NULL;
2833 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002834 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002835 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002836 ret = btrfs_update_inode_fallback(trans, root, inode);
2837 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002838 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002839 goto out;
2840 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002841
Josef Bacik2ac55d42010-02-03 19:33:23 +00002842 lock_extent_bits(io_tree, ordered_extent->file_offset,
2843 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002844 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002845
Liu Bo38c227d2013-01-29 03:18:40 +00002846 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2847 ordered_extent->file_offset + ordered_extent->len - 1,
2848 EXTENT_DEFRAG, 1, cached_state);
2849 if (ret) {
2850 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002851 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002852 /* the inode is shared */
2853 new = record_old_file_extents(inode, ordered_extent);
2854
2855 clear_extent_bit(io_tree, ordered_extent->file_offset,
2856 ordered_extent->file_offset + ordered_extent->len - 1,
2857 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2858 }
2859
Josef Bacik0cb59c92010-07-02 12:14:14 -04002860 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002861 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002862 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002863 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002864 if (IS_ERR(trans)) {
2865 ret = PTR_ERR(trans);
2866 trans = NULL;
2867 goto out_unlock;
2868 }
Chris Masona79b7d42014-05-22 16:18:52 -07002869
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002870 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002871
Chris Masonc8b97812008-10-29 14:49:59 -04002872 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002873 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002874 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002875 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002876 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002877 ordered_extent->file_offset,
2878 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002879 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002880 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002881 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002882 ret = insert_reserved_file_extent(trans, inode,
2883 ordered_extent->file_offset,
2884 ordered_extent->start,
2885 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002886 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002887 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002888 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002889 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002890 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002891 ordered_extent->start,
2892 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002893 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002894 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2895 ordered_extent->file_offset, ordered_extent->len,
2896 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002897 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002898 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002899 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002900 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002901
David Sterbadf9f6282017-02-10 19:35:37 +01002902 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002903
Josef Bacik6c760c02012-11-09 10:53:21 -05002904 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2905 ret = btrfs_update_inode_fallback(trans, root, inode);
2906 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002907 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002908 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002909 }
2910 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002911out_unlock:
2912 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2913 ordered_extent->file_offset +
2914 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002915out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002916 if (root != fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002917 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002918 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002919 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002920
Josef Bacik77cef2e2013-08-29 13:57:21 -04002921 if (ret || truncated) {
2922 u64 start, end;
2923
2924 if (truncated)
2925 start = ordered_extent->file_offset + logical_len;
2926 else
2927 start = ordered_extent->file_offset;
2928 end = ordered_extent->file_offset + ordered_extent->len - 1;
2929 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2930
2931 /* Drop the cache for the part of the extent we didn't write. */
2932 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002933
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002934 /*
2935 * If the ordered extent had an IOERR or something else went
2936 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002937 * back to the allocator. We only free the extent in the
2938 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002939 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002940 if ((ret || !logical_len) &&
2941 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002942 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002943 btrfs_free_reserved_extent(fs_info,
2944 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002945 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002946 }
2947
2948
Josef Bacik5fd02042012-05-02 14:00:54 -04002949 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002950 * This needs to be done to make sure anybody waiting knows we are done
2951 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002952 */
2953 btrfs_remove_ordered_extent(inode, ordered_extent);
2954
Liu Bo38c227d2013-01-29 03:18:40 +00002955 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002956 if (new) {
2957 if (ret) {
2958 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002959 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08002960 } else {
2961 relink_file_extents(new);
2962 }
2963 }
Liu Bo38c227d2013-01-29 03:18:40 +00002964
Chris Masone6dcd2d2008-07-17 12:53:50 -04002965 /* once for us */
2966 btrfs_put_ordered_extent(ordered_extent);
2967 /* once for the tree */
2968 btrfs_put_ordered_extent(ordered_extent);
2969
Josef Bacik5fd02042012-05-02 14:00:54 -04002970 return ret;
2971}
2972
2973static void finish_ordered_fn(struct btrfs_work *work)
2974{
2975 struct btrfs_ordered_extent *ordered_extent;
2976 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2977 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002978}
2979
Christoph Hellwigb2950862008-12-02 09:54:17 -05002980static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002981 struct extent_state *state, int uptodate)
2982{
Josef Bacik5fd02042012-05-02 14:00:54 -04002983 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002984 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002985 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002986 struct btrfs_workqueue *wq;
2987 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002988
liubo1abe9b82011-03-24 11:18:59 +00002989 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2990
Chris Mason8b62b722009-09-02 16:53:46 -04002991 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002992 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2993 end - start + 1, uptodate))
2994 return 0;
2995
Liu Bo9e0af232014-08-15 23:36:53 +08002996 if (btrfs_is_free_space_inode(inode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002997 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08002998 func = btrfs_freespace_write_helper;
2999 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003000 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003001 func = btrfs_endio_write_helper;
3002 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003003
Liu Bo9e0af232014-08-15 23:36:53 +08003004 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3005 NULL);
3006 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003007
3008 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003009}
3010
Miao Xiedc380ae2014-09-12 18:43:55 +08003011static int __readpage_endio_check(struct inode *inode,
3012 struct btrfs_io_bio *io_bio,
3013 int icsum, struct page *page,
3014 int pgoff, u64 start, size_t len)
3015{
3016 char *kaddr;
3017 u32 csum_expected;
3018 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003019
3020 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3021
3022 kaddr = kmap_atomic(page);
3023 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003024 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003025 if (csum != csum_expected)
3026 goto zeroit;
3027
3028 kunmap_atomic(kaddr);
3029 return 0;
3030zeroit:
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003031 btrfs_print_data_csum_error(inode, start, csum, csum_expected,
3032 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003033 memset(kaddr + pgoff, 1, len);
3034 flush_dcache_page(page);
3035 kunmap_atomic(kaddr);
3036 if (csum_expected == 0)
3037 return 0;
3038 return -EIO;
3039}
3040
Chris Masond352ac62008-09-29 15:18:18 -04003041/*
Chris Masond352ac62008-09-29 15:18:18 -04003042 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003043 * if there's a match, we allow the bio to finish. If not, the code in
3044 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003045 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003046static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3047 u64 phy_offset, struct page *page,
3048 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003049{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003050 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003051 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003052 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003053 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003054
Chris Masond20f7042008-12-08 16:58:54 -05003055 if (PageChecked(page)) {
3056 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003057 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003058 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003059
3060 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003061 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003062
Yan Zheng17d217f2008-12-12 10:03:38 -05003063 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003064 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003065 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003066 return 0;
3067 }
3068
Miao Xiefacc8a222013-07-25 19:22:34 +08003069 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003070 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3071 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003072}
Chris Masonb888db2b2007-08-27 16:49:44 -04003073
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003074void btrfs_add_delayed_iput(struct inode *inode)
3075{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003076 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003077 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003078
3079 if (atomic_add_unless(&inode->i_count, -1, 1))
3080 return;
3081
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003082 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003083 if (binode->delayed_iput_count == 0) {
3084 ASSERT(list_empty(&binode->delayed_iput));
3085 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3086 } else {
3087 binode->delayed_iput_count++;
3088 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003089 spin_unlock(&fs_info->delayed_iput_lock);
3090}
3091
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003092void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003093{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003094
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003095 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003096 while (!list_empty(&fs_info->delayed_iputs)) {
3097 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003098
David Sterba8089fe62015-11-19 14:15:51 +01003099 inode = list_first_entry(&fs_info->delayed_iputs,
3100 struct btrfs_inode, delayed_iput);
3101 if (inode->delayed_iput_count) {
3102 inode->delayed_iput_count--;
3103 list_move_tail(&inode->delayed_iput,
3104 &fs_info->delayed_iputs);
3105 } else {
3106 list_del_init(&inode->delayed_iput);
3107 }
3108 spin_unlock(&fs_info->delayed_iput_lock);
3109 iput(&inode->vfs_inode);
3110 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003111 }
David Sterba8089fe62015-11-19 14:15:51 +01003112 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003113}
3114
Yan, Zhengd68fc572010-05-16 10:49:58 -04003115/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003116 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003117 * files in the subvolume, it removes orphan item and frees block_rsv
3118 * structure.
3119 */
3120void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3121 struct btrfs_root *root)
3122{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003123 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003124 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003125 int ret;
3126
Josef Bacik8a35d952012-05-23 14:26:42 -04003127 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003128 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3129 return;
3130
Josef Bacik90290e12011-12-02 15:44:12 -05003131 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003132 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003133 spin_unlock(&root->orphan_lock);
3134 return;
3135 }
3136
3137 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3138 spin_unlock(&root->orphan_lock);
3139 return;
3140 }
3141
3142 block_rsv = root->orphan_block_rsv;
3143 root->orphan_block_rsv = NULL;
3144 spin_unlock(&root->orphan_lock);
3145
Miao Xie27cdeb72014-04-02 19:51:05 +08003146 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003147 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003148 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003149 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003150 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003151 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003152 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003153 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3154 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003155 }
3156
Josef Bacik90290e12011-12-02 15:44:12 -05003157 if (block_rsv) {
3158 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003159 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003160 }
3161}
3162
3163/*
Josef Bacik7b128762008-07-24 12:17:14 -04003164 * This creates an orphan entry for the given inode in case something goes
3165 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003166 *
3167 * NOTE: caller of this function should reserve 5 units of metadata for
3168 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003169 */
3170int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3171{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003172 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003173 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003174 struct btrfs_block_rsv *block_rsv = NULL;
3175 int reserve = 0;
3176 int insert = 0;
3177 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003178
Yan, Zhengd68fc572010-05-16 10:49:58 -04003179 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003180 block_rsv = btrfs_alloc_block_rsv(fs_info,
3181 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003182 if (!block_rsv)
3183 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003184 }
3185
Yan, Zhengd68fc572010-05-16 10:49:58 -04003186 spin_lock(&root->orphan_lock);
3187 if (!root->orphan_block_rsv) {
3188 root->orphan_block_rsv = block_rsv;
3189 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003190 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003191 block_rsv = NULL;
3192 }
Josef Bacik7b128762008-07-24 12:17:14 -04003193
Josef Bacik8a35d952012-05-23 14:26:42 -04003194 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3195 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003196#if 0
3197 /*
3198 * For proper ENOSPC handling, we should do orphan
3199 * cleanup when mounting. But this introduces backward
3200 * compatibility issue.
3201 */
3202 if (!xchg(&root->orphan_item_inserted, 1))
3203 insert = 2;
3204 else
3205 insert = 1;
3206#endif
3207 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003208 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003209 }
Josef Bacik7b128762008-07-24 12:17:14 -04003210
Josef Bacik72ac3c02012-05-23 14:13:11 -04003211 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3212 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003213 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003214 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003215
Yan, Zhengd68fc572010-05-16 10:49:58 -04003216 /* grab metadata reservation from transaction handle */
3217 if (reserve) {
3218 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003219 ASSERT(!ret);
3220 if (ret) {
3221 atomic_dec(&root->orphan_inodes);
3222 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3223 &BTRFS_I(inode)->runtime_flags);
3224 if (insert)
3225 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3226 &BTRFS_I(inode)->runtime_flags);
3227 return ret;
3228 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003229 }
3230
3231 /* insert an orphan item to track this unlinked/truncated file */
3232 if (insert >= 1) {
David Sterbaf85b7372017-01-20 14:54:07 +01003233 ret = btrfs_insert_orphan_item(trans, root,
3234 btrfs_ino(BTRFS_I(inode)));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003235 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003236 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003237 if (reserve) {
3238 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3239 &BTRFS_I(inode)->runtime_flags);
3240 btrfs_orphan_release_metadata(inode);
3241 }
3242 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003243 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3244 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003245 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003246 return ret;
3247 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003248 }
3249 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 }
3251
3252 /* insert an orphan item to track subvolume contains orphan files */
3253 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003254 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003256 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003257 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003258 return ret;
3259 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003260 }
3261 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003262}
3263
3264/*
3265 * We have done the truncate/delete so we can go ahead and remove the orphan
3266 * item for this particular inode.
3267 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003268static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3269 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003270{
3271 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003272 int delete_item = 0;
3273 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003274 int ret = 0;
3275
Yan, Zhengd68fc572010-05-16 10:49:58 -04003276 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003277 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3278 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003279 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003280
Josef Bacik72ac3c02012-05-23 14:13:11 -04003281 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3282 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003283 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284 spin_unlock(&root->orphan_lock);
3285
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003286 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003287 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003288 if (trans)
3289 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003290 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04003291 }
Josef Bacik7b128762008-07-24 12:17:14 -04003292
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003293 if (release_rsv)
3294 btrfs_orphan_release_metadata(inode);
3295
Josef Bacik4ef31a42013-08-13 14:10:08 -04003296 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003297}
3298
3299/*
3300 * this cleans up any orphans that may be left on the list from the last use
3301 * of this root.
3302 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003303int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003304{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003305 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003306 struct btrfs_path *path;
3307 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003308 struct btrfs_key key, found_key;
3309 struct btrfs_trans_handle *trans;
3310 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003311 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003312 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3313
Yan, Zhengd68fc572010-05-16 10:49:58 -04003314 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003315 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003316
3317 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003318 if (!path) {
3319 ret = -ENOMEM;
3320 goto out;
3321 }
David Sterbae4058b52015-11-27 16:31:35 +01003322 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003323
3324 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003325 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003326 key.offset = (u64)-1;
3327
Josef Bacik7b128762008-07-24 12:17:14 -04003328 while (1) {
3329 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003330 if (ret < 0)
3331 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003332
3333 /*
3334 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003335 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003336 * find the key and see if we have stuff that matches
3337 */
3338 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003339 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003340 if (path->slots[0] == 0)
3341 break;
3342 path->slots[0]--;
3343 }
3344
3345 /* pull out the item */
3346 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3348
3349 /* make sure the item matches what we want */
3350 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3351 break;
David Sterba962a2982014-06-04 18:41:45 +02003352 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003353 break;
3354
3355 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003356 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003357
3358 /*
3359 * this is where we are basically btrfs_lookup, without the
3360 * crossing root thing. we store the inode number in the
3361 * offset of the orphan item.
3362 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003363
3364 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003365 btrfs_err(fs_info,
3366 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003367 ret = -EINVAL;
3368 goto out;
3369 }
3370
3371 last_objectid = found_key.offset;
3372
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003373 found_key.objectid = found_key.offset;
3374 found_key.type = BTRFS_INODE_ITEM_KEY;
3375 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003376 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303377 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003378 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003379 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003380
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003381 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003382 struct btrfs_root *dead_root;
3383 struct btrfs_fs_info *fs_info = root->fs_info;
3384 int is_dead_root = 0;
3385
3386 /*
3387 * this is an orphan in the tree root. Currently these
3388 * could come from 2 sources:
3389 * a) a snapshot deletion in progress
3390 * b) a free space cache inode
3391 * We need to distinguish those two, as the snapshot
3392 * orphan must not get deleted.
3393 * find_dead_roots already ran before us, so if this
3394 * is a snapshot deletion, we should find the root
3395 * in the dead_roots list
3396 */
3397 spin_lock(&fs_info->trans_lock);
3398 list_for_each_entry(dead_root, &fs_info->dead_roots,
3399 root_list) {
3400 if (dead_root->root_key.objectid ==
3401 found_key.objectid) {
3402 is_dead_root = 1;
3403 break;
3404 }
3405 }
3406 spin_unlock(&fs_info->trans_lock);
3407 if (is_dead_root) {
3408 /* prevent this orphan from being found again */
3409 key.offset = found_key.objectid - 1;
3410 continue;
3411 }
3412 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003413 /*
3414 * Inode is already gone but the orphan item is still there,
3415 * kill the orphan item.
3416 */
Filipe Manana67710892016-06-06 11:51:25 +01003417 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003418 trans = btrfs_start_transaction(root, 1);
3419 if (IS_ERR(trans)) {
3420 ret = PTR_ERR(trans);
3421 goto out;
3422 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003423 btrfs_debug(fs_info, "auto deleting %Lu",
3424 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003425 ret = btrfs_del_orphan_item(trans, root,
3426 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003427 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003428 if (ret)
3429 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003430 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003431 }
Josef Bacik7b128762008-07-24 12:17:14 -04003432
Josef Bacik7b128762008-07-24 12:17:14 -04003433 /*
3434 * add this inode to the orphan list so btrfs_orphan_del does
3435 * the proper thing when we hit it
3436 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003437 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3438 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003439 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003440
Josef Bacik7b128762008-07-24 12:17:14 -04003441 /* if we have links, this was a truncate, lets do that */
3442 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303443 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003444 iput(inode);
3445 continue;
3446 }
Josef Bacik7b128762008-07-24 12:17:14 -04003447 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003448
3449 /* 1 for the orphan item deletion. */
3450 trans = btrfs_start_transaction(root, 1);
3451 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003452 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003453 ret = PTR_ERR(trans);
3454 goto out;
3455 }
3456 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003457 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003458 if (ret) {
3459 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003460 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003461 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003462
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003463 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003464 if (ret)
3465 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003466 } else {
3467 nr_unlink++;
3468 }
3469
3470 /* this will do delete_inode and everything for us */
3471 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003472 if (ret)
3473 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003474 }
Miao Xie3254c872011-11-10 20:45:05 -05003475 /* release the path since we're done with it */
3476 btrfs_release_path(path);
3477
Yan, Zhengd68fc572010-05-16 10:49:58 -04003478 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3479
3480 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003481 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003482 (u64)-1);
3483
Miao Xie27cdeb72014-04-02 19:51:05 +08003484 if (root->orphan_block_rsv ||
3485 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003486 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003487 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003488 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003489 }
Josef Bacik7b128762008-07-24 12:17:14 -04003490
3491 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003492 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003493 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003494 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495
3496out:
3497 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003498 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003499 btrfs_free_path(path);
3500 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003501}
3502
Chris Masond352ac62008-09-29 15:18:18 -04003503/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003504 * very simple check to peek ahead in the leaf looking for xattrs. If we
3505 * don't find any xattrs, we know there can't be any acls.
3506 *
3507 * slot is the slot the inode is in, objectid is the objectid of the inode
3508 */
3509static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003510 int slot, u64 objectid,
3511 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003512{
3513 u32 nritems = btrfs_header_nritems(leaf);
3514 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003515 static u64 xattr_access = 0;
3516 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003517 int scanned = 0;
3518
Josef Bacikf23b5a52013-06-19 10:16:26 -04003519 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003520 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3521 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3522 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3523 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003524 }
3525
Chris Mason46a53cc2009-04-27 11:47:50 -04003526 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003527 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003528 while (slot < nritems) {
3529 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3530
3531 /* we found a different objectid, there must not be acls */
3532 if (found_key.objectid != objectid)
3533 return 0;
3534
3535 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003536 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003537 if (*first_xattr_slot == -1)
3538 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003539 if (found_key.offset == xattr_access ||
3540 found_key.offset == xattr_default)
3541 return 1;
3542 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003543
3544 /*
3545 * we found a key greater than an xattr key, there can't
3546 * be any acls later on
3547 */
3548 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3549 return 0;
3550
3551 slot++;
3552 scanned++;
3553
3554 /*
3555 * it goes inode, inode backrefs, xattrs, extents,
3556 * so if there are a ton of hard links to an inode there can
3557 * be a lot of backrefs. Don't waste time searching too hard,
3558 * this is just an optimization
3559 */
3560 if (scanned >= 8)
3561 break;
3562 }
3563 /* we hit the end of the leaf before we found an xattr or
3564 * something larger than an xattr. We have to assume the inode
3565 * has acls
3566 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003567 if (*first_xattr_slot == -1)
3568 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003569 return 1;
3570}
3571
3572/*
Chris Masond352ac62008-09-29 15:18:18 -04003573 * read an inode from the btree into the in-memory inode
3574 */
Filipe Manana67710892016-06-06 11:51:25 +01003575static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003576{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003577 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003578 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003579 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003580 struct btrfs_inode_item *inode_item;
3581 struct btrfs_root *root = BTRFS_I(inode)->root;
3582 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003583 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003584 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003585 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003586 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003587 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003588 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003589
3590 ret = btrfs_fill_inode(inode, &rdev);
3591 if (!ret)
3592 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003593
3594 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003595 if (!path) {
3596 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003597 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003598 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003599
Chris Mason39279cc2007-06-12 06:35:45 -04003600 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003601
Chris Mason39279cc2007-06-12 06:35:45 -04003602 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003603 if (ret) {
3604 if (ret > 0)
3605 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003606 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003607 }
Chris Mason39279cc2007-06-12 06:35:45 -04003608
Chris Mason5f39d392007-10-15 16:14:19 -04003609 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003610
3611 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003612 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003613
Chris Mason5f39d392007-10-15 16:14:19 -04003614 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3615 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003616 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003617 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003618 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3619 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003620 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003621
David Sterbaa937b972014-12-12 17:39:12 +01003622 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3623 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003624
David Sterbaa937b972014-12-12 17:39:12 +01003625 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3626 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003627
David Sterbaa937b972014-12-12 17:39:12 +01003628 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3629 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003630
chandan r9cc97d62012-07-04 12:48:07 +05303631 BTRFS_I(inode)->i_otime.tv_sec =
3632 btrfs_timespec_sec(leaf, &inode_item->otime);
3633 BTRFS_I(inode)->i_otime.tv_nsec =
3634 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003635
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003636 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003637 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003638 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3639
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003640 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003641 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003642 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003643 rdev = btrfs_inode_rdev(leaf, inode_item);
3644
Josef Bacikaec74772008-07-24 12:12:38 -04003645 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003646 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003647
3648cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003649 /*
3650 * If we were modified in the current generation and evicted from memory
3651 * and then re-read we need to do a full sync since we don't have any
3652 * idea about which extents were modified before we were evicted from
3653 * cache.
3654 *
3655 * This is required for both inode re-read from disk and delayed inode
3656 * in delayed_nodes_tree.
3657 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003658 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003659 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3660 &BTRFS_I(inode)->runtime_flags);
3661
Filipe Mananabde6c242015-07-24 00:00:19 +01003662 /*
3663 * We don't persist the id of the transaction where an unlink operation
3664 * against the inode was last made. So here we assume the inode might
3665 * have been evicted, and therefore the exact value of last_unlink_trans
3666 * lost, and set it to last_trans to avoid metadata inconsistencies
3667 * between the inode and its parent if the inode is fsync'ed and the log
3668 * replayed. For example, in the scenario:
3669 *
3670 * touch mydir/foo
3671 * ln mydir/foo mydir/bar
3672 * sync
3673 * unlink mydir/bar
3674 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3675 * xfs_io -c fsync mydir/foo
3676 * <power failure>
3677 * mount fs, triggers fsync log replay
3678 *
3679 * We must make sure that when we fsync our inode foo we also log its
3680 * parent inode, otherwise after log replay the parent still has the
3681 * dentry with the "bar" name but our inode foo has a link count of 1
3682 * and doesn't have an inode ref with the name "bar" anymore.
3683 *
3684 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003685 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003686 * transaction commits on fsync if our inode is a directory, or if our
3687 * inode is not a directory, logging its parent unnecessarily.
3688 */
3689 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3690
Miao Xie67de1172013-12-26 13:07:06 +08003691 path->slots[0]++;
3692 if (inode->i_nlink != 1 ||
3693 path->slots[0] >= btrfs_header_nritems(leaf))
3694 goto cache_acl;
3695
3696 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003697 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003698 goto cache_acl;
3699
3700 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3701 if (location.type == BTRFS_INODE_REF_KEY) {
3702 struct btrfs_inode_ref *ref;
3703
3704 ref = (struct btrfs_inode_ref *)ptr;
3705 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3706 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3707 struct btrfs_inode_extref *extref;
3708
3709 extref = (struct btrfs_inode_extref *)ptr;
3710 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3711 extref);
3712 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003713cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003714 /*
3715 * try to precache a NULL acl entry for files that don't have
3716 * any xattrs or acls
3717 */
Li Zefan33345d012011-04-20 10:31:50 +08003718 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003719 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003720 if (first_xattr_slot != -1) {
3721 path->slots[0] = first_xattr_slot;
3722 ret = btrfs_load_inode_props(inode, path);
3723 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003724 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003725 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003726 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003727 root->root_key.objectid, ret);
3728 }
3729 btrfs_free_path(path);
3730
Al Viro72c04902009-06-24 16:58:48 -04003731 if (!maybe_acls)
3732 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003733
Chris Mason39279cc2007-06-12 06:35:45 -04003734 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003735 case S_IFREG:
3736 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003737 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003738 inode->i_fop = &btrfs_file_operations;
3739 inode->i_op = &btrfs_file_inode_operations;
3740 break;
3741 case S_IFDIR:
3742 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003743 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003744 break;
3745 case S_IFLNK:
3746 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003747 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003748 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3749 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003750 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003751 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003752 init_special_inode(inode, inode->i_mode, rdev);
3753 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003754 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003755
3756 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003757 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003758
3759make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003760 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003761 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003762 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003763}
3764
Chris Masond352ac62008-09-29 15:18:18 -04003765/*
3766 * given a leaf and an inode, copy the inode fields into the leaf
3767 */
Chris Masone02119d2008-09-05 16:13:11 -04003768static void fill_inode_item(struct btrfs_trans_handle *trans,
3769 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003770 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003771 struct inode *inode)
3772{
Liu Bo51fab692012-12-27 09:01:21 +00003773 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003774
Liu Bo51fab692012-12-27 09:01:21 +00003775 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003776
Liu Bo51fab692012-12-27 09:01:21 +00003777 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3778 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3779 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3780 &token);
3781 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3782 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003783
David Sterbaa937b972014-12-12 17:39:12 +01003784 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003785 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003786 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003787 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003788
David Sterbaa937b972014-12-12 17:39:12 +01003789 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003790 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003791 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003792 inode->i_mtime.tv_nsec, &token);
3793
David Sterbaa937b972014-12-12 17:39:12 +01003794 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003795 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003796 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003797 inode->i_ctime.tv_nsec, &token);
3798
chandan r9cc97d62012-07-04 12:48:07 +05303799 btrfs_set_token_timespec_sec(leaf, &item->otime,
3800 BTRFS_I(inode)->i_otime.tv_sec, &token);
3801 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3802 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3803
Liu Bo51fab692012-12-27 09:01:21 +00003804 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3805 &token);
3806 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3807 &token);
3808 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3809 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3810 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3811 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3812 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003813}
3814
Chris Masond352ac62008-09-29 15:18:18 -04003815/*
3816 * copy everything in the in-memory inode into the btree.
3817 */
Chris Mason21151332011-11-10 20:39:08 -05003818static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003819 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003820{
3821 struct btrfs_inode_item *inode_item;
3822 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003823 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003824 int ret;
3825
3826 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003827 if (!path)
3828 return -ENOMEM;
3829
Chris Masonb9473432009-03-13 11:00:37 -04003830 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003831 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3832 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003833 if (ret) {
3834 if (ret > 0)
3835 ret = -ENOENT;
3836 goto failed;
3837 }
3838
Chris Mason5f39d392007-10-15 16:14:19 -04003839 leaf = path->nodes[0];
3840 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003841 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003842
Chris Masone02119d2008-09-05 16:13:11 -04003843 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003844 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003845 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003846 ret = 0;
3847failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003848 btrfs_free_path(path);
3849 return ret;
3850}
3851
Chris Masond352ac62008-09-29 15:18:18 -04003852/*
Chris Mason21151332011-11-10 20:39:08 -05003853 * copy everything in the in-memory inode into the btree.
3854 */
3855noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3856 struct btrfs_root *root, struct inode *inode)
3857{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003858 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003859 int ret;
3860
3861 /*
3862 * If the inode is a free space inode, we can deadlock during commit
3863 * if we put it into the delayed code.
3864 *
3865 * The data relocation inode should also be directly updated
3866 * without delay
3867 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003868 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003869 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003870 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003871 btrfs_update_root_times(trans, root);
3872
Chris Mason21151332011-11-10 20:39:08 -05003873 ret = btrfs_delayed_update_inode(trans, root, inode);
3874 if (!ret)
3875 btrfs_set_inode_last_trans(trans, inode);
3876 return ret;
3877 }
3878
3879 return btrfs_update_inode_item(trans, root, inode);
3880}
3881
Josef Bacikbe6aef62012-10-22 15:43:12 -04003882noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3883 struct btrfs_root *root,
3884 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003885{
3886 int ret;
3887
3888 ret = btrfs_update_inode(trans, root, inode);
3889 if (ret == -ENOSPC)
3890 return btrfs_update_inode_item(trans, root, inode);
3891 return ret;
3892}
3893
3894/*
Chris Masond352ac62008-09-29 15:18:18 -04003895 * unlink helper that gets used here in inode.c and in the tree logging
3896 * recovery code. It remove a link in a directory with a given name, and
3897 * also drops the back refs in the inode to the directory
3898 */
Al Viro92986792011-03-04 17:14:37 +00003899static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3900 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003901 struct btrfs_inode *dir,
3902 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003903 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003904{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003905 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003906 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003907 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003908 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003909 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003910 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003911 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003912 u64 ino = btrfs_ino(inode);
3913 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003914
3915 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003916 if (!path) {
3917 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003918 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003919 }
3920
Chris Masonb9473432009-03-13 11:00:37 -04003921 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003922 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003923 name, name_len, -1);
3924 if (IS_ERR(di)) {
3925 ret = PTR_ERR(di);
3926 goto err;
3927 }
3928 if (!di) {
3929 ret = -ENOENT;
3930 goto err;
3931 }
Chris Mason5f39d392007-10-15 16:14:19 -04003932 leaf = path->nodes[0];
3933 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003934 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003935 if (ret)
3936 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003937 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003938
Miao Xie67de1172013-12-26 13:07:06 +08003939 /*
3940 * If we don't have dir index, we have to get it by looking up
3941 * the inode ref, since we get the inode ref, remove it directly,
3942 * it is unnecessary to do delayed deletion.
3943 *
3944 * But if we have dir index, needn't search inode ref to get it.
3945 * Since the inode ref is close to the inode item, it is better
3946 * that we delay to delete it, and just do this deletion when
3947 * we update the inode item.
3948 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003949 if (inode->dir_index) {
3950 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08003951 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003952 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003953 goto skip_backref;
3954 }
3955 }
3956
Li Zefan33345d012011-04-20 10:31:50 +08003957 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3958 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003959 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003960 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003961 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003962 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003963 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003964 goto err;
3965 }
Miao Xie67de1172013-12-26 13:07:06 +08003966skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003967 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003968 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003969 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003970 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003971 }
Chris Mason39279cc2007-06-12 06:35:45 -04003972
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003973 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3974 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003975 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003976 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003977 goto err;
3978 }
Chris Masone02119d2008-09-05 16:13:11 -04003979
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003980 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3981 index);
Chris Mason6418c962010-10-30 07:34:24 -04003982 if (ret == -ENOENT)
3983 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003984 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003985 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003986err:
3987 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003988 if (ret)
3989 goto out;
3990
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003991 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003992 inode_inc_iversion(&inode->vfs_inode);
3993 inode_inc_iversion(&dir->vfs_inode);
3994 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3995 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3996 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003997out:
Chris Mason39279cc2007-06-12 06:35:45 -04003998 return ret;
3999}
4000
Al Viro92986792011-03-04 17:14:37 +00004001int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4002 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004003 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004004 const char *name, int name_len)
4005{
4006 int ret;
4007 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4008 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004009 drop_nlink(&inode->vfs_inode);
4010 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004011 }
4012 return ret;
4013}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014
4015/*
4016 * helper to start transaction for unlink and rmdir.
4017 *
Josef Bacikd52be812013-05-29 14:54:47 -04004018 * unlink and rmdir are special in btrfs, they do not always free space, so
4019 * if we cannot make our reservations the normal way try and see if there is
4020 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4021 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004022 */
Josef Bacikd52be812013-05-29 14:54:47 -04004023static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004025 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004026
Josef Bacike70bea52011-10-11 14:18:24 -04004027 /*
4028 * 1 for the possible orphan item
4029 * 1 for the dir item
4030 * 1 for the dir index
4031 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004032 * 1 for the inode
4033 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004034 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004035}
4036
Chris Mason39279cc2007-06-12 06:35:45 -04004037static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4038{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004039 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004040 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004041 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004042 int ret;
4043
Josef Bacikd52be812013-05-29 14:54:47 -04004044 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004046 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004047
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004048 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4049 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004050
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004051 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4052 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4053 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004054 if (ret)
4055 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004056
Yan, Zhenga22285a2010-05-16 10:48:46 -04004057 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004058 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004059 if (ret)
4060 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004061 }
Josef Bacik7b128762008-07-24 12:17:14 -04004062
Tsutomu Itohb5324022011-07-19 07:27:20 +00004063out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004064 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004065 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004066 return ret;
4067}
4068
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004069int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4070 struct btrfs_root *root,
4071 struct inode *dir, u64 objectid,
4072 const char *name, int name_len)
4073{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004074 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004075 struct btrfs_path *path;
4076 struct extent_buffer *leaf;
4077 struct btrfs_dir_item *di;
4078 struct btrfs_key key;
4079 u64 index;
4080 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004081 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004082
4083 path = btrfs_alloc_path();
4084 if (!path)
4085 return -ENOMEM;
4086
Li Zefan33345d012011-04-20 10:31:50 +08004087 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004088 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004089 if (IS_ERR_OR_NULL(di)) {
4090 if (!di)
4091 ret = -ENOENT;
4092 else
4093 ret = PTR_ERR(di);
4094 goto out;
4095 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004096
4097 leaf = path->nodes[0];
4098 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4099 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4100 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004101 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004102 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004103 goto out;
4104 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004105 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004106
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004107 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4108 root->root_key.objectid, dir_ino,
4109 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004110 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004111 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004112 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004113 goto out;
4114 }
Li Zefan33345d012011-04-20 10:31:50 +08004115 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004116 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004117 if (IS_ERR_OR_NULL(di)) {
4118 if (!di)
4119 ret = -ENOENT;
4120 else
4121 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004122 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004123 goto out;
4124 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004125
4126 leaf = path->nodes[0];
4127 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004128 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004129 index = key.offset;
4130 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004131 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004132
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004133 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004134 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004135 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004136 goto out;
4137 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004138
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004139 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004140 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004141 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004142 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004143 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004144 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004145out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004146 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004147 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004148}
4149
Chris Mason39279cc2007-06-12 06:35:45 -04004150static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4151{
David Howells2b0143b2015-03-17 22:25:59 +00004152 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004153 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004154 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004155 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004156 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004157
David Sterbab3ae2442012-09-13 16:04:34 -06004158 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004159 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004160 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004161 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004162
Josef Bacikd52be812013-05-29 14:54:47 -04004163 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004164 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004165 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004166
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004167 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004168 err = btrfs_unlink_subvol(trans, root, dir,
4169 BTRFS_I(inode)->location.objectid,
4170 dentry->d_name.name,
4171 dentry->d_name.len);
4172 goto out;
4173 }
4174
Josef Bacik7b128762008-07-24 12:17:14 -04004175 err = btrfs_orphan_add(trans, inode);
4176 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004177 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004178
Filipe Manana44f714d2016-06-06 16:11:13 +01004179 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4180
Chris Mason39279cc2007-06-12 06:35:45 -04004181 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004182 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4183 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4184 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004185 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004186 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004187 /*
4188 * Propagate the last_unlink_trans value of the deleted dir to
4189 * its parent directory. This is to prevent an unrecoverable
4190 * log tree in the case we do something like this:
4191 * 1) create dir foo
4192 * 2) create snapshot under dir foo
4193 * 3) delete the snapshot
4194 * 4) rmdir foo
4195 * 5) mkdir foo
4196 * 6) fsync foo or some file inside foo
4197 */
4198 if (last_unlink_trans >= trans->transid)
4199 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4200 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004201out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004202 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004203 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004204
Chris Mason39279cc2007-06-12 06:35:45 -04004205 return err;
4206}
4207
Chris Mason28f75a02015-02-04 06:59:29 -08004208static int truncate_space_check(struct btrfs_trans_handle *trans,
4209 struct btrfs_root *root,
4210 u64 bytes_deleted)
4211{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004212 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004213 int ret;
4214
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004215 /*
4216 * This is only used to apply pressure to the enospc system, we don't
4217 * intend to use this reservation at all.
4218 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004219 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004220 bytes_deleted *= fs_info->nodesize;
4221 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004222 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004223 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004224 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004225 trans->transid,
4226 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004227 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004228 }
Chris Mason28f75a02015-02-04 06:59:29 -08004229 return ret;
4230
4231}
4232
Filipe Manana0305cd52015-10-16 12:34:25 +01004233static int truncate_inline_extent(struct inode *inode,
4234 struct btrfs_path *path,
4235 struct btrfs_key *found_key,
4236 const u64 item_end,
4237 const u64 new_size)
4238{
4239 struct extent_buffer *leaf = path->nodes[0];
4240 int slot = path->slots[0];
4241 struct btrfs_file_extent_item *fi;
4242 u32 size = (u32)(new_size - found_key->offset);
4243 struct btrfs_root *root = BTRFS_I(inode)->root;
4244
4245 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4246
4247 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4248 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004249 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004250
4251 /*
4252 * Zero out the remaining of the last page of our inline extent,
4253 * instead of directly truncating our inline extent here - that
4254 * would be much more complex (decompressing all the data, then
4255 * compressing the truncated data, which might be bigger than
4256 * the size of the inline extent, resize the extent, etc).
4257 * We release the path because to get the page we might need to
4258 * read the extent item from disk (data not in the page cache).
4259 */
4260 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304261 return btrfs_truncate_block(inode, offset, page_end - offset,
4262 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004263 }
4264
4265 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4266 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004267 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004268
4269 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4270 inode_sub_bytes(inode, item_end + 1 - new_size);
4271
4272 return 0;
4273}
4274
Chris Mason323ac952008-10-01 19:05:46 -04004275/*
Chris Mason39279cc2007-06-12 06:35:45 -04004276 * this can truncate away extent items, csum items and directory items.
4277 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004278 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004279 *
4280 * csum items that cross the new i_size are truncated to the new size
4281 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004282 *
4283 * min_type is the minimum key type to truncate down to. If set to 0, this
4284 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004285 */
Yan, Zheng80825102009-11-12 09:35:36 +00004286int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4287 struct btrfs_root *root,
4288 struct inode *inode,
4289 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004290{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004291 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004292 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004293 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004294 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004295 struct btrfs_key key;
4296 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004297 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004298 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004299 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004300 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004301 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004302 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004303 int found_extent;
4304 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004305 int pending_del_nr = 0;
4306 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004307 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004308 int ret;
4309 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004310 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004311 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004312 bool be_nice = 0;
4313 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004314 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004315
4316 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004317
Chris Mason28ed1342014-12-17 09:41:04 -08004318 /*
4319 * for non-free space inodes and ref cows, we want to back off from
4320 * time to time
4321 */
4322 if (!btrfs_is_free_space_inode(inode) &&
4323 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4324 be_nice = 1;
4325
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004326 path = btrfs_alloc_path();
4327 if (!path)
4328 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004329 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004330
Josef Bacik5dc562c2012-08-17 13:14:17 -04004331 /*
4332 * We want to drop from the next block forward in case this new size is
4333 * not block aligned since we will be keeping the last block of the
4334 * extent just the way it is.
4335 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004336 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004337 root == fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004338 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004339 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004340 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004341
Miao Xie16cdcec2011-04-22 18:12:22 +08004342 /*
4343 * This function is also used to drop the items in the log tree before
4344 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4345 * it is used to drop the loged items. So we shouldn't kill the delayed
4346 * items.
4347 */
4348 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004349 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004350
Li Zefan33345d012011-04-20 10:31:50 +08004351 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004352 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004353 key.type = (u8)-1;
4354
Chris Mason85e21ba2008-01-29 15:11:36 -05004355search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004356 /*
4357 * with a 16K leaf size and 128MB extents, you can actually queue
4358 * up a huge file in a single leaf. Most of the time that
4359 * bytes_deleted is > 0, it will be huge by the time we get here
4360 */
Byongho Leeee221842015-12-15 01:42:10 +09004361 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004362 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004363 err = -EAGAIN;
4364 goto error;
4365 }
4366 }
4367
4368
Chris Masonb9473432009-03-13 11:00:37 -04004369 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004370 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004371 if (ret < 0) {
4372 err = ret;
4373 goto out;
4374 }
Chris Masond3977122009-01-05 21:25:51 -05004375
Chris Mason85e21ba2008-01-29 15:11:36 -05004376 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004377 /* there are no items in the tree for us to truncate, we're
4378 * done
4379 */
Yan, Zheng80825102009-11-12 09:35:36 +00004380 if (path->slots[0] == 0)
4381 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004382 path->slots[0]--;
4383 }
4384
Chris Masond3977122009-01-05 21:25:51 -05004385 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004386 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004387 leaf = path->nodes[0];
4388 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004389 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004390
Li Zefan33345d012011-04-20 10:31:50 +08004391 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004392 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004393
Chris Mason85e21ba2008-01-29 15:11:36 -05004394 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004395 break;
4396
Chris Mason5f39d392007-10-15 16:14:19 -04004397 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004398 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004399 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004400 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004401 extent_type = btrfs_file_extent_type(leaf, fi);
4402 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004403 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004404 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004405 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004406 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004407 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004408 }
Yan008630c2007-11-07 13:31:09 -05004409 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004410 }
Yan, Zheng80825102009-11-12 09:35:36 +00004411 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004412 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004413 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004414 if (item_end < new_size) {
4415 /*
4416 * With NO_HOLES mode, for the following mapping
4417 *
4418 * [0-4k][hole][8k-12k]
4419 *
4420 * if truncating isize down to 6k, it ends up
4421 * isize being 8k.
4422 */
4423 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4424 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004425 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004426 }
Yan, Zheng80825102009-11-12 09:35:36 +00004427 if (found_key.offset >= new_size)
4428 del_item = 1;
4429 else
4430 del_item = 0;
4431 }
Chris Mason39279cc2007-06-12 06:35:45 -04004432 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004433 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004434 if (found_type != BTRFS_EXTENT_DATA_KEY)
4435 goto delete;
4436
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004437 if (del_item)
4438 last_size = found_key.offset;
4439 else
4440 last_size = new_size;
4441
Chris Mason179e29e2007-11-01 11:28:41 -04004442 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004443 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004444 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004445 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004446 u64 orig_num_bytes =
4447 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004448 extent_num_bytes = ALIGN(new_size -
4449 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004450 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004451 btrfs_set_file_extent_num_bytes(leaf, fi,
4452 extent_num_bytes);
4453 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004454 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004455 if (test_bit(BTRFS_ROOT_REF_COWS,
4456 &root->state) &&
4457 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004458 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004459 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004460 } else {
Chris Masondb945352007-10-15 16:15:53 -04004461 extent_num_bytes =
4462 btrfs_file_extent_disk_num_bytes(leaf,
4463 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004464 extent_offset = found_key.offset -
4465 btrfs_file_extent_offset(leaf, fi);
4466
Chris Mason39279cc2007-06-12 06:35:45 -04004467 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004468 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004469 if (extent_start != 0) {
4470 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004471 if (test_bit(BTRFS_ROOT_REF_COWS,
4472 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004473 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004474 }
4475 }
Chris Mason90692182008-02-08 13:49:28 -05004476 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004477 /*
4478 * we can't truncate inline items that have had
4479 * special encodings
4480 */
4481 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004482 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4483 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004484
4485 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004486 * Need to release path in order to truncate a
4487 * compressed extent. So delete any accumulated
4488 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004489 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004490 if (btrfs_file_extent_compression(leaf, fi) !=
4491 BTRFS_COMPRESS_NONE && pending_del_nr) {
4492 err = btrfs_del_items(trans, root, path,
4493 pending_del_slot,
4494 pending_del_nr);
4495 if (err) {
4496 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004497 err);
4498 goto error;
4499 }
4500 pending_del_nr = 0;
4501 }
4502
4503 err = truncate_inline_extent(inode, path,
4504 &found_key,
4505 item_end,
4506 new_size);
4507 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004508 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004509 goto error;
4510 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004511 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4512 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004513 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004514 }
Chris Mason39279cc2007-06-12 06:35:45 -04004515 }
Chris Mason179e29e2007-11-01 11:28:41 -04004516delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004517 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004518 if (!pending_del_nr) {
4519 /* no pending yet, add ourselves */
4520 pending_del_slot = path->slots[0];
4521 pending_del_nr = 1;
4522 } else if (pending_del_nr &&
4523 path->slots[0] + 1 == pending_del_slot) {
4524 /* hop on the pending chunk */
4525 pending_del_nr++;
4526 pending_del_slot = path->slots[0];
4527 } else {
Chris Masond3977122009-01-05 21:25:51 -05004528 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004529 }
Chris Mason39279cc2007-06-12 06:35:45 -04004530 } else {
4531 break;
4532 }
Chris Mason28f75a02015-02-04 06:59:29 -08004533 should_throttle = 0;
4534
Miao Xie27cdeb72014-04-02 19:51:05 +08004535 if (found_extent &&
4536 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004537 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004538 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004539 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004540 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004541 extent_num_bytes, 0,
4542 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004543 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004544 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004545 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4546 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004547 trans->delayed_ref_updates * 2,
4548 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004549 if (be_nice) {
4550 if (truncate_space_check(trans, root,
4551 extent_num_bytes)) {
4552 should_end = 1;
4553 }
4554 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004555 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004556 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004557 }
Chris Mason39279cc2007-06-12 06:35:45 -04004558 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004559
Yan, Zheng80825102009-11-12 09:35:36 +00004560 if (found_type == BTRFS_INODE_ITEM_KEY)
4561 break;
4562
4563 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004564 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004565 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004566 if (pending_del_nr) {
4567 ret = btrfs_del_items(trans, root, path,
4568 pending_del_slot,
4569 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004570 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004571 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004572 goto error;
4573 }
Yan, Zheng80825102009-11-12 09:35:36 +00004574 pending_del_nr = 0;
4575 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004576 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004577 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004578 unsigned long updates = trans->delayed_ref_updates;
4579 if (updates) {
4580 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004581 ret = btrfs_run_delayed_refs(trans,
4582 fs_info,
4583 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004584 if (ret && !err)
4585 err = ret;
4586 }
4587 }
Chris Mason28f75a02015-02-04 06:59:29 -08004588 /*
4589 * if we failed to refill our space rsv, bail out
4590 * and let the transaction restart
4591 */
4592 if (should_end) {
4593 err = -EAGAIN;
4594 goto error;
4595 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004596 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004597 } else {
4598 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004599 }
Chris Mason39279cc2007-06-12 06:35:45 -04004600 }
Yan, Zheng80825102009-11-12 09:35:36 +00004601out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004602 if (pending_del_nr) {
4603 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4604 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004605 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004606 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004607 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004608error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004609 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004610 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004611
Chris Mason39279cc2007-06-12 06:35:45 -04004612 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004613
Liu Bo19fd2df2016-12-01 13:01:02 -08004614 if (err == 0) {
4615 /* only inline file may have last_size != new_size */
4616 if (new_size >= fs_info->sectorsize ||
4617 new_size > fs_info->max_inline)
4618 ASSERT(last_size == new_size);
4619 }
4620
Byongho Leeee221842015-12-15 01:42:10 +09004621 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004622 unsigned long updates = trans->delayed_ref_updates;
4623 if (updates) {
4624 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004625 ret = btrfs_run_delayed_refs(trans, fs_info,
4626 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004627 if (ret && !err)
4628 err = ret;
4629 }
4630 }
Yan, Zheng80825102009-11-12 09:35:36 +00004631 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004632}
4633
Chris Masona52d9a82007-08-27 16:49:44 -04004634/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304635 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004636 * @inode - inode that we're zeroing
4637 * @from - the offset to start zeroing
4638 * @len - the length to zero, 0 to zero the entire range respective to the
4639 * offset
4640 * @front - zero up to the offset instead of from the offset on
4641 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304642 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004643 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004644 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304645int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004646 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004647{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004648 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004649 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004650 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4651 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004652 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004653 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004654 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004655 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304656 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004657 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004658 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004659 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304660 u64 block_start;
4661 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004662
Josef Bacik2aaa6652012-08-29 14:27:18 -04004663 if ((offset & (blocksize - 1)) == 0 &&
4664 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004665 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304666
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004667 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304668 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004669 if (ret)
4670 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004671
Chris Mason211c17f2008-05-15 09:13:45 -04004672again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004673 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004674 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004675 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304676 round_down(from, blocksize),
4677 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004678 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004679 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004680 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004681
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304682 block_start = round_down(from, blocksize);
4683 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004684
Chris Masona52d9a82007-08-27 16:49:44 -04004685 if (!PageUptodate(page)) {
4686 ret = btrfs_readpage(NULL, page);
4687 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004688 if (page->mapping != mapping) {
4689 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004690 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004691 goto again;
4692 }
Chris Masona52d9a82007-08-27 16:49:44 -04004693 if (!PageUptodate(page)) {
4694 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004695 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004696 }
4697 }
Chris Mason211c17f2008-05-15 09:13:45 -04004698 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004699
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304700 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004701 set_page_extent_mapped(page);
4702
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304703 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004704 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304705 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004706 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004707 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004708 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004709 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004710 btrfs_put_ordered_extent(ordered);
4711 goto again;
4712 }
4713
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304714 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004715 EXTENT_DIRTY | EXTENT_DELALLOC |
4716 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004717 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004718
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304719 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004720 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004721 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304722 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004723 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004724 goto out_unlock;
4725 }
4726
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304727 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004728 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304729 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004730 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004731 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304732 memset(kaddr + (block_start - page_offset(page)),
4733 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004734 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304735 memset(kaddr + (block_start - page_offset(page)) + offset,
4736 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004737 flush_dcache_page(page);
4738 kunmap(page);
4739 }
Chris Mason247e7432008-07-17 12:53:51 -04004740 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004741 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304742 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004743 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004744
Chris Mason89642222008-07-24 09:41:53 -04004745out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004746 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304747 btrfs_delalloc_release_space(inode, block_start,
4748 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004749 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004750 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004751out:
4752 return ret;
4753}
4754
Josef Bacik16e75492013-10-22 12:18:51 -04004755static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4756 u64 offset, u64 len)
4757{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004758 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004759 struct btrfs_trans_handle *trans;
4760 int ret;
4761
4762 /*
4763 * Still need to make sure the inode looks like it's been updated so
4764 * that any holes get logged if we fsync.
4765 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004766 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4767 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004768 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4769 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4770 return 0;
4771 }
4772
4773 /*
4774 * 1 - for the one we're dropping
4775 * 1 - for the one we're adding
4776 * 1 - for updating the inode.
4777 */
4778 trans = btrfs_start_transaction(root, 3);
4779 if (IS_ERR(trans))
4780 return PTR_ERR(trans);
4781
4782 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4783 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004784 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004785 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004786 return ret;
4787 }
4788
David Sterbaf85b7372017-01-20 14:54:07 +01004789 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4790 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004791 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004792 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004793 else
4794 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004795 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004796 return ret;
4797}
4798
Josef Bacik695a0d02011-03-04 15:46:53 -05004799/*
4800 * This function puts in dummy file extents for the area we're creating a hole
4801 * for. So if we are truncating this file to a larger size we need to insert
4802 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4803 * the range between oldsize and size
4804 */
Josef Bacika41ad392011-01-31 15:30:16 -05004805int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004806{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004807 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004808 struct btrfs_root *root = BTRFS_I(inode)->root;
4809 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004810 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004811 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004812 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004813 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4814 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004815 u64 last_byte;
4816 u64 cur_offset;
4817 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004818 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004819
Josef Bacika71754f2013-06-17 17:14:39 -04004820 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304821 * If our size started in the middle of a block we need to zero out the
4822 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004823 * expose stale data.
4824 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304825 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004826 if (err)
4827 return err;
4828
Yan Zheng9036c102008-10-30 14:19:41 -04004829 if (size <= hole_start)
4830 return 0;
4831
Yan Zheng9036c102008-10-30 14:19:41 -04004832 while (1) {
4833 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004834
David Sterbaff13db42015-12-03 14:30:40 +01004835 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004836 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004837 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4838 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004839 if (!ordered)
4840 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004841 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4842 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004843 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004844 btrfs_put_ordered_extent(ordered);
4845 }
4846
Yan Zheng9036c102008-10-30 14:19:41 -04004847 cur_offset = hole_start;
4848 while (1) {
4849 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4850 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004851 if (IS_ERR(em)) {
4852 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004853 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004854 break;
4855 }
Yan Zheng9036c102008-10-30 14:19:41 -04004856 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004857 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004858 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004859 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004860 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004861
Josef Bacik16e75492013-10-22 12:18:51 -04004862 err = maybe_insert_hole(root, inode, cur_offset,
4863 hole_size);
4864 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004865 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004866 btrfs_drop_extent_cache(inode, cur_offset,
4867 cur_offset + hole_size - 1, 0);
4868 hole_em = alloc_extent_map();
4869 if (!hole_em) {
4870 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4871 &BTRFS_I(inode)->runtime_flags);
4872 goto next;
4873 }
4874 hole_em->start = cur_offset;
4875 hole_em->len = hole_size;
4876 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004877
Josef Bacik5dc562c2012-08-17 13:14:17 -04004878 hole_em->block_start = EXTENT_MAP_HOLE;
4879 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004880 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004881 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004882 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004883 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004884 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004885
4886 while (1) {
4887 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004888 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004889 write_unlock(&em_tree->lock);
4890 if (err != -EEXIST)
4891 break;
4892 btrfs_drop_extent_cache(inode, cur_offset,
4893 cur_offset +
4894 hole_size - 1, 0);
4895 }
4896 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004897 }
Josef Bacik16e75492013-10-22 12:18:51 -04004898next:
Yan Zheng9036c102008-10-30 14:19:41 -04004899 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004900 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004901 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004902 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004903 break;
4904 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004905 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004906 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4907 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004908 return err;
4909}
4910
Eric Sandeen3972f262013-01-12 02:57:22 +00004911static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004912{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004913 struct btrfs_root *root = BTRFS_I(inode)->root;
4914 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004915 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004916 loff_t newsize = attr->ia_size;
4917 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004918 int ret;
4919
Eric Sandeen3972f262013-01-12 02:57:22 +00004920 /*
4921 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4922 * special case where we need to update the times despite not having
4923 * these flags set. For all other operations the VFS set these flags
4924 * explicitly if it wants a timestamp update.
4925 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004926 if (newsize != oldsize) {
4927 inode_inc_iversion(inode);
4928 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4929 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004930 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004931 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004932
Josef Bacika41ad392011-01-31 15:30:16 -05004933 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004934 /*
4935 * Don't do an expanding truncate while snapshoting is ongoing.
4936 * This is to ensure the snapshot captures a fully consistent
4937 * state of this file - if the snapshot captures this expanding
4938 * truncation, it must capture all writes that happened before
4939 * this truncation.
4940 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004941 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004942 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004943 if (ret) {
4944 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004945 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004946 }
Yan, Zheng80825102009-11-12 09:35:36 +00004947
Miao Xief4a2f4c2011-12-14 20:12:01 -05004948 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004949 if (IS_ERR(trans)) {
4950 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004951 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004952 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004953
4954 i_size_write(inode, newsize);
4955 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304956 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004957 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004958 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004959 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004960 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004961
Josef Bacika41ad392011-01-31 15:30:16 -05004962 /*
4963 * We're truncating a file that used to have good data down to
4964 * zero. Make sure it gets into the ordered flush list so that
4965 * any new writes get down to disk quickly.
4966 */
4967 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004968 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4969 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004970
Josef Bacikf3fe8202013-01-07 17:03:21 -05004971 /*
4972 * 1 for the orphan item we're going to add
4973 * 1 for the orphan item deletion.
4974 */
4975 trans = btrfs_start_transaction(root, 2);
4976 if (IS_ERR(trans))
4977 return PTR_ERR(trans);
4978
4979 /*
4980 * We need to do this in case we fail at _any_ point during the
4981 * actual truncate. Once we do the truncate_setsize we could
4982 * invalidate pages which forces any outstanding ordered io to
4983 * be instantly completed which will give us extents that need
4984 * to be truncated. If we fail to get an orphan inode down we
4985 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004986 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05004987 * will be consistent.
4988 */
4989 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004990 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004991 if (ret)
4992 return ret;
4993
Josef Bacika41ad392011-01-31 15:30:16 -05004994 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4995 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004996
4997 /* Disable nonlocked read DIO to avoid the end less truncate */
4998 btrfs_inode_block_unlocked_dio(inode);
4999 inode_dio_wait(inode);
5000 btrfs_inode_resume_unlocked_dio(inode);
5001
Josef Bacika41ad392011-01-31 15:30:16 -05005002 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005003 if (ret && inode->i_nlink) {
5004 int err;
5005
Liu Bo19fd2df2016-12-01 13:01:02 -08005006 /* To get a stable disk_i_size */
5007 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5008 if (err) {
5009 btrfs_orphan_del(NULL, inode);
5010 return err;
5011 }
5012
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005013 /*
5014 * failed to truncate, disk_i_size is only adjusted down
5015 * as we remove extents, so it should represent the true
5016 * size of the inode, so reset the in memory size and
5017 * delete our orphan entry.
5018 */
5019 trans = btrfs_join_transaction(root);
5020 if (IS_ERR(trans)) {
5021 btrfs_orphan_del(NULL, inode);
5022 return ret;
5023 }
5024 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5025 err = btrfs_orphan_del(trans, inode);
5026 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005027 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005028 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005029 }
Yan, Zheng80825102009-11-12 09:35:36 +00005030 }
5031
Josef Bacika41ad392011-01-31 15:30:16 -05005032 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005033}
5034
Chris Mason39279cc2007-06-12 06:35:45 -04005035static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5036{
David Howells2b0143b2015-03-17 22:25:59 +00005037 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005038 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005039 int err;
5040
Li Zefanb83cc962010-12-20 16:04:08 +08005041 if (btrfs_root_readonly(root))
5042 return -EROFS;
5043
Jan Kara31051c82016-05-26 16:55:18 +02005044 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005045 if (err)
5046 return err;
5047
Chris Mason5a3f23d2009-03-31 13:27:11 -04005048 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005049 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005050 if (err)
5051 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005052 }
Yan Zheng9036c102008-10-30 14:19:41 -04005053
Christoph Hellwig10257742010-06-04 11:30:02 +02005054 if (attr->ia_valid) {
5055 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005056 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005057 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005058
Josef Bacik22c44fe2011-11-30 10:45:38 -05005059 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005060 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005061 }
5062
Chris Mason39279cc2007-06-12 06:35:45 -04005063 return err;
5064}
Chris Mason61295eb2008-01-14 16:24:38 -05005065
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005066/*
5067 * While truncating the inode pages during eviction, we get the VFS calling
5068 * btrfs_invalidatepage() against each page of the inode. This is slow because
5069 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5070 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5071 * extent_state structures over and over, wasting lots of time.
5072 *
5073 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5074 * those expensive operations on a per page basis and do only the ordered io
5075 * finishing, while we release here the extent_map and extent_state structures,
5076 * without the excessive merging and splitting.
5077 */
5078static void evict_inode_truncate_pages(struct inode *inode)
5079{
5080 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5081 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5082 struct rb_node *node;
5083
5084 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005085 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005086
5087 write_lock(&map_tree->lock);
5088 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5089 struct extent_map *em;
5090
5091 node = rb_first(&map_tree->map);
5092 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005093 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5094 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005095 remove_extent_mapping(map_tree, em);
5096 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005097 if (need_resched()) {
5098 write_unlock(&map_tree->lock);
5099 cond_resched();
5100 write_lock(&map_tree->lock);
5101 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005102 }
5103 write_unlock(&map_tree->lock);
5104
Filipe Manana6ca07092015-05-26 00:55:42 +01005105 /*
5106 * Keep looping until we have no more ranges in the io tree.
5107 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005108 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5109 * still in progress (unlocked the pages in the bio but did not yet
5110 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005111 * ranges can still be locked and eviction started because before
5112 * submitting those bios, which are executed by a separate task (work
5113 * queue kthread), inode references (inode->i_count) were not taken
5114 * (which would be dropped in the end io callback of each bio).
5115 * Therefore here we effectively end up waiting for those bios and
5116 * anyone else holding locked ranges without having bumped the inode's
5117 * reference count - if we don't do it, when they access the inode's
5118 * io_tree to unlock a range it may be too late, leading to an
5119 * use-after-free issue.
5120 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005121 spin_lock(&io_tree->lock);
5122 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5123 struct extent_state *state;
5124 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005125 u64 start;
5126 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005127
5128 node = rb_first(&io_tree->state);
5129 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005130 start = state->start;
5131 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005132 spin_unlock(&io_tree->lock);
5133
David Sterbaff13db42015-12-03 14:30:40 +01005134 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005135
5136 /*
5137 * If still has DELALLOC flag, the extent didn't reach disk,
5138 * and its reserved space won't be freed by delayed_ref.
5139 * So we need to free its reserved space here.
5140 * (Refer to comment in btrfs_invalidatepage, case 2)
5141 *
5142 * Note, end is the bytenr of last byte, so we need + 1 here.
5143 */
5144 if (state->state & EXTENT_DELALLOC)
5145 btrfs_qgroup_free_data(inode, start, end - start + 1);
5146
Filipe Manana6ca07092015-05-26 00:55:42 +01005147 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005148 EXTENT_LOCKED | EXTENT_DIRTY |
5149 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5150 EXTENT_DEFRAG, 1, 1,
5151 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005152
Filipe Manana7064dd52014-08-08 02:47:05 +01005153 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005154 spin_lock(&io_tree->lock);
5155 }
5156 spin_unlock(&io_tree->lock);
5157}
5158
Al Virobd555972010-06-07 11:35:40 -04005159void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005160{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005161 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005162 struct btrfs_trans_handle *trans;
5163 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005164 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005165 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005166 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005167 int ret;
5168
liubo1abe9b82011-03-24 11:18:59 +00005169 trace_btrfs_inode_evict(inode);
5170
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005171 if (!root) {
5172 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5173 return;
5174 }
5175
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005176 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005177
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005178 evict_inode_truncate_pages(inode);
5179
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005180 if (inode->i_nlink &&
5181 ((btrfs_root_refs(&root->root_item) != 0 &&
5182 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5183 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005184 goto no_delete;
5185
Chris Mason39279cc2007-06-12 06:35:45 -04005186 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005187 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005188 goto no_delete;
5189 }
Al Virobd555972010-06-07 11:35:40 -04005190 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005191 if (!special_file(inode->i_mode))
5192 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005193
Miao Xief6124962014-09-12 18:44:04 +08005194 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5195
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005196 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005197 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005198 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005199 goto no_delete;
5200 }
5201
Yan, Zheng76dda932009-09-21 16:00:26 -04005202 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005203 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5204 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005205 goto no_delete;
5206 }
5207
Nikolay Borisovaa790212017-01-10 20:35:40 +02005208 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005209 if (ret) {
5210 btrfs_orphan_del(NULL, inode);
5211 goto no_delete;
5212 }
5213
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005214 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005215 if (!rsv) {
5216 btrfs_orphan_del(NULL, inode);
5217 goto no_delete;
5218 }
Josef Bacik4a338542011-08-29 11:01:31 -04005219 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005220 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005221 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005222
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005223 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005224
Josef Bacik4289a662011-08-05 13:22:24 -04005225 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005226 * This is a bit simpler than btrfs_truncate since we've already
5227 * reserved our space for our orphan item in the unlink, so we just
5228 * need to reserve some slack space in case we add bytes and update
5229 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005230 */
Yan, Zheng80825102009-11-12 09:35:36 +00005231 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005232 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5233 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005234
Josef Bacik726c35f2011-09-26 15:46:06 -04005235 /*
5236 * Try and steal from the global reserve since we will
5237 * likely not use this space anyway, we want to try as
5238 * hard as possible to get this to work.
5239 */
5240 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005241 steal_from_global++;
5242 else
5243 steal_from_global = 0;
5244 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005245
Josef Bacik3bce8762015-02-24 12:35:51 -08005246 /*
5247 * steal_from_global == 0: we reserved stuff, hooray!
5248 * steal_from_global == 1: we didn't reserve stuff, boo!
5249 * steal_from_global == 2: we've committed, still not a lot of
5250 * room but maybe we'll have room in the global reserve this
5251 * time.
5252 * steal_from_global == 3: abandon all hope!
5253 */
5254 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005255 btrfs_warn(fs_info,
5256 "Could not get space for a delete, will truncate on mount %d",
5257 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005258 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005259 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005260 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005261 }
5262
Miao Xie0e8c36a2012-12-19 06:59:51 +00005263 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005264 if (IS_ERR(trans)) {
5265 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005266 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005267 goto no_delete;
5268 }
5269
Josef Bacik3bce8762015-02-24 12:35:51 -08005270 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005271 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005272 * sure there is room to do it, if not we need to commit and try
5273 * again.
5274 */
5275 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005276 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005277 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005278 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005279 else
5280 ret = -ENOSPC;
5281 }
5282
5283 /*
5284 * Couldn't steal from the global reserve, we have too much
5285 * pending stuff built up, commit the transaction and try it
5286 * again.
5287 */
5288 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005289 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005290 if (ret) {
5291 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005292 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005293 goto no_delete;
5294 }
5295 continue;
5296 } else {
5297 steal_from_global = 0;
5298 }
5299
Josef Bacik4289a662011-08-05 13:22:24 -04005300 trans->block_rsv = rsv;
5301
Yan, Zhengd68fc572010-05-16 10:49:58 -04005302 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005303 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005304 break;
5305
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005306 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005307 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005308 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005309 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005310 }
5311
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005312 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005313
Josef Bacik4ef31a42013-08-13 14:10:08 -04005314 /*
5315 * Errors here aren't a big deal, it just means we leave orphan items
5316 * in the tree. They will be cleaned up on the next mount.
5317 */
Yan, Zheng80825102009-11-12 09:35:36 +00005318 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005319 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005320 btrfs_orphan_del(trans, inode);
5321 } else {
5322 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005323 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005324
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005325 trans->block_rsv = &fs_info->trans_block_rsv;
5326 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005327 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005328 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005329
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005330 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005331 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005332no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005333 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005334 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005335}
5336
5337/*
5338 * this returns the key found in the dir entry in the location pointer.
5339 * If no dir entries were found, location->objectid is 0.
5340 */
5341static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5342 struct btrfs_key *location)
5343{
5344 const char *name = dentry->d_name.name;
5345 int namelen = dentry->d_name.len;
5346 struct btrfs_dir_item *di;
5347 struct btrfs_path *path;
5348 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005349 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005350
5351 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005352 if (!path)
5353 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005354
David Sterbaf85b7372017-01-20 14:54:07 +01005355 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5356 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005357 if (IS_ERR(di))
5358 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005359
David Sterbac7040052011-04-19 18:00:01 +02005360 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005361 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005362
Chris Mason5f39d392007-10-15 16:14:19 -04005363 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005364out:
Chris Mason39279cc2007-06-12 06:35:45 -04005365 btrfs_free_path(path);
5366 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005367out_err:
5368 location->objectid = 0;
5369 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005370}
5371
5372/*
5373 * when we hit a tree root in a directory, the btrfs part of the inode
5374 * needs to be changed to reflect the root directory of the tree root. This
5375 * is kind of like crossing a mount point.
5376 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005377static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005378 struct inode *dir,
5379 struct dentry *dentry,
5380 struct btrfs_key *location,
5381 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005382{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005383 struct btrfs_path *path;
5384 struct btrfs_root *new_root;
5385 struct btrfs_root_ref *ref;
5386 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005387 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005388 int ret;
5389 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005390
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005391 path = btrfs_alloc_path();
5392 if (!path) {
5393 err = -ENOMEM;
5394 goto out;
5395 }
Chris Mason39279cc2007-06-12 06:35:45 -04005396
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005397 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005398 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5399 key.type = BTRFS_ROOT_REF_KEY;
5400 key.offset = location->objectid;
5401
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005402 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005403 if (ret) {
5404 if (ret < 0)
5405 err = ret;
5406 goto out;
5407 }
Chris Mason39279cc2007-06-12 06:35:45 -04005408
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005409 leaf = path->nodes[0];
5410 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005411 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005412 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5413 goto out;
5414
5415 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5416 (unsigned long)(ref + 1),
5417 dentry->d_name.len);
5418 if (ret)
5419 goto out;
5420
David Sterbab3b4aa72011-04-21 01:20:15 +02005421 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005422
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005423 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005424 if (IS_ERR(new_root)) {
5425 err = PTR_ERR(new_root);
5426 goto out;
5427 }
5428
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005429 *sub_root = new_root;
5430 location->objectid = btrfs_root_dirid(&new_root->root_item);
5431 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005432 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005433 err = 0;
5434out:
5435 btrfs_free_path(path);
5436 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005437}
5438
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005439static void inode_tree_add(struct inode *inode)
5440{
5441 struct btrfs_root *root = BTRFS_I(inode)->root;
5442 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005443 struct rb_node **p;
5444 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005445 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005446 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005447
Al Viro1d3382cb2010-10-23 15:19:20 -04005448 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005449 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005450 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005451 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005452 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005453 while (*p) {
5454 parent = *p;
5455 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5456
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005457 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005458 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005459 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005460 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005461 else {
5462 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005463 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005464 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005465 RB_CLEAR_NODE(parent);
5466 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005467 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005468 }
5469 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005470 rb_link_node(new, parent, p);
5471 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005472 spin_unlock(&root->inode_lock);
5473}
5474
5475static void inode_tree_del(struct inode *inode)
5476{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005477 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005478 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005479 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005480
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005481 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005482 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005483 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005484 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005485 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005486 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005487 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005488
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005489 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005490 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005491 spin_lock(&root->inode_lock);
5492 empty = RB_EMPTY_ROOT(&root->inode_tree);
5493 spin_unlock(&root->inode_lock);
5494 if (empty)
5495 btrfs_add_dead_root(root);
5496 }
5497}
5498
Jeff Mahoney143bede2012-03-01 14:56:26 +01005499void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005500{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005501 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005502 struct rb_node *node;
5503 struct rb_node *prev;
5504 struct btrfs_inode *entry;
5505 struct inode *inode;
5506 u64 objectid = 0;
5507
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005508 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005509 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005510
5511 spin_lock(&root->inode_lock);
5512again:
5513 node = root->inode_tree.rb_node;
5514 prev = NULL;
5515 while (node) {
5516 prev = node;
5517 entry = rb_entry(node, struct btrfs_inode, rb_node);
5518
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005519 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005520 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005521 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005522 node = node->rb_right;
5523 else
5524 break;
5525 }
5526 if (!node) {
5527 while (prev) {
5528 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005529 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005530 node = prev;
5531 break;
5532 }
5533 prev = rb_next(prev);
5534 }
5535 }
5536 while (node) {
5537 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005538 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005539 inode = igrab(&entry->vfs_inode);
5540 if (inode) {
5541 spin_unlock(&root->inode_lock);
5542 if (atomic_read(&inode->i_count) > 1)
5543 d_prune_aliases(inode);
5544 /*
Al Viro45321ac2010-06-07 13:43:19 -04005545 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005546 * the inode cache when its usage count
5547 * hits zero.
5548 */
5549 iput(inode);
5550 cond_resched();
5551 spin_lock(&root->inode_lock);
5552 goto again;
5553 }
5554
5555 if (cond_resched_lock(&root->inode_lock))
5556 goto again;
5557
5558 node = rb_next(node);
5559 }
5560 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561}
5562
Chris Masone02119d2008-09-05 16:13:11 -04005563static int btrfs_init_locked_inode(struct inode *inode, void *p)
5564{
5565 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005566 inode->i_ino = args->location->objectid;
5567 memcpy(&BTRFS_I(inode)->location, args->location,
5568 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005569 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005570 return 0;
5571}
5572
5573static int btrfs_find_actor(struct inode *inode, void *opaque)
5574{
5575 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005576 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005577 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005578}
5579
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005581 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005582 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005583{
5584 struct inode *inode;
5585 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005586 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005587
Chris Mason90d3e592014-01-09 17:28:00 -08005588 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005589 args.root = root;
5590
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005591 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005592 btrfs_init_locked_inode,
5593 (void *)&args);
5594 return inode;
5595}
5596
Balaji Rao1a54ef82008-07-21 02:01:04 +05305597/* Get an inode object given its location and corresponding root.
5598 * Returns in *is_new if the inode was read from disk
5599 */
5600struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005601 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305602{
5603 struct inode *inode;
5604
Chris Mason90d3e592014-01-09 17:28:00 -08005605 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305606 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305608
5609 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005610 int ret;
5611
5612 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005613 if (!is_bad_inode(inode)) {
5614 inode_tree_add(inode);
5615 unlock_new_inode(inode);
5616 if (new)
5617 *new = 1;
5618 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005619 unlock_new_inode(inode);
5620 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005621 ASSERT(ret < 0);
5622 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005623 }
5624 }
5625
Balaji Rao1a54ef82008-07-21 02:01:04 +05305626 return inode;
5627}
5628
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005629static struct inode *new_simple_dir(struct super_block *s,
5630 struct btrfs_key *key,
5631 struct btrfs_root *root)
5632{
5633 struct inode *inode = new_inode(s);
5634
5635 if (!inode)
5636 return ERR_PTR(-ENOMEM);
5637
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638 BTRFS_I(inode)->root = root;
5639 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005640 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005641
5642 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005643 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005644 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005645 inode->i_fop = &simple_dir_operations;
5646 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005647 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305648 inode->i_atime = inode->i_mtime;
5649 inode->i_ctime = inode->i_mtime;
5650 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005651
5652 return inode;
5653}
5654
Chris Mason3de45862008-11-17 21:02:50 -05005655struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005656{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005657 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005658 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005659 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005660 struct btrfs_root *sub_root = root;
5661 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005662 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005663 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005664
5665 if (dentry->d_name.len > BTRFS_NAME_LEN)
5666 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005667
Jeff Layton39e3c952012-11-28 11:30:53 -05005668 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005669 if (ret < 0)
5670 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005671
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005672 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005673 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005674
5675 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005676 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005677 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005678 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005679
5680 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5681
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005682 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005683 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005684 &location, &sub_root);
5685 if (ret < 0) {
5686 if (ret != -ENOENT)
5687 inode = ERR_PTR(ret);
5688 else
5689 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5690 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005691 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005692 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005693 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005694
Julia Lawall34d19ba2011-01-24 19:55:19 +00005695 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005696 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005697 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005698 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005699 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005700 if (ret) {
5701 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005702 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005703 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005704 }
5705
Chris Mason3de45862008-11-17 21:02:50 -05005706 return inode;
5707}
5708
Nick Pigginfe15ce42011-01-07 17:49:23 +11005709static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005710{
5711 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005712 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005713
Li Zefan848cce02012-02-21 17:04:28 +08005714 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005715 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005716
Li Zefan848cce02012-02-21 17:04:28 +08005717 if (inode) {
5718 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005719 if (btrfs_root_refs(&root->root_item) == 0)
5720 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005721
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005722 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005723 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005724 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005725 return 0;
5726}
5727
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005728static void btrfs_dentry_release(struct dentry *dentry)
5729{
Daeseok Youn944a4512014-04-14 15:37:02 +09005730 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005731}
5732
Chris Mason3de45862008-11-17 21:02:50 -05005733static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005734 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005735{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005736 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005737
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005738 inode = btrfs_lookup_dentry(dir, dentry);
5739 if (IS_ERR(inode)) {
5740 if (PTR_ERR(inode) == -ENOENT)
5741 inode = NULL;
5742 else
5743 return ERR_CAST(inode);
5744 }
5745
Al Viro41d28bc2014-10-12 22:24:21 -04005746 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005747}
5748
Miao Xie16cdcec2011-04-22 18:12:22 +08005749unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005750 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5751};
5752
Al Viro9cdda8d2013-05-22 16:48:09 -04005753static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005754{
Al Viro9cdda8d2013-05-22 16:48:09 -04005755 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005756 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005757 struct btrfs_root *root = BTRFS_I(inode)->root;
5758 struct btrfs_item *item;
5759 struct btrfs_dir_item *di;
5760 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005761 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005762 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005763 struct list_head ins_list;
5764 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005765 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005766 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005767 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005768 unsigned char d_type;
5769 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005770 char tmp_name[32];
5771 char *name_ptr;
5772 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005773 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005774 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005775
Al Viro9cdda8d2013-05-22 16:48:09 -04005776 if (!dir_emit_dots(file, ctx))
5777 return 0;
5778
David Woodhouse49593bf2008-08-17 17:08:36 +01005779 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005780 if (!path)
5781 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005782
David Sterbae4058b52015-11-27 16:31:35 +01005783 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005784
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005785 INIT_LIST_HEAD(&ins_list);
5786 INIT_LIST_HEAD(&del_list);
5787 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005788
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005789 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005790 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005791 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005792
Chris Mason39279cc2007-06-12 06:35:45 -04005793 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5794 if (ret < 0)
5795 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005796
5797 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005798 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005799 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005800 if (slot >= btrfs_header_nritems(leaf)) {
5801 ret = btrfs_next_leaf(root, path);
5802 if (ret < 0)
5803 goto err;
5804 else if (ret > 0)
5805 break;
5806 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005807 }
Chris Mason3de45862008-11-17 21:02:50 -05005808
Ross Kirkdd3cc162013-09-16 15:58:09 +01005809 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005810 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5811
5812 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005813 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005814 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005815 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005816 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005817 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005818 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005819 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005820
Al Viro9cdda8d2013-05-22 16:48:09 -04005821 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005822
Chris Mason39279cc2007-06-12 06:35:45 -04005823 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005824 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005825 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005826
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005827 name_len = btrfs_dir_name_len(leaf, di);
5828 if (name_len <= sizeof(tmp_name)) {
5829 name_ptr = tmp_name;
5830 } else {
5831 name_ptr = kmalloc(name_len, GFP_KERNEL);
5832 if (!name_ptr) {
5833 ret = -ENOMEM;
5834 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005835 }
Chris Mason39279cc2007-06-12 06:35:45 -04005836 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005837 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5838 name_len);
5839
5840 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5841 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5842
5843 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5844 d_type);
5845
5846 if (name_ptr != tmp_name)
5847 kfree(name_ptr);
5848
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005849 if (over)
5850 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005851 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005852next:
5853 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005854 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005855
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005856 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005857 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005858 goto nopos;
5859
Zach Browndb62efb2013-07-11 16:19:42 -07005860 /*
5861 * Stop new entries from being returned after we return the last
5862 * entry.
5863 *
5864 * New directory entries are assigned a strictly increasing
5865 * offset. This means that new entries created during readdir
5866 * are *guaranteed* to be seen in the future by that readdir.
5867 * This has broken buggy programs which operate on names as
5868 * they're returned by readdir. Until we re-use freed offsets
5869 * we have this hack to stop new entries from being returned
5870 * under the assumption that they'll never reach this huge
5871 * offset.
5872 *
5873 * This is being careful not to overflow 32bit loff_t unless the
5874 * last entry requires it because doing so has broken 32bit apps
5875 * in the past.
5876 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005877 if (ctx->pos >= INT_MAX)
5878 ctx->pos = LLONG_MAX;
5879 else
5880 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005881nopos:
5882 ret = 0;
5883err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005884 if (put)
5885 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005886 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005887 return ret;
5888}
5889
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005890int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005891{
5892 struct btrfs_root *root = BTRFS_I(inode)->root;
5893 struct btrfs_trans_handle *trans;
5894 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005895 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005896
Josef Bacik72ac3c02012-05-23 14:13:11 -04005897 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005898 return 0;
5899
Liu Bo83eea1f2012-07-10 05:28:39 -06005900 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005901 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005902
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005903 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005904 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005905 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005906 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005907 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005908 if (IS_ERR(trans))
5909 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005910 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005911 }
5912 return ret;
5913}
5914
5915/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005916 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005917 * inode changes. But, it is most likely to find the inode in cache.
5918 * FIXME, needs more benchmarking...there are no reasons other than performance
5919 * to keep or drop this code.
5920 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005921static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005922{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005923 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005924 struct btrfs_root *root = BTRFS_I(inode)->root;
5925 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005926 int ret;
5927
Josef Bacik72ac3c02012-05-23 14:13:11 -04005928 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005929 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005930
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005931 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005932 if (IS_ERR(trans))
5933 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005934
5935 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005936 if (ret && ret == -ENOSPC) {
5937 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005938 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005939 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005940 if (IS_ERR(trans))
5941 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005942
Chris Mason94b60442010-05-26 11:02:00 -04005943 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005944 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005945 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005946 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005947 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005948
5949 return ret;
5950}
5951
5952/*
5953 * This is a copy of file_update_time. We need this so we can return error on
5954 * ENOSPC for updating the inode in the case of file write and mmap writes.
5955 */
Josef Bacike41f9412012-03-26 09:46:47 -04005956static int btrfs_update_time(struct inode *inode, struct timespec *now,
5957 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005958{
Alexander Block2bc5565282012-06-15 09:49:33 +02005959 struct btrfs_root *root = BTRFS_I(inode)->root;
5960
5961 if (btrfs_root_readonly(root))
5962 return -EROFS;
5963
Josef Bacike41f9412012-03-26 09:46:47 -04005964 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005965 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005966 if (flags & S_CTIME)
5967 inode->i_ctime = *now;
5968 if (flags & S_MTIME)
5969 inode->i_mtime = *now;
5970 if (flags & S_ATIME)
5971 inode->i_atime = *now;
5972 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005973}
5974
Chris Masond352ac62008-09-29 15:18:18 -04005975/*
5976 * find the highest existing sequence number in a directory
5977 * and then set the in-memory index_cnt variable to reflect
5978 * free sequence numbers
5979 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005980static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005981{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005982 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005983 struct btrfs_key key, found_key;
5984 struct btrfs_path *path;
5985 struct extent_buffer *leaf;
5986 int ret;
5987
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005988 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005989 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005990 key.offset = (u64)-1;
5991
5992 path = btrfs_alloc_path();
5993 if (!path)
5994 return -ENOMEM;
5995
5996 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5997 if (ret < 0)
5998 goto out;
5999 /* FIXME: we should be able to handle this */
6000 if (ret == 0)
6001 goto out;
6002 ret = 0;
6003
6004 /*
6005 * MAGIC NUMBER EXPLANATION:
6006 * since we search a directory based on f_pos we have to start at 2
6007 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6008 * else has to start at 2
6009 */
6010 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006011 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006012 goto out;
6013 }
6014
6015 path->slots[0]--;
6016
6017 leaf = path->nodes[0];
6018 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6019
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006020 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006021 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006022 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006023 goto out;
6024 }
6025
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006026 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006027out:
6028 btrfs_free_path(path);
6029 return ret;
6030}
6031
Chris Masond352ac62008-09-29 15:18:18 -04006032/*
6033 * helper to find a free sequence number in a given directory. This current
6034 * code is very simple, later versions will do smarter things in the btree
6035 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006036int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006037{
6038 int ret = 0;
6039
Nikolay Borisov877574e2017-02-20 13:50:33 +02006040 if (dir->index_cnt == (u64)-1) {
6041 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006042 if (ret) {
Nikolay Borisov877574e2017-02-20 13:50:33 +02006043 ret = btrfs_set_inode_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006044 if (ret)
6045 return ret;
6046 }
Josef Bacikaec74772008-07-24 12:12:38 -04006047 }
6048
Nikolay Borisov877574e2017-02-20 13:50:33 +02006049 *index = dir->index_cnt;
6050 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006051
6052 return ret;
6053}
6054
Chris Masonb0d5d102014-09-08 13:08:51 -07006055static int btrfs_insert_inode_locked(struct inode *inode)
6056{
6057 struct btrfs_iget_args args;
6058 args.location = &BTRFS_I(inode)->location;
6059 args.root = BTRFS_I(inode)->root;
6060
6061 return insert_inode_locked4(inode,
6062 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6063 btrfs_find_actor, &args);
6064}
6065
Chris Mason39279cc2007-06-12 06:35:45 -04006066static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6067 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006068 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006069 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006070 u64 ref_objectid, u64 objectid,
6071 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006072{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006073 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006074 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006075 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006076 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006077 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006078 struct btrfs_inode_ref *ref;
6079 struct btrfs_key key[2];
6080 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006081 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006082 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006083 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006084
Chris Mason5f39d392007-10-15 16:14:19 -04006085 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006086 if (!path)
6087 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006088
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006089 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006090 if (!inode) {
6091 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006092 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006093 }
Chris Mason39279cc2007-06-12 06:35:45 -04006094
Li Zefan581bb052011-04-20 10:06:11 +08006095 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006096 * O_TMPFILE, set link count to 0, so that after this point,
6097 * we fill in an inode item with the correct link count.
6098 */
6099 if (!name)
6100 set_nlink(inode, 0);
6101
6102 /*
Li Zefan581bb052011-04-20 10:06:11 +08006103 * we have to initialize this early, so we can reclaim the inode
6104 * number if we fail afterwards in this function.
6105 */
6106 inode->i_ino = objectid;
6107
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006108 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006109 trace_btrfs_inode_request(dir);
6110
Nikolay Borisov877574e2017-02-20 13:50:33 +02006111 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006112 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006113 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006114 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006115 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006116 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006117 } else if (dir) {
6118 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006119 }
6120 /*
6121 * index_cnt is ignored for everything but a dir,
6122 * btrfs_get_inode_index_count has an explanation for the magic
6123 * number
6124 */
6125 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006126 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006127 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006128 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006129 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006130
Josef Bacik5dc562c2012-08-17 13:14:17 -04006131 /*
6132 * We could have gotten an inode number from somebody who was fsynced
6133 * and then removed in this same transaction, so let's just set full
6134 * sync since it will be a full sync anyway and this will blow away the
6135 * old info in the log.
6136 */
6137 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6138
Chris Mason9c583092008-01-29 15:15:18 -05006139 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006140 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006141 key[0].offset = 0;
6142
Chris Mason9c583092008-01-29 15:15:18 -05006143 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006144
6145 if (name) {
6146 /*
6147 * Start new inodes with an inode_ref. This is slightly more
6148 * efficient for small numbers of hard links since they will
6149 * be packed into one item. Extended refs will kick in if we
6150 * add more hard links than can fit in the ref item.
6151 */
6152 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006153 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006154 key[1].offset = ref_objectid;
6155
6156 sizes[1] = name_len + sizeof(*ref);
6157 }
Chris Mason9c583092008-01-29 15:15:18 -05006158
Chris Masonb0d5d102014-09-08 13:08:51 -07006159 location = &BTRFS_I(inode)->location;
6160 location->objectid = objectid;
6161 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006162 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006163
6164 ret = btrfs_insert_inode_locked(inode);
6165 if (ret < 0)
6166 goto fail;
6167
Chris Masonb9473432009-03-13 11:00:37 -04006168 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006169 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006170 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006171 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006172
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006173 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006174 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306175
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006176 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306177 inode->i_atime = inode->i_mtime;
6178 inode->i_ctime = inode->i_mtime;
6179 BTRFS_I(inode)->i_otime = inode->i_mtime;
6180
Chris Mason5f39d392007-10-15 16:14:19 -04006181 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6182 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006183 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006184 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006185 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006186
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006187 if (name) {
6188 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6189 struct btrfs_inode_ref);
6190 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6191 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6192 ptr = (unsigned long)(ref + 1);
6193 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6194 }
Chris Mason9c583092008-01-29 15:15:18 -05006195
Chris Mason5f39d392007-10-15 16:14:19 -04006196 btrfs_mark_buffer_dirty(path->nodes[0]);
6197 btrfs_free_path(path);
6198
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006199 btrfs_inherit_iflags(inode, dir);
6200
Al Viro569254b2011-07-24 17:08:40 -04006201 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006202 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006203 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006204 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006205 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6206 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006207 }
6208
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006209 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006210
6211 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006212 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006213
Alexander Block8ea05e32012-07-25 17:35:53 +02006214 btrfs_update_root_times(trans, root);
6215
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006216 ret = btrfs_inode_inherit_props(trans, inode, dir);
6217 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006218 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006219 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006220 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006221
Chris Mason39279cc2007-06-12 06:35:45 -04006222 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006223
6224fail_unlock:
6225 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006226fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006227 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006228 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006229 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006230 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006231 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006232}
6233
6234static inline u8 btrfs_inode_type(struct inode *inode)
6235{
6236 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6237}
6238
Chris Masond352ac62008-09-29 15:18:18 -04006239/*
6240 * utility function to add 'inode' into 'parent_inode' with
6241 * a give name and a given sequence number.
6242 * if 'add_backref' is true, also insert a backref from the
6243 * inode to the parent directory.
6244 */
Chris Masone02119d2008-09-05 16:13:11 -04006245int btrfs_add_link(struct btrfs_trans_handle *trans,
6246 struct inode *parent_inode, struct inode *inode,
6247 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006248{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006249 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006250 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006251 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006252 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006253 u64 ino = btrfs_ino(BTRFS_I(inode));
6254 u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006255
Li Zefan33345d012011-04-20 10:31:50 +08006256 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006257 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6258 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006259 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006260 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006261 key.offset = 0;
6262 }
Chris Mason39279cc2007-06-12 06:35:45 -04006263
Li Zefan33345d012011-04-20 10:31:50 +08006264 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006265 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6266 root->root_key.objectid, parent_ino,
6267 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006268 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006269 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6270 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006271 }
6272
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006273 /* Nothing to clean up yet */
6274 if (ret)
6275 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006276
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006277 ret = btrfs_insert_dir_item(trans, root, name, name_len,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +02006278 BTRFS_I(parent_inode), &key,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006279 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006280 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006281 goto fail_dir_item;
6282 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006283 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006284 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006285 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006286
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006287 btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006288 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006289 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006290 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006291 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006292 ret = btrfs_update_inode(trans, root, parent_inode);
6293 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006294 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006295 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006296
6297fail_dir_item:
6298 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6299 u64 local_index;
6300 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006301 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6302 root->root_key.objectid, parent_ino,
6303 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006304
6305 } else if (add_backref) {
6306 u64 local_index;
6307 int err;
6308
6309 err = btrfs_del_inode_ref(trans, root, name, name_len,
6310 ino, parent_ino, &local_index);
6311 }
6312 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006313}
6314
6315static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006316 struct inode *dir, struct dentry *dentry,
6317 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006318{
Josef Bacika1b075d2010-11-19 20:36:11 +00006319 int err = btrfs_add_link(trans, dir, inode,
6320 dentry->d_name.name, dentry->d_name.len,
6321 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006322 if (err > 0)
6323 err = -EEXIST;
6324 return err;
6325}
6326
Josef Bacik618e21d2007-07-11 10:18:17 -04006327static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006328 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006329{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006330 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006331 struct btrfs_trans_handle *trans;
6332 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006333 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006334 int err;
6335 int drop_inode = 0;
6336 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006337 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006338
Josef Bacik9ed74f22009-09-11 16:12:44 -04006339 /*
6340 * 2 for inode item and ref
6341 * 2 for dir items
6342 * 1 for xattr if selinux is on
6343 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006344 trans = btrfs_start_transaction(root, 5);
6345 if (IS_ERR(trans))
6346 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006347
Li Zefan581bb052011-04-20 10:06:11 +08006348 err = btrfs_find_free_ino(root, &objectid);
6349 if (err)
6350 goto out_unlock;
6351
Josef Bacikaec74772008-07-24 12:12:38 -04006352 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006353 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6354 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006355 if (IS_ERR(inode)) {
6356 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006357 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006358 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006359
Casey Schauflerad19db72011-12-15 10:09:07 -05006360 /*
6361 * If the active LSM wants to access the inode during
6362 * d_instantiate it needs these. Smack checks to see
6363 * if the filesystem supports xattrs by looking at the
6364 * ops vector.
6365 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006366 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006367 init_special_inode(inode, inode->i_mode, rdev);
6368
6369 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006370 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006371 goto out_unlock_inode;
6372
6373 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6374 if (err) {
6375 goto out_unlock_inode;
6376 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006377 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006378 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006379 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006380 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006381
Josef Bacik618e21d2007-07-11 10:18:17 -04006382out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006383 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006384 btrfs_balance_delayed_items(fs_info);
6385 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006386 if (drop_inode) {
6387 inode_dec_link_count(inode);
6388 iput(inode);
6389 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006390 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006391
6392out_unlock_inode:
6393 drop_inode = 1;
6394 unlock_new_inode(inode);
6395 goto out_unlock;
6396
Josef Bacik618e21d2007-07-11 10:18:17 -04006397}
6398
Chris Mason39279cc2007-06-12 06:35:45 -04006399static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006400 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006401{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006402 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006403 struct btrfs_trans_handle *trans;
6404 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006405 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006406 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006407 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006408 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006409 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006410
Josef Bacik9ed74f22009-09-11 16:12:44 -04006411 /*
6412 * 2 for inode item and ref
6413 * 2 for dir items
6414 * 1 for xattr if selinux is on
6415 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006416 trans = btrfs_start_transaction(root, 5);
6417 if (IS_ERR(trans))
6418 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006419
Li Zefan581bb052011-04-20 10:06:11 +08006420 err = btrfs_find_free_ino(root, &objectid);
6421 if (err)
6422 goto out_unlock;
6423
Josef Bacikaec74772008-07-24 12:12:38 -04006424 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006425 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6426 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006427 if (IS_ERR(inode)) {
6428 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006429 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006430 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006431 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006432 /*
6433 * If the active LSM wants to access the inode during
6434 * d_instantiate it needs these. Smack checks to see
6435 * if the filesystem supports xattrs by looking at the
6436 * ops vector.
6437 */
6438 inode->i_fop = &btrfs_file_operations;
6439 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006440 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006441
6442 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6443 if (err)
6444 goto out_unlock_inode;
6445
6446 err = btrfs_update_inode(trans, root, inode);
6447 if (err)
6448 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006449
Josef Bacika1b075d2010-11-19 20:36:11 +00006450 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006451 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006452 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006453
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006454 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006455 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006456 d_instantiate(dentry, inode);
6457
Chris Mason39279cc2007-06-12 06:35:45 -04006458out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006459 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006460 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006461 inode_dec_link_count(inode);
6462 iput(inode);
6463 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006464 btrfs_balance_delayed_items(fs_info);
6465 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006466 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006467
6468out_unlock_inode:
6469 unlock_new_inode(inode);
6470 goto out_unlock;
6471
Chris Mason39279cc2007-06-12 06:35:45 -04006472}
6473
6474static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6475 struct dentry *dentry)
6476{
Filipe Manana271dba452016-01-05 16:24:05 +00006477 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006478 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006479 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006480 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006481 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006482 int err;
6483 int drop_inode = 0;
6484
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006485 /* do not allow sys_link's with other subvols of the same device */
6486 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006487 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006488
Mark Fashehf1863732012-08-08 11:32:27 -07006489 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006490 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006491
Nikolay Borisov877574e2017-02-20 13:50:33 +02006492 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006493 if (err)
6494 goto fail;
6495
Yan, Zhenga22285a2010-05-16 10:48:46 -04006496 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006497 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006498 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006499 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006500 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006501 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006502 if (IS_ERR(trans)) {
6503 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006504 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006505 goto fail;
6506 }
Chris Mason5f39d392007-10-15 16:14:19 -04006507
Miao Xie67de1172013-12-26 13:07:06 +08006508 /* There are several dir indexes for this inode, clear the cache. */
6509 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006510 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006511 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006512 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006513 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006514 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006515
Josef Bacika1b075d2010-11-19 20:36:11 +00006516 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006517
Yan, Zhenga5719522009-09-24 09:17:31 -04006518 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006519 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006520 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006521 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006522 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006523 if (err)
6524 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006525 if (inode->i_nlink == 1) {
6526 /*
6527 * If new hard link count is 1, it's a file created
6528 * with open(2) O_TMPFILE flag.
6529 */
6530 err = btrfs_orphan_del(trans, inode);
6531 if (err)
6532 goto fail;
6533 }
Al Viro08c422c2011-12-23 07:58:13 -05006534 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006535 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006536 }
Chris Mason39279cc2007-06-12 06:35:45 -04006537
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006538 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006539fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006540 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006541 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006542 if (drop_inode) {
6543 inode_dec_link_count(inode);
6544 iput(inode);
6545 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006546 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006547 return err;
6548}
6549
Al Viro18bb1db2011-07-26 01:41:39 -04006550static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006551{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006552 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006553 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006554 struct btrfs_trans_handle *trans;
6555 struct btrfs_root *root = BTRFS_I(dir)->root;
6556 int err = 0;
6557 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006558 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006559 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006560
Josef Bacik9ed74f22009-09-11 16:12:44 -04006561 /*
6562 * 2 items for inode and ref
6563 * 2 items for dir items
6564 * 1 for xattr if selinux is on
6565 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006566 trans = btrfs_start_transaction(root, 5);
6567 if (IS_ERR(trans))
6568 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006569
Li Zefan581bb052011-04-20 10:06:11 +08006570 err = btrfs_find_free_ino(root, &objectid);
6571 if (err)
6572 goto out_fail;
6573
Josef Bacikaec74772008-07-24 12:12:38 -04006574 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006575 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6576 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006577 if (IS_ERR(inode)) {
6578 err = PTR_ERR(inode);
6579 goto out_fail;
6580 }
Chris Mason5f39d392007-10-15 16:14:19 -04006581
Chris Mason39279cc2007-06-12 06:35:45 -04006582 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006583 /* these must be set before we unlock the inode */
6584 inode->i_op = &btrfs_dir_inode_operations;
6585 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006586
Eric Paris2a7dba32011-02-01 11:05:39 -05006587 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006588 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006589 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006590
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006591 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006592 err = btrfs_update_inode(trans, root, inode);
6593 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006594 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006595
Josef Bacika1b075d2010-11-19 20:36:11 +00006596 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6597 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006598 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006599 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006600
Chris Mason39279cc2007-06-12 06:35:45 -04006601 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006602 /*
6603 * mkdir is special. We're unlocking after we call d_instantiate
6604 * to avoid a race with nfsd calling d_instantiate.
6605 */
6606 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006607 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006608
6609out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006610 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006611 if (drop_on_err) {
6612 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006613 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006614 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006615 btrfs_balance_delayed_items(fs_info);
6616 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006617 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006618
6619out_fail_inode:
6620 unlock_new_inode(inode);
6621 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006622}
6623
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006624/* Find next extent map of a given extent map, caller needs to ensure locks */
6625static struct extent_map *next_extent_map(struct extent_map *em)
6626{
6627 struct rb_node *next;
6628
6629 next = rb_next(&em->rb_node);
6630 if (!next)
6631 return NULL;
6632 return container_of(next, struct extent_map, rb_node);
6633}
6634
6635static struct extent_map *prev_extent_map(struct extent_map *em)
6636{
6637 struct rb_node *prev;
6638
6639 prev = rb_prev(&em->rb_node);
6640 if (!prev)
6641 return NULL;
6642 return container_of(prev, struct extent_map, rb_node);
6643}
6644
Chris Masond352ac62008-09-29 15:18:18 -04006645/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006646 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006647 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006648 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006649 */
Chris Mason3b951512008-04-17 11:29:12 -04006650static int merge_extent_mapping(struct extent_map_tree *em_tree,
6651 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006652 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006653 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006654{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006655 struct extent_map *prev;
6656 struct extent_map *next;
6657 u64 start;
6658 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006659 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006660
Chris Masone6dcd2d2008-07-17 12:53:50 -04006661 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006662
6663 if (existing->start > map_start) {
6664 next = existing;
6665 prev = prev_extent_map(next);
6666 } else {
6667 prev = existing;
6668 next = next_extent_map(prev);
6669 }
6670
6671 start = prev ? extent_map_end(prev) : em->start;
6672 start = max_t(u64, start, em->start);
6673 end = next ? next->start : extent_map_end(em);
6674 end = min_t(u64, end, extent_map_end(em));
6675 start_diff = start - em->start;
6676 em->start = start;
6677 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006678 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6679 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006680 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006681 em->block_len -= start_diff;
6682 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006683 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006684}
6685
Chris Masonc8b97812008-10-29 14:49:59 -04006686static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006687 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006688 size_t pg_offset, u64 extent_offset,
6689 struct btrfs_file_extent_item *item)
6690{
6691 int ret;
6692 struct extent_buffer *leaf = path->nodes[0];
6693 char *tmp;
6694 size_t max_size;
6695 unsigned long inline_size;
6696 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006697 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006698
6699 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006700 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006701 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6702 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006703 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006704 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006705 if (!tmp)
6706 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006707 ptr = btrfs_file_extent_inline_start(item);
6708
6709 read_extent_buffer(leaf, tmp, ptr, inline_size);
6710
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006711 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006712 ret = btrfs_decompress(compress_type, tmp, page,
6713 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006714 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006715 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006716}
6717
Chris Masond352ac62008-09-29 15:18:18 -04006718/*
6719 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006720 * the ugly parts come from merging extents from the disk with the in-ram
6721 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006722 * where the in-ram extents might be locked pending data=ordered completion.
6723 *
6724 * This also copies inline extents directly into the page.
6725 */
Chris Masond3977122009-01-05 21:25:51 -05006726
Chris Masona52d9a82007-08-27 16:49:44 -04006727struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006728 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006729 int create)
6730{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006731 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006732 int ret;
6733 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006734 u64 extent_start = 0;
6735 u64 extent_end = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006736 u64 objectid = btrfs_ino(BTRFS_I(inode));
Chris Masona52d9a82007-08-27 16:49:44 -04006737 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006738 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006739 struct btrfs_root *root = BTRFS_I(inode)->root;
6740 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006741 struct extent_buffer *leaf;
6742 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006743 struct extent_map *em = NULL;
6744 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006745 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006746 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006747 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006748
Chris Masona52d9a82007-08-27 16:49:44 -04006749again:
Chris Mason890871b2009-09-02 16:24:52 -04006750 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006751 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006752 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006753 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006754 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006755
Chris Masona52d9a82007-08-27 16:49:44 -04006756 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006757 if (em->start > start || em->start + em->len <= start)
6758 free_extent_map(em);
6759 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006760 free_extent_map(em);
6761 else
6762 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006763 }
David Sterba172ddd62011-04-21 00:48:27 +02006764 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006765 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006766 err = -ENOMEM;
6767 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006768 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006769 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006770 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006771 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006772 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006773 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006774
6775 if (!path) {
6776 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006777 if (!path) {
6778 err = -ENOMEM;
6779 goto out;
6780 }
6781 /*
6782 * Chances are we'll be called again, so go ahead and do
6783 * readahead
6784 */
David Sterbae4058b52015-11-27 16:31:35 +01006785 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006786 }
6787
Chris Mason179e29e2007-11-01 11:28:41 -04006788 ret = btrfs_lookup_file_extent(trans, root, path,
6789 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006790 if (ret < 0) {
6791 err = ret;
6792 goto out;
6793 }
6794
6795 if (ret != 0) {
6796 if (path->slots[0] == 0)
6797 goto not_found;
6798 path->slots[0]--;
6799 }
6800
Chris Mason5f39d392007-10-15 16:14:19 -04006801 leaf = path->nodes[0];
6802 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006803 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006804 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006805 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006806 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006807 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006808 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006809 /*
6810 * If we backup past the first extent we want to move forward
6811 * and see if there is an extent in front of us, otherwise we'll
6812 * say there is a hole for our whole search range which can
6813 * cause problems.
6814 */
6815 extent_end = start;
6816 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006817 }
6818
Chris Mason5f39d392007-10-15 16:14:19 -04006819 found_type = btrfs_file_extent_type(leaf, item);
6820 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006821 if (found_type == BTRFS_FILE_EXTENT_REG ||
6822 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006823 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006824 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006825 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6826 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006827 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006828 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006829 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006830 }
Josef Bacik25a50342013-10-14 12:08:38 -04006831next:
Yan Zheng9036c102008-10-30 14:19:41 -04006832 if (start >= extent_end) {
6833 path->slots[0]++;
6834 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6835 ret = btrfs_next_leaf(root, path);
6836 if (ret < 0) {
6837 err = ret;
6838 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006839 }
Yan Zheng9036c102008-10-30 14:19:41 -04006840 if (ret > 0)
6841 goto not_found;
6842 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006843 }
Yan Zheng9036c102008-10-30 14:19:41 -04006844 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6845 if (found_key.objectid != objectid ||
6846 found_key.type != BTRFS_EXTENT_DATA_KEY)
6847 goto not_found;
6848 if (start + len <= found_key.offset)
6849 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006850 if (start > found_key.offset)
6851 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006852 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006853 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006854 em->len = found_key.offset - start;
6855 goto not_found_em;
6856 }
6857
Filipe Manana7ffbb592014-06-09 03:48:05 +01006858 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6859
Yan Zhengd899e052008-10-30 14:25:28 -04006860 if (found_type == BTRFS_FILE_EXTENT_REG ||
6861 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006862 goto insert;
6863 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006864 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006865 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006866 size_t size;
6867 size_t extent_offset;
6868 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006869
Filipe Manana7ffbb592014-06-09 03:48:05 +01006870 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006871 goto out;
Yan689f9342007-10-29 11:41:07 -04006872
Chris Mason514ac8a2014-01-03 21:07:00 -08006873 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006874 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006875 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6876 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006877 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006878 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006879 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006880 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006881 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006882 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006883 if (btrfs_file_extent_compression(leaf, item) !=
6884 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006885 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006886 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006887 if (ret) {
6888 err = ret;
6889 goto out;
6890 }
Chris Masonc8b97812008-10-29 14:49:59 -04006891 } else {
6892 map = kmap(page);
6893 read_extent_buffer(leaf, map + pg_offset, ptr,
6894 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006895 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006896 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006897 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006898 copy_size);
6899 }
Chris Masonc8b97812008-10-29 14:49:59 -04006900 kunmap(page);
6901 }
Chris Mason179e29e2007-11-01 11:28:41 -04006902 flush_dcache_page(page);
6903 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006904 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006905 if (!trans) {
6906 kunmap(page);
6907 free_extent_map(em);
6908 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006909
David Sterbab3b4aa72011-04-21 01:20:15 +02006910 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006911 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006912
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006913 if (IS_ERR(trans))
6914 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006915 goto again;
6916 }
Chris Masonc8b97812008-10-29 14:49:59 -04006917 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006918 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006919 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006920 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006921 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006922 }
Chris Masond1310b22008-01-24 16:13:08 -05006923 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006924 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006925 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006926 }
6927not_found:
6928 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006929 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006930 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006931not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006932 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006933 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006934insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006935 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006936 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006937 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006938 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6939 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006940 err = -EIO;
6941 goto out;
6942 }
Chris Masond1310b22008-01-24 16:13:08 -05006943
6944 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006945 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006946 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006947 /* it is possible that someone inserted the extent into the tree
6948 * while we had the lock dropped. It is also possible that
6949 * an overlapping map exists in the tree
6950 */
Chris Masona52d9a82007-08-27 16:49:44 -04006951 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006952 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006953
6954 ret = 0;
6955
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006956 existing = search_extent_mapping(em_tree, start, len);
6957 /*
6958 * existing will always be non-NULL, since there must be
6959 * extent causing the -EEXIST.
6960 */
Chris Mason8dff9c82015-09-19 11:28:25 -07006961 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006962 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07006963 em->block_start == existing->block_start) {
6964 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006965 * The existing extent map already encompasses the
6966 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07006967 */
6968 free_extent_map(em);
6969 em = existing;
6970 err = 0;
6971
6972 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006973 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006974 /*
6975 * The existing extent map is the one nearest to
6976 * the [start, start + len) range which overlaps
6977 */
6978 err = merge_extent_mapping(em_tree, existing,
6979 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006980 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006981 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006982 free_extent_map(em);
6983 em = NULL;
6984 }
6985 } else {
6986 free_extent_map(em);
6987 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006988 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006989 }
Chris Masona52d9a82007-08-27 16:49:44 -04006990 }
Chris Mason890871b2009-09-02 16:24:52 -04006991 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006992out:
liubo1abe9b82011-03-24 11:18:59 +00006993
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006994 trace_btrfs_get_extent(root, BTRFS_I(inode), em);
liubo1abe9b82011-03-24 11:18:59 +00006995
Tsutomu Itoh527afb42015-08-19 14:55:00 +09006996 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006997 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006998 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05006999 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007000 err = ret;
7001 }
Chris Masona52d9a82007-08-27 16:49:44 -04007002 if (err) {
7003 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007004 return ERR_PTR(err);
7005 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007006 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007007 return em;
7008}
7009
Chris Masonec29ed52011-02-23 16:23:20 -05007010struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7011 size_t pg_offset, u64 start, u64 len,
7012 int create)
7013{
7014 struct extent_map *em;
7015 struct extent_map *hole_em = NULL;
7016 u64 range_start = start;
7017 u64 end;
7018 u64 found;
7019 u64 found_end;
7020 int err = 0;
7021
7022 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7023 if (IS_ERR(em))
7024 return em;
7025 if (em) {
7026 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007027 * if our em maps to
7028 * - a hole or
7029 * - a pre-alloc extent,
7030 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007031 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007032 if (em->block_start != EXTENT_MAP_HOLE &&
7033 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007034 return em;
7035 else
7036 hole_em = em;
7037 }
7038
7039 /* check to see if we've wrapped (len == -1 or similar) */
7040 end = start + len;
7041 if (end < start)
7042 end = (u64)-1;
7043 else
7044 end -= 1;
7045
7046 em = NULL;
7047
7048 /* ok, we didn't find anything, lets look for delalloc */
7049 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7050 end, len, EXTENT_DELALLOC, 1);
7051 found_end = range_start + found;
7052 if (found_end < range_start)
7053 found_end = (u64)-1;
7054
7055 /*
7056 * we didn't find anything useful, return
7057 * the original results from get_extent()
7058 */
7059 if (range_start > end || found_end <= start) {
7060 em = hole_em;
7061 hole_em = NULL;
7062 goto out;
7063 }
7064
7065 /* adjust the range_start to make sure it doesn't
7066 * go backwards from the start they passed in
7067 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307068 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007069 found = found_end - range_start;
7070
7071 if (found > 0) {
7072 u64 hole_start = start;
7073 u64 hole_len = len;
7074
David Sterba172ddd62011-04-21 00:48:27 +02007075 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007076 if (!em) {
7077 err = -ENOMEM;
7078 goto out;
7079 }
7080 /*
7081 * when btrfs_get_extent can't find anything it
7082 * returns one huge hole
7083 *
7084 * make sure what it found really fits our range, and
7085 * adjust to make sure it is based on the start from
7086 * the caller
7087 */
7088 if (hole_em) {
7089 u64 calc_end = extent_map_end(hole_em);
7090
7091 if (calc_end <= start || (hole_em->start > end)) {
7092 free_extent_map(hole_em);
7093 hole_em = NULL;
7094 } else {
7095 hole_start = max(hole_em->start, start);
7096 hole_len = calc_end - hole_start;
7097 }
7098 }
7099 em->bdev = NULL;
7100 if (hole_em && range_start > hole_start) {
7101 /* our hole starts before our delalloc, so we
7102 * have to return just the parts of the hole
7103 * that go until the delalloc starts
7104 */
7105 em->len = min(hole_len,
7106 range_start - hole_start);
7107 em->start = hole_start;
7108 em->orig_start = hole_start;
7109 /*
7110 * don't adjust block start at all,
7111 * it is fixed at EXTENT_MAP_HOLE
7112 */
7113 em->block_start = hole_em->block_start;
7114 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007115 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7116 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007117 } else {
7118 em->start = range_start;
7119 em->len = found;
7120 em->orig_start = range_start;
7121 em->block_start = EXTENT_MAP_DELALLOC;
7122 em->block_len = found;
7123 }
7124 } else if (hole_em) {
7125 return hole_em;
7126 }
7127out:
7128
7129 free_extent_map(hole_em);
7130 if (err) {
7131 free_extent_map(em);
7132 return ERR_PTR(err);
7133 }
7134 return em;
7135}
7136
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007137static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7138 const u64 start,
7139 const u64 len,
7140 const u64 orig_start,
7141 const u64 block_start,
7142 const u64 block_len,
7143 const u64 orig_block_len,
7144 const u64 ram_bytes,
7145 const int type)
7146{
7147 struct extent_map *em = NULL;
7148 int ret;
7149
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007150 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007151 em = create_io_em(inode, start, len, orig_start,
7152 block_start, block_len, orig_block_len,
7153 ram_bytes,
7154 BTRFS_COMPRESS_NONE, /* compress_type */
7155 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007156 if (IS_ERR(em))
7157 goto out;
7158 }
7159 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7160 len, block_len, type);
7161 if (ret) {
7162 if (em) {
7163 free_extent_map(em);
7164 btrfs_drop_extent_cache(inode, start,
7165 start + len - 1, 0);
7166 }
7167 em = ERR_PTR(ret);
7168 }
7169 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007170
7171 return em;
7172}
7173
Josef Bacik4b46fce2010-05-23 11:00:55 -04007174static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7175 u64 start, u64 len)
7176{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007177 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007178 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007179 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007180 struct btrfs_key ins;
7181 u64 alloc_hint;
7182 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007183
Josef Bacik4b46fce2010-05-23 11:00:55 -04007184 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007185 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007186 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007187 if (ret)
7188 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007189
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007190 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7191 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007192 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007193 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007194 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007195 btrfs_free_reserved_extent(fs_info, ins.objectid,
7196 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007197
Josef Bacik4b46fce2010-05-23 11:00:55 -04007198 return em;
7199}
7200
Chris Mason46bfbb52010-05-26 11:04:10 -04007201/*
7202 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7203 * block must be cow'd
7204 */
Josef Bacik00361582013-08-14 14:02:47 -04007205noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007206 u64 *orig_start, u64 *orig_block_len,
7207 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007208{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007209 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007210 struct btrfs_path *path;
7211 int ret;
7212 struct extent_buffer *leaf;
7213 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007214 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007215 struct btrfs_file_extent_item *fi;
7216 struct btrfs_key key;
7217 u64 disk_bytenr;
7218 u64 backref_offset;
7219 u64 extent_end;
7220 u64 num_bytes;
7221 int slot;
7222 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007223 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007224
Chris Mason46bfbb52010-05-26 11:04:10 -04007225 path = btrfs_alloc_path();
7226 if (!path)
7227 return -ENOMEM;
7228
David Sterbaf85b7372017-01-20 14:54:07 +01007229 ret = btrfs_lookup_file_extent(NULL, root, path,
7230 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007231 if (ret < 0)
7232 goto out;
7233
7234 slot = path->slots[0];
7235 if (ret == 1) {
7236 if (slot == 0) {
7237 /* can't find the item, must cow */
7238 ret = 0;
7239 goto out;
7240 }
7241 slot--;
7242 }
7243 ret = 0;
7244 leaf = path->nodes[0];
7245 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007246 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007247 key.type != BTRFS_EXTENT_DATA_KEY) {
7248 /* not our file or wrong item type, must cow */
7249 goto out;
7250 }
7251
7252 if (key.offset > offset) {
7253 /* Wrong offset, must cow */
7254 goto out;
7255 }
7256
7257 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7258 found_type = btrfs_file_extent_type(leaf, fi);
7259 if (found_type != BTRFS_FILE_EXTENT_REG &&
7260 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7261 /* not a regular extent, must cow */
7262 goto out;
7263 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007264
7265 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7266 goto out;
7267
Miao Xiee77751a2013-12-27 21:11:50 +08007268 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7269 if (extent_end <= offset)
7270 goto out;
7271
Chris Mason46bfbb52010-05-26 11:04:10 -04007272 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007273 if (disk_bytenr == 0)
7274 goto out;
7275
7276 if (btrfs_file_extent_compression(leaf, fi) ||
7277 btrfs_file_extent_encryption(leaf, fi) ||
7278 btrfs_file_extent_other_encoding(leaf, fi))
7279 goto out;
7280
Chris Mason46bfbb52010-05-26 11:04:10 -04007281 backref_offset = btrfs_file_extent_offset(leaf, fi);
7282
Josef Bacik7ee9e442013-06-21 16:37:03 -04007283 if (orig_start) {
7284 *orig_start = key.offset - backref_offset;
7285 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7286 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7287 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007288
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007289 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007290 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007291
7292 num_bytes = min(offset + *len, extent_end) - offset;
7293 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7294 u64 range_end;
7295
Jeff Mahoneyda170662016-06-15 09:22:56 -04007296 range_end = round_up(offset + num_bytes,
7297 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007298 ret = test_range_bit(io_tree, offset, range_end,
7299 EXTENT_DELALLOC, 0, NULL);
7300 if (ret) {
7301 ret = -EAGAIN;
7302 goto out;
7303 }
7304 }
7305
Josef Bacik1bda19e2013-10-18 12:10:36 -04007306 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007307
7308 /*
7309 * look for other files referencing this extent, if we
7310 * find any we must cow
7311 */
Josef Bacik00361582013-08-14 14:02:47 -04007312
Liu Boe4c3b2d2017-01-30 12:25:28 -08007313 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007314 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007315 if (ret) {
7316 ret = 0;
7317 goto out;
7318 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007319
7320 /*
7321 * adjust disk_bytenr and num_bytes to cover just the bytes
7322 * in this extent we are about to write. If there
7323 * are any csums in that range we have to cow in order
7324 * to keep the csums correct
7325 */
7326 disk_bytenr += backref_offset;
7327 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007328 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7329 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007330 /*
7331 * all of the above have passed, it is safe to overwrite this extent
7332 * without cow
7333 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007334 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007335 ret = 1;
7336out:
7337 btrfs_free_path(path);
7338 return ret;
7339}
7340
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007341bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7342{
7343 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7344 int found = false;
7345 void **pagep = NULL;
7346 struct page *page = NULL;
7347 int start_idx;
7348 int end_idx;
7349
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007350 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007351
7352 /*
7353 * end is the last byte in the last page. end == start is legal
7354 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007355 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007356
7357 rcu_read_lock();
7358
7359 /* Most of the code in this while loop is lifted from
7360 * find_get_page. It's been modified to begin searching from a
7361 * page and return just the first page found in that range. If the
7362 * found idx is less than or equal to the end idx then we know that
7363 * a page exists. If no pages are found or if those pages are
7364 * outside of the range then we're fine (yay!) */
7365 while (page == NULL &&
7366 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7367 page = radix_tree_deref_slot(pagep);
7368 if (unlikely(!page))
7369 break;
7370
7371 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007372 if (radix_tree_deref_retry(page)) {
7373 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007374 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007375 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007376 /*
7377 * Otherwise, shmem/tmpfs must be storing a swap entry
7378 * here as an exceptional entry: so return it without
7379 * attempting to raise page count.
7380 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007381 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007382 break; /* TODO: Is this relevant for this use case? */
7383 }
7384
Filipe Manana91405152014-06-05 13:22:24 +01007385 if (!page_cache_get_speculative(page)) {
7386 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007387 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007388 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007389
7390 /*
7391 * Has the page moved?
7392 * This is part of the lockless pagecache protocol. See
7393 * include/linux/pagemap.h for details.
7394 */
7395 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007396 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007397 page = NULL;
7398 }
7399 }
7400
7401 if (page) {
7402 if (page->index <= end_idx)
7403 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007404 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007405 }
7406
7407 rcu_read_unlock();
7408 return found;
7409}
7410
Josef Bacikeb838e72012-07-31 16:28:48 -04007411static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7412 struct extent_state **cached_state, int writing)
7413{
7414 struct btrfs_ordered_extent *ordered;
7415 int ret = 0;
7416
7417 while (1) {
7418 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007419 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007420 /*
7421 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007422 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007423 * extents in this range.
7424 */
7425 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7426 lockend - lockstart + 1);
7427
7428 /*
7429 * We need to make sure there are no buffered pages in this
7430 * range either, we could have raced between the invalidate in
7431 * generic_file_direct_write and locking the extent. The
7432 * invalidate needs to happen so that reads after a write do not
7433 * get stale data.
7434 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007435 if (!ordered &&
7436 (!writing ||
7437 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007438 break;
7439
7440 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7441 cached_state, GFP_NOFS);
7442
7443 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007444 /*
7445 * If we are doing a DIO read and the ordered extent we
7446 * found is for a buffered write, we can not wait for it
7447 * to complete and retry, because if we do so we can
7448 * deadlock with concurrent buffered writes on page
7449 * locks. This happens only if our DIO read covers more
7450 * than one extent map, if at this point has already
7451 * created an ordered extent for a previous extent map
7452 * and locked its range in the inode's io tree, and a
7453 * concurrent write against that previous extent map's
7454 * range and this range started (we unlock the ranges
7455 * in the io tree only when the bios complete and
7456 * buffered writes always lock pages before attempting
7457 * to lock range in the io tree).
7458 */
7459 if (writing ||
7460 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7461 btrfs_start_ordered_extent(inode, ordered, 1);
7462 else
7463 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007464 btrfs_put_ordered_extent(ordered);
7465 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007466 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007467 * We could trigger writeback for this range (and wait
7468 * for it to complete) and then invalidate the pages for
7469 * this range (through invalidate_inode_pages2_range()),
7470 * but that can lead us to a deadlock with a concurrent
7471 * call to readpages() (a buffered read or a defrag call
7472 * triggered a readahead) on a page lock due to an
7473 * ordered dio extent we created before but did not have
7474 * yet a corresponding bio submitted (whence it can not
7475 * complete), which makes readpages() wait for that
7476 * ordered extent to complete while holding a lock on
7477 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007478 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007479 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007480 }
7481
Filipe Mananaade77022016-02-18 14:28:55 +00007482 if (ret)
7483 break;
7484
Josef Bacikeb838e72012-07-31 16:28:48 -04007485 cond_resched();
7486 }
7487
7488 return ret;
7489}
7490
Liu Bo6f9994d2017-01-31 07:50:22 -08007491/* The callers of this must take lock_extent() */
7492static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7493 u64 orig_start, u64 block_start,
7494 u64 block_len, u64 orig_block_len,
7495 u64 ram_bytes, int compress_type,
7496 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007497{
7498 struct extent_map_tree *em_tree;
7499 struct extent_map *em;
7500 struct btrfs_root *root = BTRFS_I(inode)->root;
7501 int ret;
7502
Liu Bo6f9994d2017-01-31 07:50:22 -08007503 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7504 type == BTRFS_ORDERED_COMPRESSED ||
7505 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007506 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007507
Josef Bacik69ffb542012-09-11 15:40:07 -04007508 em_tree = &BTRFS_I(inode)->extent_tree;
7509 em = alloc_extent_map();
7510 if (!em)
7511 return ERR_PTR(-ENOMEM);
7512
7513 em->start = start;
7514 em->orig_start = orig_start;
7515 em->len = len;
7516 em->block_len = block_len;
7517 em->block_start = block_start;
7518 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007519 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007520 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007521 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007522 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007523 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007524 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007525 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007526 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7527 em->compress_type = compress_type;
7528 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007529
7530 do {
7531 btrfs_drop_extent_cache(inode, em->start,
7532 em->start + em->len - 1, 0);
7533 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007534 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007535 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007536 /*
7537 * The caller has taken lock_extent(), who could race with us
7538 * to add em?
7539 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007540 } while (ret == -EEXIST);
7541
7542 if (ret) {
7543 free_extent_map(em);
7544 return ERR_PTR(ret);
7545 }
7546
Liu Bo6f9994d2017-01-31 07:50:22 -08007547 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007548 return em;
7549}
7550
Filipe Manana9c9464c2015-11-04 09:52:04 +00007551static void adjust_dio_outstanding_extents(struct inode *inode,
7552 struct btrfs_dio_data *dio_data,
7553 const u64 len)
7554{
David Sterba823bb202017-01-04 11:09:51 +01007555 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007556
Filipe Manana9c9464c2015-11-04 09:52:04 +00007557 /*
7558 * If we have an outstanding_extents count still set then we're
7559 * within our reservation, otherwise we need to adjust our inode
7560 * counter appropriately.
7561 */
Liu Boc2931662016-12-22 17:13:54 -08007562 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007563 dio_data->outstanding_extents -= num_extents;
7564 } else {
Liu Boc2931662016-12-22 17:13:54 -08007565 /*
7566 * If dio write length has been split due to no large enough
7567 * contiguous space, we need to compensate our inode counter
7568 * appropriately.
7569 */
7570 u64 num_needed = num_extents - dio_data->outstanding_extents;
7571
Filipe Manana9c9464c2015-11-04 09:52:04 +00007572 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007573 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007574 spin_unlock(&BTRFS_I(inode)->lock);
7575 }
7576}
7577
Josef Bacik4b46fce2010-05-23 11:00:55 -04007578static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7579 struct buffer_head *bh_result, int create)
7580{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007581 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007582 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007583 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307584 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007585 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007586 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007587 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007588 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007589 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007590
Miao Xie172a5042013-02-21 02:48:22 -07007591 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007592 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007593 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007594 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007595
Josef Bacikc3298612012-08-03 16:49:19 -04007596 lockstart = start;
7597 lockend = start + len - 1;
7598
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007599 if (current->journal_info) {
7600 /*
7601 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007602 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007603 * confused.
7604 */
chandan50745b02015-08-28 21:10:13 +05307605 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007606 current->journal_info = NULL;
7607 }
7608
Josef Bacikeb838e72012-07-31 16:28:48 -04007609 /*
7610 * If this errors out it's because we couldn't invalidate pagecache for
7611 * this range and we need to fallback to buffered.
7612 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007613 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7614 create)) {
7615 ret = -ENOTBLK;
7616 goto err;
7617 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007618
Josef Bacik4b46fce2010-05-23 11:00:55 -04007619 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007620 if (IS_ERR(em)) {
7621 ret = PTR_ERR(em);
7622 goto unlock_err;
7623 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007624
7625 /*
7626 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7627 * io. INLINE is special, and we could probably kludge it in here, but
7628 * it's still buffered so for safety lets just fall back to the generic
7629 * buffered path.
7630 *
7631 * For COMPRESSED we _have_ to read the entire extent in so we can
7632 * decompress it, so there will be buffering required no matter what we
7633 * do, so go ahead and fallback to buffered.
7634 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007635 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007636 * to buffered IO. Don't blame me, this is the price we pay for using
7637 * the generic code.
7638 */
7639 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7640 em->block_start == EXTENT_MAP_INLINE) {
7641 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007642 ret = -ENOTBLK;
7643 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007644 }
7645
7646 /* Just a good old fashioned hole, return */
7647 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7648 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7649 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007650 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007651 }
7652
7653 /*
7654 * We don't allocate a new extent in the following cases
7655 *
7656 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7657 * existing extent.
7658 * 2) The extent is marked as PREALLOC. We're good to go here and can
7659 * just use the extent.
7660 *
7661 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007662 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007663 len = min(len, em->len - (start - em->start));
7664 lockstart = start + len;
7665 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007666 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007667
7668 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7669 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7670 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007671 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007672 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007673
7674 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7675 type = BTRFS_ORDERED_PREALLOC;
7676 else
7677 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007678 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007679 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007680
Josef Bacik00361582013-08-14 14:02:47 -04007681 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007682 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007683 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007684 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007685
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007686 em2 = btrfs_create_dio_extent(inode, start, len,
7687 orig_start, block_start,
7688 len, orig_block_len,
7689 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007690 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007691 if (type == BTRFS_ORDERED_PREALLOC) {
7692 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007693 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007694 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007695 if (em2 && IS_ERR(em2)) {
7696 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007697 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007698 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007699 /*
7700 * For inode marked NODATACOW or extent marked PREALLOC,
7701 * use the existing or preallocated extent, so does not
7702 * need to adjust btrfs_space_info's bytes_may_use.
7703 */
7704 btrfs_free_reserved_data_space_noquota(inode,
7705 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007706 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007707 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007708 }
Josef Bacik00361582013-08-14 14:02:47 -04007709
Chris Mason46bfbb52010-05-26 11:04:10 -04007710 /*
7711 * this will cow the extent, reset the len in case we changed
7712 * it above
7713 */
7714 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007715 free_extent_map(em);
7716 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007717 if (IS_ERR(em)) {
7718 ret = PTR_ERR(em);
7719 goto unlock_err;
7720 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007721 len = min(len, em->len - (start - em->start));
7722unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007723 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7724 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007725 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007726 bh_result->b_bdev = em->bdev;
7727 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007728 if (create) {
7729 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7730 set_buffer_new(bh_result);
7731
7732 /*
7733 * Need to update the i_size under the extent lock so buffered
7734 * readers will get the updated i_size when we unlock.
7735 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007736 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007737 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007738
Filipe Manana9c9464c2015-11-04 09:52:04 +00007739 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307740 WARN_ON(dio_data->reserve < len);
7741 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007742 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307743 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007744 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007745
Josef Bacikeb838e72012-07-31 16:28:48 -04007746 /*
7747 * In the case of write we need to clear and unlock the entire range,
7748 * in the case of read we need to unlock only the end area that we
7749 * aren't using if there is any left over space.
7750 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007751 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007752 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7753 lockend, unlock_bits, 1, 0,
7754 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007755 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007756 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007757 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007758
Josef Bacik4b46fce2010-05-23 11:00:55 -04007759 free_extent_map(em);
7760
7761 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007762
7763unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007764 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7765 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007766err:
chandan50745b02015-08-28 21:10:13 +05307767 if (dio_data)
7768 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007769 /*
7770 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7771 * write less data then expected, so that we don't underflow our inode's
7772 * outstanding extents counter.
7773 */
7774 if (create && dio_data)
7775 adjust_dio_outstanding_extents(inode, dio_data, len);
7776
Josef Bacikeb838e72012-07-31 16:28:48 -04007777 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007778}
7779
Miao Xie8b110e32014-09-12 18:44:03 +08007780static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007781 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007782{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007783 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007784 int ret;
7785
Mike Christie37226b22016-06-05 14:31:52 -05007786 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007787
7788 bio_get(bio);
7789
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007790 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007791 if (ret)
7792 goto err;
7793
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007794 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007795err:
7796 bio_put(bio);
7797 return ret;
7798}
7799
7800static int btrfs_check_dio_repairable(struct inode *inode,
7801 struct bio *failed_bio,
7802 struct io_failure_record *failrec,
7803 int failed_mirror)
7804{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007805 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007806 int num_copies;
7807
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007808 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007809 if (num_copies == 1) {
7810 /*
7811 * we only have a single copy of the data, so don't bother with
7812 * all the retry and error correction code that follows. no
7813 * matter what the error is, it is very likely to persist.
7814 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007815 btrfs_debug(fs_info,
7816 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7817 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007818 return 0;
7819 }
7820
7821 failrec->failed_mirror = failed_mirror;
7822 failrec->this_mirror++;
7823 if (failrec->this_mirror == failed_mirror)
7824 failrec->this_mirror++;
7825
7826 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007827 btrfs_debug(fs_info,
7828 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7829 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007830 return 0;
7831 }
7832
7833 return 1;
7834}
7835
7836static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307837 struct page *page, unsigned int pgoff,
7838 u64 start, u64 end, int failed_mirror,
7839 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007840{
7841 struct io_failure_record *failrec;
7842 struct bio *bio;
7843 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007844 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007845 int ret;
7846
Mike Christie37226b22016-06-05 14:31:52 -05007847 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007848
7849 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7850 if (ret)
7851 return ret;
7852
7853 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7854 failed_mirror);
7855 if (!ret) {
7856 free_io_failure(inode, failrec);
7857 return -EIO;
7858 }
7859
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307860 if ((failed_bio->bi_vcnt > 1)
7861 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007862 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007863 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007864
7865 isector = start - btrfs_io_bio(failed_bio)->logical;
7866 isector >>= inode->i_sb->s_blocksize_bits;
7867 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307868 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007869 if (!bio) {
7870 free_io_failure(inode, failrec);
7871 return -EIO;
7872 }
Mike Christie37226b22016-06-05 14:31:52 -05007873 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007874
7875 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7876 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7877 read_mode, failrec->this_mirror, failrec->in_validation);
7878
Mike Christie81a75f672016-06-05 14:31:54 -05007879 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007880 if (ret) {
7881 free_io_failure(inode, failrec);
7882 bio_put(bio);
7883 }
7884
7885 return ret;
7886}
7887
7888struct btrfs_retry_complete {
7889 struct completion done;
7890 struct inode *inode;
7891 u64 start;
7892 int uptodate;
7893};
7894
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007895static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007896{
7897 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307898 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007899 struct bio_vec *bvec;
7900 int i;
7901
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007902 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007903 goto end;
7904
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307905 ASSERT(bio->bi_vcnt == 1);
7906 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007907 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307908
Miao Xie8b110e32014-09-12 18:44:03 +08007909 done->uptodate = 1;
7910 bio_for_each_segment_all(bvec, bio, i)
7911 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7912end:
7913 complete(&done->done);
7914 bio_put(bio);
7915}
7916
7917static int __btrfs_correct_data_nocsum(struct inode *inode,
7918 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007919{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307920 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007921 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007922 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007923 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307924 unsigned int pgoff;
7925 u32 sectorsize;
7926 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007927 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007928 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007929
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307930 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007931 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307932
Miao Xiec1dc0892014-09-12 18:43:56 +08007933 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007934 done.inode = inode;
7935
7936 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307937 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7938 pgoff = bvec->bv_offset;
7939
7940next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007941 done.uptodate = 0;
7942 done.start = start;
7943 init_completion(&done.done);
7944
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307945 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7946 pgoff, start, start + sectorsize - 1,
7947 io_bio->mirror_num,
7948 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007949 if (ret)
7950 return ret;
7951
7952 wait_for_completion(&done.done);
7953
7954 if (!done.uptodate) {
7955 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307956 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007957 }
7958
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307959 start += sectorsize;
7960
7961 if (nr_sectors--) {
7962 pgoff += sectorsize;
7963 goto next_block_or_try_again;
7964 }
Miao Xie8b110e32014-09-12 18:44:03 +08007965 }
7966
7967 return 0;
7968}
7969
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007970static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007971{
7972 struct btrfs_retry_complete *done = bio->bi_private;
7973 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307974 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007975 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307976 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08007977 int uptodate;
7978 int ret;
7979 int i;
7980
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007981 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007982 goto end;
7983
7984 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307985
7986 start = done->start;
7987
7988 ASSERT(bio->bi_vcnt == 1);
7989 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007990 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307991
Miao Xie8b110e32014-09-12 18:44:03 +08007992 bio_for_each_segment_all(bvec, bio, i) {
7993 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307994 bvec->bv_page, bvec->bv_offset,
7995 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007996 if (!ret)
7997 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307998 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007999 else
8000 uptodate = 0;
8001 }
8002
8003 done->uptodate = uptodate;
8004end:
8005 complete(&done->done);
8006 bio_put(bio);
8007}
8008
8009static int __btrfs_subio_endio_read(struct inode *inode,
8010 struct btrfs_io_bio *io_bio, int err)
8011{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308012 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008013 struct bio_vec *bvec;
8014 struct btrfs_retry_complete done;
8015 u64 start;
8016 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308017 u32 sectorsize;
8018 int nr_sectors;
8019 unsigned int pgoff;
8020 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008021 int i;
8022 int ret;
8023
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308024 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008025 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308026
Miao Xie8b110e32014-09-12 18:44:03 +08008027 err = 0;
8028 start = io_bio->logical;
8029 done.inode = inode;
8030
Miao Xiec1dc0892014-09-12 18:43:56 +08008031 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308032 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8033
8034 pgoff = bvec->bv_offset;
8035next_block:
8036 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8037 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8038 bvec->bv_page, pgoff, start,
8039 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008040 if (likely(!ret))
8041 goto next;
8042try_again:
8043 done.uptodate = 0;
8044 done.start = start;
8045 init_completion(&done.done);
8046
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308047 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8048 pgoff, start, start + sectorsize - 1,
8049 io_bio->mirror_num,
8050 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008051 if (ret) {
8052 err = ret;
8053 goto next;
8054 }
8055
8056 wait_for_completion(&done.done);
8057
8058 if (!done.uptodate) {
8059 /* We might have another mirror, so try again */
8060 goto try_again;
8061 }
8062next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308063 offset += sectorsize;
8064 start += sectorsize;
8065
8066 ASSERT(nr_sectors);
8067
8068 if (--nr_sectors) {
8069 pgoff += sectorsize;
8070 goto next_block;
8071 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008072 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008073
8074 return err;
8075}
8076
Miao Xie8b110e32014-09-12 18:44:03 +08008077static int btrfs_subio_endio_read(struct inode *inode,
8078 struct btrfs_io_bio *io_bio, int err)
8079{
8080 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8081
8082 if (skip_csum) {
8083 if (unlikely(err))
8084 return __btrfs_correct_data_nocsum(inode, io_bio);
8085 else
8086 return 0;
8087 } else {
8088 return __btrfs_subio_endio_read(inode, io_bio, err);
8089 }
8090}
8091
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008092static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008093{
8094 struct btrfs_dio_private *dip = bio->bi_private;
8095 struct inode *inode = dip->inode;
8096 struct bio *dio_bio;
8097 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008098 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008099
Miao Xie8b110e32014-09-12 18:44:03 +08008100 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8101 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008102
Josef Bacik4b46fce2010-05-23 11:00:55 -04008103 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008104 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008105 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008106
Josef Bacik4b46fce2010-05-23 11:00:55 -04008107 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008108
Filipe Manana1636d1d2016-02-15 16:20:26 +00008109 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008110 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008111
8112 if (io_bio->end_io)
8113 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008114 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008115}
8116
Filipe Manana14543772015-11-24 16:23:54 +00008117static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8118 const u64 offset,
8119 const u64 bytes,
8120 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008121{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008122 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008123 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008124 u64 ordered_offset = offset;
8125 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008126 int ret;
8127
Chris Mason163cf092010-11-28 19:56:33 -05008128again:
8129 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8130 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008131 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008132 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008133 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008134 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008135
Liu Bo9e0af232014-08-15 23:36:53 +08008136 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8137 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008138 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008139out_test:
8140 /*
8141 * our bio might span multiple ordered extents. If we haven't
8142 * completed the accounting for the whole dio, go back and try again
8143 */
Filipe Manana14543772015-11-24 16:23:54 +00008144 if (ordered_offset < offset + bytes) {
8145 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008146 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008147 goto again;
8148 }
Filipe Manana14543772015-11-24 16:23:54 +00008149}
8150
8151static void btrfs_endio_direct_write(struct bio *bio)
8152{
8153 struct btrfs_dio_private *dip = bio->bi_private;
8154 struct bio *dio_bio = dip->dio_bio;
8155
8156 btrfs_endio_direct_write_update_ordered(dip->inode,
8157 dip->logical_offset,
8158 dip->bytes,
8159 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008160
Josef Bacik4b46fce2010-05-23 11:00:55 -04008161 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008162
Filipe Manana1636d1d2016-02-15 16:20:26 +00008163 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008164 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008165 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008166}
8167
Mike Christie81a75f672016-06-05 14:31:54 -05008168static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008169 struct bio *bio, int mirror_num,
8170 unsigned long bio_flags, u64 offset)
8171{
8172 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008173 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008174 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008175 return 0;
8176}
8177
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008178static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008179{
8180 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008181 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008182
Miao Xie8b110e32014-09-12 18:44:03 +08008183 if (err)
8184 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008185 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008186 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8187 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008188 (unsigned long long)bio->bi_iter.bi_sector,
8189 bio->bi_iter.bi_size, err);
8190
8191 if (dip->subio_endio)
8192 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008193
8194 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008195 dip->errors = 1;
8196
8197 /*
8198 * before atomic variable goto zero, we must make sure
8199 * dip->errors is perceived to be set.
8200 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008201 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008202 }
8203
8204 /* if there are more bios still pending for this dio, just exit */
8205 if (!atomic_dec_and_test(&dip->pending_bios))
8206 goto out;
8207
Chris Mason9be33952013-05-17 18:30:14 -04008208 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008209 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008210 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008211 dip->dio_bio->bi_error = 0;
8212 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008213 }
8214out:
8215 bio_put(bio);
8216}
8217
8218static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8219 u64 first_sector, gfp_t gfp_flags)
8220{
Chris Masonda2f0f72015-07-02 13:57:22 -07008221 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008222 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008223 if (bio)
8224 bio_associate_current(bio);
8225 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008226}
8227
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008228static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008229 struct btrfs_dio_private *dip,
8230 struct bio *bio,
8231 u64 file_offset)
8232{
8233 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8234 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8235 int ret;
8236
8237 /*
8238 * We load all the csum data we need when we submit
8239 * the first bio to reduce the csum tree search and
8240 * contention.
8241 */
8242 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008243 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008244 file_offset);
8245 if (ret)
8246 return ret;
8247 }
8248
8249 if (bio == dip->orig_bio)
8250 return 0;
8251
8252 file_offset -= dip->logical_offset;
8253 file_offset >>= inode->i_sb->s_blocksize_bits;
8254 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8255
8256 return 0;
8257}
8258
Miao Xiee65e1532010-11-22 03:04:43 +00008259static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008260 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008261 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008262{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008264 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008265 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008266 int ret;
8267
Josef Bacikb812ce22012-11-16 13:56:32 -05008268 if (async_submit)
8269 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8270
Miao Xiee65e1532010-11-22 03:04:43 +00008271 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008272
8273 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008274 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008275 if (ret)
8276 goto err;
8277 }
Miao Xiee65e1532010-11-22 03:04:43 +00008278
Josef Bacik1ae39932011-04-06 14:41:34 -04008279 if (skip_sum)
8280 goto map;
8281
8282 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008283 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8284 file_offset,
8285 __btrfs_submit_bio_start_direct_io,
8286 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008287 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008288 } else if (write) {
8289 /*
8290 * If we aren't doing async submit, calculate the csum of the
8291 * bio now.
8292 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008293 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008294 if (ret)
8295 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008296 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008297 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008298 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008299 if (ret)
8300 goto err;
8301 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008302map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008303 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008304err:
8305 bio_put(bio);
8306 return ret;
8307}
8308
Mike Christie81a75f672016-06-05 14:31:54 -05008309static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008310 int skip_sum)
8311{
8312 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008313 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008314 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008315 struct bio *bio;
8316 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008317 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008318 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008319 u64 file_offset = dip->logical_offset;
8320 u64 submit_len = 0;
8321 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008322 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008323 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308324 int nr_sectors;
8325 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008326 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008327
Kent Overstreet4f024f32013-10-11 15:44:27 -07008328 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008329 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8330 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008331 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008332 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008333
Kent Overstreet4f024f32013-10-11 15:44:27 -07008334 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008335 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008336 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008337 goto submit;
8338 }
8339
David Woodhouse53b381b2013-01-29 18:40:14 -05008340 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008341 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008342 async_submit = 0;
8343 else
8344 async_submit = 1;
8345
Josef Bacik02f57c72011-04-06 14:25:44 -04008346 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8347 if (!bio)
8348 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008349
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008350 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008351 bio->bi_private = dip;
8352 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008353 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008354 atomic_inc(&dip->pending_bios);
8355
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008356 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008357 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308358 i = 0;
8359next_block:
8360 if (unlikely(map_length < submit_len + blocksize ||
8361 bio_add_page(bio, bvec->bv_page, blocksize,
8362 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008363 /*
8364 * inc the count before we submit the bio so
8365 * we know the end IO handler won't happen before
8366 * we inc the count. Otherwise, the dip might get freed
8367 * before we're done setting it up
8368 */
8369 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008370 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008371 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008372 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008373 if (ret) {
8374 bio_put(bio);
8375 atomic_dec(&dip->pending_bios);
8376 goto out_err;
8377 }
8378
Miao Xiee65e1532010-11-22 03:04:43 +00008379 start_sector += submit_len >> 9;
8380 file_offset += submit_len;
8381
8382 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008383
8384 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8385 start_sector, GFP_NOFS);
8386 if (!bio)
8387 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008388 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008389 bio->bi_private = dip;
8390 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008391 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008392
Kent Overstreet4f024f32013-10-11 15:44:27 -07008393 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008394 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008395 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008396 &map_length, NULL, 0);
8397 if (ret) {
8398 bio_put(bio);
8399 goto out_err;
8400 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308401
8402 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008403 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308404 submit_len += blocksize;
8405 if (--nr_sectors) {
8406 i++;
8407 goto next_block;
8408 }
Miao Xiee65e1532010-11-22 03:04:43 +00008409 }
8410 }
8411
Josef Bacik02f57c72011-04-06 14:25:44 -04008412submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008413 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008414 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008415 if (!ret)
8416 return 0;
8417
8418 bio_put(bio);
8419out_err:
8420 dip->errors = 1;
8421 /*
8422 * before atomic variable goto zero, we must
8423 * make sure dip->errors is perceived to be set.
8424 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008425 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008426 if (atomic_dec_and_test(&dip->pending_bios))
8427 bio_io_error(dip->orig_bio);
8428
8429 /* bio_end_io() will handle error, so we needn't return it */
8430 return 0;
8431}
8432
Mike Christie8a4c1e42016-06-05 14:31:50 -05008433static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8434 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008435{
Filipe Manana61de7182015-07-01 12:13:10 +01008436 struct btrfs_dio_private *dip = NULL;
8437 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008438 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008439 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008440 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008441 int ret = 0;
8442
8443 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8444
Chris Mason9be33952013-05-17 18:30:14 -04008445 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008446 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008447 ret = -ENOMEM;
8448 goto free_ordered;
8449 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008450
Miao Xiec1dc0892014-09-12 18:43:56 +08008451 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008452 if (!dip) {
8453 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008454 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008455 }
8456
8457 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008458 dip->inode = inode;
8459 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008460 dip->bytes = dio_bio->bi_iter.bi_size;
8461 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008462 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008463 dip->orig_bio = io_bio;
8464 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008465 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008466 btrfs_bio = btrfs_io_bio(io_bio);
8467 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008468
Miao Xiec1dc0892014-09-12 18:43:56 +08008469 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008470 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008471 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008472 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008473 dip->subio_endio = btrfs_subio_endio_read;
8474 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008475
Filipe Mananaf28a4922015-12-08 19:23:20 +00008476 /*
8477 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8478 * even if we fail to submit a bio, because in such case we do the
8479 * corresponding error handling below and it must not be done a second
8480 * time by btrfs_direct_IO().
8481 */
8482 if (write) {
8483 struct btrfs_dio_data *dio_data = current->journal_info;
8484
8485 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8486 dip->bytes;
8487 dio_data->unsubmitted_oe_range_start =
8488 dio_data->unsubmitted_oe_range_end;
8489 }
8490
Mike Christie81a75f672016-06-05 14:31:54 -05008491 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008492 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008493 return;
Chris Mason9be33952013-05-17 18:30:14 -04008494
Miao Xie23ea8e52014-09-12 18:43:54 +08008495 if (btrfs_bio->end_io)
8496 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008497
Josef Bacik4b46fce2010-05-23 11:00:55 -04008498free_ordered:
8499 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008500 * If we arrived here it means either we failed to submit the dip
8501 * or we either failed to clone the dio_bio or failed to allocate the
8502 * dip. If we cloned the dio_bio and allocated the dip, we can just
8503 * call bio_endio against our io_bio so that we get proper resource
8504 * cleanup if we fail to submit the dip, otherwise, we must do the
8505 * same as btrfs_endio_direct_[write|read] because we can't call these
8506 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008507 */
Filipe Manana61de7182015-07-01 12:13:10 +01008508 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008509 io_bio->bi_error = -EIO;
8510 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008511 /*
8512 * The end io callbacks free our dip, do the final put on io_bio
8513 * and all the cleanup and final put for dio_bio (through
8514 * dio_end_io()).
8515 */
8516 dip = NULL;
8517 io_bio = NULL;
8518 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008519 if (write)
8520 btrfs_endio_direct_write_update_ordered(inode,
8521 file_offset,
8522 dio_bio->bi_iter.bi_size,
8523 0);
8524 else
Filipe Manana61de7182015-07-01 12:13:10 +01008525 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8526 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008527
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008528 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008529 /*
8530 * Releases and cleans up our dio_bio, no need to bio_put()
8531 * nor bio_endio()/bio_io_error() against dio_bio.
8532 */
8533 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008534 }
Filipe Manana61de7182015-07-01 12:13:10 +01008535 if (io_bio)
8536 bio_put(io_bio);
8537 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008538}
8539
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008540static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8541 struct kiocb *iocb,
8542 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008543{
8544 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008545 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008546 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008547 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008548
8549 if (offset & blocksize_mask)
8550 goto out;
8551
Al Viro28060d52014-03-22 05:15:17 -04008552 if (iov_iter_alignment(iter) & blocksize_mask)
8553 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008554
Al Viro28060d52014-03-22 05:15:17 -04008555 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008556 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008557 return 0;
8558 /*
8559 * Check to make sure we don't have duplicate iov_base's in this
8560 * iovec, if so return EINVAL, otherwise we'll get csum errors
8561 * when reading back.
8562 */
8563 for (seg = 0; seg < iter->nr_segs; seg++) {
8564 for (i = seg + 1; i < iter->nr_segs; i++) {
8565 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008566 goto out;
8567 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008568 }
8569 retval = 0;
8570out:
8571 return retval;
8572}
Josef Bacikeb838e72012-07-31 16:28:48 -04008573
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008574static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008575{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008576 struct file *file = iocb->ki_filp;
8577 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008578 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308579 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008580 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008581 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008582 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008583 bool wakeup = true;
8584 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008585 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008586
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008587 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008588 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008589
Jens Axboefe0f07d2015-04-15 17:05:48 -06008590 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008591 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008592
Josef Bacik0e267c42013-07-02 10:38:02 -04008593 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008594 * The generic stuff only does filemap_write_and_wait_range, which
8595 * isn't enough if we've written compressed pages to this area, so
8596 * we need to flush the dirty pages again to make absolutely sure
8597 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008598 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008599 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008600 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8601 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008602 filemap_fdatawrite_range(inode->i_mapping, offset,
8603 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008604
Omar Sandoval6f673762015-03-16 04:33:52 -07008605 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008606 /*
8607 * If the write DIO is beyond the EOF, we need update
8608 * the isize, but it is protected by i_mutex. So we can
8609 * not unlock the i_mutex at this case.
8610 */
8611 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008612 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008613 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008614 relock = true;
8615 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008616 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008617 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008618 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008619 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008620
8621 /*
8622 * We need to know how many extents we reserved so that we can
8623 * do the accounting properly if we go over the number we
8624 * originally calculated. Abuse current->journal_info for this.
8625 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008626 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008627 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008628 dio_data.unsubmitted_oe_range_start = (u64)offset;
8629 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308630 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308631 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008632 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8633 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008634 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008635 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8636 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008637 }
8638
Omar Sandoval17f8c842015-03-16 04:33:50 -07008639 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008640 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008641 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008642 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008643 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308644 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008645 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008646 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308647 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008648 btrfs_delalloc_release_space(inode, offset,
8649 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008650 /*
8651 * On error we might have left some ordered extents
8652 * without submitting corresponding bios for them, so
8653 * cleanup them up to avoid other tasks getting them
8654 * and waiting for them to complete forever.
8655 */
8656 if (dio_data.unsubmitted_oe_range_start <
8657 dio_data.unsubmitted_oe_range_end)
8658 btrfs_endio_direct_write_update_ordered(inode,
8659 dio_data.unsubmitted_oe_range_start,
8660 dio_data.unsubmitted_oe_range_end -
8661 dio_data.unsubmitted_oe_range_start,
8662 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008663 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008664 btrfs_delalloc_release_space(inode, offset,
8665 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008666 }
Miao Xie38851cc2013-02-08 07:04:11 +00008667out:
Miao Xie2e60a512013-02-08 07:01:08 +00008668 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008669 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008670 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008671 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008672
8673 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008674}
8675
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008676#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8677
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008678static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8679 __u64 start, __u64 len)
8680{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008681 int ret;
8682
8683 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8684 if (ret)
8685 return ret;
8686
Chris Masonec29ed52011-02-23 16:23:20 -05008687 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008688}
8689
Chris Mason9ebefb182007-06-15 13:50:00 -04008690int btrfs_readpage(struct file *file, struct page *page)
8691{
Chris Masond1310b22008-01-24 16:13:08 -05008692 struct extent_io_tree *tree;
8693 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008694 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008695}
Chris Mason1832a6d2007-12-21 16:27:21 -05008696
Chris Mason39279cc2007-06-12 06:35:45 -04008697static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8698{
Chris Masond1310b22008-01-24 16:13:08 -05008699 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008700 struct inode *inode = page->mapping->host;
8701 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008702
8703 if (current->flags & PF_MEMALLOC) {
8704 redirty_page_for_writepage(wbc, page);
8705 unlock_page(page);
8706 return 0;
8707 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008708
8709 /*
8710 * If we are under memory pressure we will call this directly from the
8711 * VM, we need to make sure we have the inode referenced for the ordered
8712 * extent. If not just return like we didn't do anything.
8713 */
8714 if (!igrab(inode)) {
8715 redirty_page_for_writepage(wbc, page);
8716 return AOP_WRITEPAGE_ACTIVATE;
8717 }
Chris Masond1310b22008-01-24 16:13:08 -05008718 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008719 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8720 btrfs_add_delayed_iput(inode);
8721 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008722}
Chris Mason39279cc2007-06-12 06:35:45 -04008723
Eric Sandeen48a3b632013-04-25 20:41:01 +00008724static int btrfs_writepages(struct address_space *mapping,
8725 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008726{
Chris Masond1310b22008-01-24 16:13:08 -05008727 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008728
Chris Masond1310b22008-01-24 16:13:08 -05008729 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008730 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8731}
8732
Chris Mason3ab2fb52007-11-08 10:59:22 -05008733static int
8734btrfs_readpages(struct file *file, struct address_space *mapping,
8735 struct list_head *pages, unsigned nr_pages)
8736{
Chris Masond1310b22008-01-24 16:13:08 -05008737 struct extent_io_tree *tree;
8738 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008739 return extent_readpages(tree, mapping, pages, nr_pages,
8740 btrfs_get_extent);
8741}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008742static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008743{
Chris Masond1310b22008-01-24 16:13:08 -05008744 struct extent_io_tree *tree;
8745 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008746 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008747
Chris Masond1310b22008-01-24 16:13:08 -05008748 tree = &BTRFS_I(page->mapping->host)->io_tree;
8749 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008750 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008751 if (ret == 1) {
8752 ClearPagePrivate(page);
8753 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008754 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008755 }
8756 return ret;
8757}
Chris Mason39279cc2007-06-12 06:35:45 -04008758
Chris Masone6dcd2d2008-07-17 12:53:50 -04008759static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8760{
Chris Mason98509cf2008-09-11 15:51:43 -04008761 if (PageWriteback(page) || PageDirty(page))
8762 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008763 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008764}
8765
Lukas Czernerd47992f2013-05-21 23:17:23 -04008766static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8767 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008768{
Josef Bacik5fd02042012-05-02 14:00:54 -04008769 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008770 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008771 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008772 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008773 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008774 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308775 u64 start;
8776 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008777 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008778
Chris Mason8b62b722009-09-02 16:53:46 -04008779 /*
8780 * we have the page locked, so new writeback can't start,
8781 * and the dirty bit won't be cleared while we are here.
8782 *
8783 * Wait for IO on this page so that we can safely clear
8784 * the PagePrivate2 bit and do ordered accounting
8785 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008786 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008787
Josef Bacik5fd02042012-05-02 14:00:54 -04008788 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008789 if (offset) {
8790 btrfs_releasepage(page, GFP_NOFS);
8791 return;
8792 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008793
8794 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008795 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308796again:
8797 start = page_start;
8798 ordered = btrfs_lookup_ordered_range(inode, start,
8799 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008800 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308801 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008802 /*
8803 * IO on this page will never be started, so we need
8804 * to account for any ordered extents now
8805 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008806 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308807 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008808 EXTENT_DIRTY | EXTENT_DELALLOC |
8809 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8810 EXTENT_DEFRAG, 1, 0, &cached_state,
8811 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008812 /*
8813 * whoever cleared the private bit is responsible
8814 * for the finish_ordered_io
8815 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008816 if (TestClearPagePrivate2(page)) {
8817 struct btrfs_ordered_inode_tree *tree;
8818 u64 new_len;
8819
8820 tree = &BTRFS_I(inode)->ordered_tree;
8821
8822 spin_lock_irq(&tree->lock);
8823 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308824 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008825 if (new_len < ordered->truncated_len)
8826 ordered->truncated_len = new_len;
8827 spin_unlock_irq(&tree->lock);
8828
8829 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308830 start,
8831 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008832 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008833 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008834 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008835 if (!inode_evicting) {
8836 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308837 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008838 &cached_state);
8839 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308840
8841 start = end + 1;
8842 if (start < page_end)
8843 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008844 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008845
Qu Wenruob9d0b382015-09-29 10:35:16 +08008846 /*
8847 * Qgroup reserved space handler
8848 * Page here will be either
8849 * 1) Already written to disk
8850 * In this case, its reserved space is released from data rsv map
8851 * and will be freed by delayed_ref handler finally.
8852 * So even we call qgroup_free_data(), it won't decrease reserved
8853 * space.
8854 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008855 * This means the reserved space should be freed here. However,
8856 * if a truncate invalidates the page (by clearing PageDirty)
8857 * and the page is accounted for while allocating extent
8858 * in btrfs_check_data_free_space() we let delayed_ref to
8859 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008860 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008861 if (PageDirty(page))
8862 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008863 if (!inode_evicting) {
8864 clear_extent_bit(tree, page_start, page_end,
8865 EXTENT_LOCKED | EXTENT_DIRTY |
8866 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8867 EXTENT_DEFRAG, 1, 1,
8868 &cached_state, GFP_NOFS);
8869
8870 __btrfs_releasepage(page, GFP_NOFS);
8871 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008872
Chris Mason4a096752008-07-21 10:29:44 -04008873 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008874 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008875 ClearPagePrivate(page);
8876 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008877 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008878 }
Chris Mason39279cc2007-06-12 06:35:45 -04008879}
8880
Chris Mason9ebefb182007-06-15 13:50:00 -04008881/*
8882 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8883 * called from a page fault handler when a page is first dirtied. Hence we must
8884 * be careful to check for EOF conditions here. We set the page up correctly
8885 * for a written page which means we get ENOSPC checking when writing into
8886 * holes and correct delalloc and unwritten extent mapping on filesystems that
8887 * support these features.
8888 *
8889 * We are not allowed to take the i_mutex here so we have to play games to
8890 * protect against truncate races as the page could now be beyond EOF. Because
8891 * vmtruncate() writes the inode size before removing pages, once we have the
8892 * page lock we can determine safely if the page is beyond EOF. If it is not
8893 * beyond EOF, then the page is guaranteed safe against truncation until we
8894 * unlock the page.
8895 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008896int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008897{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008898 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008899 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008900 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008901 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8902 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008903 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008904 char *kaddr;
8905 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008906 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008907 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008908 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308909 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008910 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008911 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308912 u64 end;
8913
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008914 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008915
Jan Karab2b5ef52012-06-12 16:20:45 +02008916 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008917 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008918 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308919 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008920
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308921 /*
8922 * Reserving delalloc space after obtaining the page lock can lead to
8923 * deadlock. For example, if a dirty page is locked by this function
8924 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8925 * dirty page write out, then the btrfs_writepage() function could
8926 * end up waiting indefinitely to get a lock on the page currently
8927 * being processed by btrfs_page_mkwrite() function.
8928 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008929 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308930 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008931 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008932 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008933 reserved = 1;
8934 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008935 if (ret) {
8936 if (ret == -ENOMEM)
8937 ret = VM_FAULT_OOM;
8938 else /* -ENOSPC, -EIO, etc */
8939 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008940 if (reserved)
8941 goto out;
8942 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008943 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008944
Nick Piggin56a76f82009-03-31 15:23:23 -07008945 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008946again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008947 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008948 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008949
Chris Mason9ebefb182007-06-15 13:50:00 -04008950 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008951 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008952 /* page got truncated out from underneath us */
8953 goto out_unlock;
8954 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008955 wait_on_page_writeback(page);
8956
David Sterbaff13db42015-12-03 14:30:40 +01008957 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008958 set_page_extent_mapped(page);
8959
Chris Masoneb84ae02008-07-17 13:53:27 -04008960 /*
8961 * we can't set the delalloc bits if there are pending ordered
8962 * extents. Drop our locks and wait for them to finish
8963 */
Liu Bo9a9239a2017-01-24 15:58:51 -08008964 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008965 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008966 unlock_extent_cached(io_tree, page_start, page_end,
8967 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008968 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008969 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008970 btrfs_put_ordered_extent(ordered);
8971 goto again;
8972 }
8973
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008974 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008975 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008976 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008977 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308978 end = page_start + reserved_space - 1;
8979 spin_lock(&BTRFS_I(inode)->lock);
8980 BTRFS_I(inode)->outstanding_extents++;
8981 spin_unlock(&BTRFS_I(inode)->lock);
8982 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008983 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308984 }
8985 }
8986
Josef Bacikfbf19082009-10-01 17:10:23 -04008987 /*
Liu Bo54160342016-12-13 12:15:19 -08008988 * page_mkwrite gets called when the page is firstly dirtied after it's
8989 * faulted in, but write(2) could also dirty a page and set delalloc
8990 * bits, thus in this case for space account reason, we still need to
8991 * clear any delalloc bits within this page range since we have to
8992 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008993 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308994 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008995 EXTENT_DIRTY | EXTENT_DELALLOC |
8996 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008997 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008998
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308999 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009000 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009001 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009002 unlock_extent_cached(io_tree, page_start, page_end,
9003 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009004 ret = VM_FAULT_SIGBUS;
9005 goto out_unlock;
9006 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009007 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009008
9009 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009010 if (page_start + PAGE_SIZE > size)
9011 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009012 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009013 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009014
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009015 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009016 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009017 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009018 flush_dcache_page(page);
9019 kunmap(page);
9020 }
Chris Mason247e7432008-07-17 12:53:51 -04009021 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009022 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009023 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009024
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009025 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009026 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009027 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009028
Josef Bacik2ac55d42010-02-03 19:33:23 +00009029 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009030
9031out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009032 if (!ret) {
9033 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009034 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009035 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009036 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009037out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309038 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009039out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009040 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009041 return ret;
9042}
9043
Josef Bacika41ad392011-01-31 15:30:16 -05009044static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009045{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009046 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009047 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009048 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009049 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009050 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009051 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009052 u64 mask = fs_info->sectorsize - 1;
9053 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009054
Josef Bacik0ef8b722013-10-25 16:13:35 -04009055 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9056 (u64)-1);
9057 if (ret)
9058 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009059
Josef Bacikfcb80c22011-05-03 10:40:22 -04009060 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009061 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009062 * 3 things going on here
9063 *
9064 * 1) We need to reserve space for our orphan item and the space to
9065 * delete our orphan item. Lord knows we don't want to have a dangling
9066 * orphan item because we didn't reserve space to remove it.
9067 *
9068 * 2) We need to reserve space to update our inode.
9069 *
9070 * 3) We need to have something to cache all the space that is going to
9071 * be free'd up by the truncate operation, but also have some slack
9072 * space reserved in case it uses space during the truncate (thank you
9073 * very much snapshotting).
9074 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009075 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009076 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009077 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009078 * doesn't end up using space reserved for updating the inode or
9079 * removing the orphan item. We also need to be able to stop the
9080 * transaction and start a new one, which means we need to be able to
9081 * update the inode several times, and we have no idea of knowing how
9082 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009083 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009084 * Then there is the orphan item, which does indeed need to be held on
9085 * to for the whole operation, and we need nobody to touch this reserved
9086 * space except the orphan code.
9087 *
9088 * So that leaves us with
9089 *
9090 * 1) root->orphan_block_rsv - for the orphan deletion.
9091 * 2) rsv - for the truncate reservation, which we will steal from the
9092 * transaction reservation.
9093 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9094 * updating the inode.
9095 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009096 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009097 if (!rsv)
9098 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009099 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009100 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009101
Josef Bacik907cbce2011-08-08 13:46:15 -04009102 /*
Josef Bacik07127182011-08-19 10:29:59 -04009103 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009104 * 1 for updating the inode.
9105 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009106 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009107 if (IS_ERR(trans)) {
9108 err = PTR_ERR(trans);
9109 goto out;
9110 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009111
Josef Bacik907cbce2011-08-08 13:46:15 -04009112 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009113 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009114 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009115 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009116
Chris Mason5a3f23d2009-03-31 13:27:11 -04009117 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009118 * So if we truncate and then write and fsync we normally would just
9119 * write the extents that changed, which is a problem if we need to
9120 * first truncate that entire inode. So set this flag so we write out
9121 * all of the extents in the inode to the sync log so we're completely
9122 * safe.
9123 */
9124 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009125 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009126
Yan, Zheng80825102009-11-12 09:35:36 +00009127 while (1) {
9128 ret = btrfs_truncate_inode_items(trans, root, inode,
9129 inode->i_size,
9130 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009131 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009132 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009133 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009134 }
Chris Mason39279cc2007-06-12 06:35:45 -04009135
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009136 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009137 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009138 if (ret) {
9139 err = ret;
9140 break;
9141 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009142
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009143 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009144 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009145
9146 trans = btrfs_start_transaction(root, 2);
9147 if (IS_ERR(trans)) {
9148 ret = err = PTR_ERR(trans);
9149 trans = NULL;
9150 break;
9151 }
9152
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009153 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009154 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009155 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009156 BUG_ON(ret); /* shouldn't happen */
9157 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009158 }
9159
9160 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009161 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009162 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009163 if (ret)
9164 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009165 }
9166
Chris Mason917c16b2011-11-08 14:49:59 -05009167 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009168 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009169 ret = btrfs_update_inode(trans, root, inode);
9170 if (ret && !err)
9171 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009172
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009173 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009174 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009175 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009176out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009177 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009178
Josef Bacik3893e332011-01-31 16:03:11 -05009179 if (ret && !err)
9180 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009181
Josef Bacik3893e332011-01-31 16:03:11 -05009182 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009183}
9184
Sven Wegener3b963622008-06-09 21:57:42 -04009185/*
Chris Masond352ac62008-09-29 15:18:18 -04009186 * create a new subvolume directory/inode (helper for the ioctl).
9187 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009188int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009189 struct btrfs_root *new_root,
9190 struct btrfs_root *parent_root,
9191 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009192{
Chris Mason39279cc2007-06-12 06:35:45 -04009193 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009194 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009195 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009196
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009197 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9198 new_dirid, new_dirid,
9199 S_IFDIR | (~current_umask() & S_IRWXUGO),
9200 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009201 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009202 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009203 inode->i_op = &btrfs_dir_inode_operations;
9204 inode->i_fop = &btrfs_dir_file_operations;
9205
Miklos Szeredibfe86842011-10-28 14:13:29 +02009206 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009207 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009208 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009209
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009210 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9211 if (err)
9212 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009213 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009214 new_root->root_key.objectid, err);
9215
Yan, Zheng76dda932009-09-21 16:00:26 -04009216 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009217
Yan, Zheng76dda932009-09-21 16:00:26 -04009218 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009219 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009220}
9221
Chris Mason39279cc2007-06-12 06:35:45 -04009222struct inode *btrfs_alloc_inode(struct super_block *sb)
9223{
9224 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009225 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009226
9227 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9228 if (!ei)
9229 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009230
9231 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009232 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009233 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009234 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009235 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009236 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009237 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009238 ei->disk_i_size = 0;
9239 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009240 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009241 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009242 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009243 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009244 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009245 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009246
Josef Bacik9e0baf62011-07-15 15:16:44 +00009247 spin_lock_init(&ei->lock);
9248 ei->outstanding_extents = 0;
9249 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009250
Josef Bacik72ac3c02012-05-23 14:13:11 -04009251 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009252 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009253
Miao Xie16cdcec2011-04-22 18:12:22 +08009254 ei->delayed_node = NULL;
9255
chandan r9cc97d62012-07-04 12:48:07 +05309256 ei->i_otime.tv_sec = 0;
9257 ei->i_otime.tv_nsec = 0;
9258
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009259 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009260 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009261 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9262 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009263 ei->io_tree.track_uptodate = 1;
9264 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009265 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009266 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009267 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009268 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009269 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009270 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009271 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009272 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009273
9274 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009275}
9276
Josef Bacikaaedb552013-10-11 14:44:09 -04009277#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9278void btrfs_test_destroy_inode(struct inode *inode)
9279{
9280 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9281 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9282}
9283#endif
9284
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009285static void btrfs_i_callback(struct rcu_head *head)
9286{
9287 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009288 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9289}
9290
Chris Mason39279cc2007-06-12 06:35:45 -04009291void btrfs_destroy_inode(struct inode *inode)
9292{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009293 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009294 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009295 struct btrfs_root *root = BTRFS_I(inode)->root;
9296
Al Virob3d9b7a2012-06-09 13:51:19 -04009297 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009298 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009299 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9300 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009301 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9302 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009303 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009304
Chris Mason5a3f23d2009-03-31 13:27:11 -04009305 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009306 * This can happen where we create an inode, but somebody else also
9307 * created the same inode and we need to destroy the one we already
9308 * created.
9309 */
9310 if (!root)
9311 goto free;
9312
Josef Bacik8a35d952012-05-23 14:26:42 -04009313 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9314 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009315 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009316 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009317 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009318 }
Josef Bacik7b128762008-07-24 12:17:14 -04009319
Chris Masond3977122009-01-05 21:25:51 -05009320 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009321 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9322 if (!ordered)
9323 break;
9324 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009325 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009326 "found ordered extent %llu %llu on inode cleanup",
9327 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009328 btrfs_remove_ordered_extent(inode, ordered);
9329 btrfs_put_ordered_extent(ordered);
9330 btrfs_put_ordered_extent(ordered);
9331 }
9332 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009333 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009334 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009335 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009336free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009337 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009338}
9339
Al Viro45321ac2010-06-07 13:43:19 -04009340int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009341{
9342 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009343
Naohiro Aota6379ef92013-06-06 09:56:34 +00009344 if (root == NULL)
9345 return 1;
9346
Liu Bofa6ac872013-02-20 14:10:23 +00009347 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009348 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009349 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009350 else
Al Viro45321ac2010-06-07 13:43:19 -04009351 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009352}
9353
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009354static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009355{
9356 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9357
9358 inode_init_once(&ei->vfs_inode);
9359}
9360
9361void btrfs_destroy_cachep(void)
9362{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009363 /*
9364 * Make sure all delayed rcu free inodes are flushed before we
9365 * destroy cache.
9366 */
9367 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009368 kmem_cache_destroy(btrfs_inode_cachep);
9369 kmem_cache_destroy(btrfs_trans_handle_cachep);
9370 kmem_cache_destroy(btrfs_transaction_cachep);
9371 kmem_cache_destroy(btrfs_path_cachep);
9372 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009373}
9374
9375int btrfs_init_cachep(void)
9376{
David Sterba837e1972012-09-07 03:00:48 -06009377 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009378 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009379 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9380 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009381 if (!btrfs_inode_cachep)
9382 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009383
David Sterba837e1972012-09-07 03:00:48 -06009384 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009385 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009386 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009387 if (!btrfs_trans_handle_cachep)
9388 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009389
David Sterba837e1972012-09-07 03:00:48 -06009390 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009391 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009392 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009393 if (!btrfs_transaction_cachep)
9394 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009395
David Sterba837e1972012-09-07 03:00:48 -06009396 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009397 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009398 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009399 if (!btrfs_path_cachep)
9400 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009401
David Sterba837e1972012-09-07 03:00:48 -06009402 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009403 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009404 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009405 if (!btrfs_free_space_cachep)
9406 goto fail;
9407
Chris Mason39279cc2007-06-12 06:35:45 -04009408 return 0;
9409fail:
9410 btrfs_destroy_cachep();
9411 return -ENOMEM;
9412}
9413
9414static int btrfs_getattr(struct vfsmount *mnt,
9415 struct dentry *dentry, struct kstat *stat)
9416{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009417 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009418 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009419 u32 blocksize = inode->i_sb->s_blocksize;
9420
Chris Mason39279cc2007-06-12 06:35:45 -04009421 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009422 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009423
9424 spin_lock(&BTRFS_I(inode)->lock);
9425 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9426 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009427 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009428 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009429 return 0;
9430}
9431
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009432static int btrfs_rename_exchange(struct inode *old_dir,
9433 struct dentry *old_dentry,
9434 struct inode *new_dir,
9435 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009436{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009437 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009438 struct btrfs_trans_handle *trans;
9439 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009440 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009441 struct inode *new_inode = new_dentry->d_inode;
9442 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009443 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009444 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009445 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9446 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009447 u64 old_idx = 0;
9448 u64 new_idx = 0;
9449 u64 root_objectid;
9450 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009451 bool root_log_pinned = false;
9452 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009453
9454 /* we only allow rename subvolume link between subvolumes */
9455 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9456 return -EXDEV;
9457
9458 /* close the race window with snapshot create/destroy ioctl */
9459 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009460 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009461 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009462 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009463
9464 /*
9465 * We want to reserve the absolute worst case amount of items. So if
9466 * both inodes are subvols and we need to unlink them then that would
9467 * require 4 item modifications, but if they are both normal inodes it
9468 * would require 5 item modifications, so we'll assume their normal
9469 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9470 * should cover the worst case number of items we'll modify.
9471 */
9472 trans = btrfs_start_transaction(root, 12);
9473 if (IS_ERR(trans)) {
9474 ret = PTR_ERR(trans);
9475 goto out_notrans;
9476 }
9477
9478 /*
9479 * We need to find a free sequence number both in the source and
9480 * in the destination directory for the exchange.
9481 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009482 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009483 if (ret)
9484 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009485 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009486 if (ret)
9487 goto out_fail;
9488
9489 BTRFS_I(old_inode)->dir_index = 0ULL;
9490 BTRFS_I(new_inode)->dir_index = 0ULL;
9491
9492 /* Reference for the source. */
9493 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9494 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009495 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009496 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009497 btrfs_pin_log_trans(root);
9498 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009499 ret = btrfs_insert_inode_ref(trans, dest,
9500 new_dentry->d_name.name,
9501 new_dentry->d_name.len,
9502 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009503 btrfs_ino(BTRFS_I(new_dir)),
9504 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009505 if (ret)
9506 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009507 }
9508
9509 /* And now for the dest. */
9510 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9511 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009512 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009513 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009514 btrfs_pin_log_trans(dest);
9515 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009516 ret = btrfs_insert_inode_ref(trans, root,
9517 old_dentry->d_name.name,
9518 old_dentry->d_name.len,
9519 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009520 btrfs_ino(BTRFS_I(old_dir)),
9521 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009522 if (ret)
9523 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009524 }
9525
9526 /* Update inode version and ctime/mtime. */
9527 inode_inc_iversion(old_dir);
9528 inode_inc_iversion(new_dir);
9529 inode_inc_iversion(old_inode);
9530 inode_inc_iversion(new_inode);
9531 old_dir->i_ctime = old_dir->i_mtime = ctime;
9532 new_dir->i_ctime = new_dir->i_mtime = ctime;
9533 old_inode->i_ctime = ctime;
9534 new_inode->i_ctime = ctime;
9535
9536 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009537 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9538 BTRFS_I(old_inode), 1);
9539 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9540 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009541 }
9542
9543 /* src is a subvolume */
9544 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9545 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9546 ret = btrfs_unlink_subvol(trans, root, old_dir,
9547 root_objectid,
9548 old_dentry->d_name.name,
9549 old_dentry->d_name.len);
9550 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009551 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9552 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009553 old_dentry->d_name.name,
9554 old_dentry->d_name.len);
9555 if (!ret)
9556 ret = btrfs_update_inode(trans, root, old_inode);
9557 }
9558 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009559 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009560 goto out_fail;
9561 }
9562
9563 /* dest is a subvolume */
9564 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9565 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9566 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9567 root_objectid,
9568 new_dentry->d_name.name,
9569 new_dentry->d_name.len);
9570 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009571 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9572 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009573 new_dentry->d_name.name,
9574 new_dentry->d_name.len);
9575 if (!ret)
9576 ret = btrfs_update_inode(trans, dest, new_inode);
9577 }
9578 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009579 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009580 goto out_fail;
9581 }
9582
9583 ret = btrfs_add_link(trans, new_dir, old_inode,
9584 new_dentry->d_name.name,
9585 new_dentry->d_name.len, 0, old_idx);
9586 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009587 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009588 goto out_fail;
9589 }
9590
9591 ret = btrfs_add_link(trans, old_dir, new_inode,
9592 old_dentry->d_name.name,
9593 old_dentry->d_name.len, 0, new_idx);
9594 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009595 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009596 goto out_fail;
9597 }
9598
9599 if (old_inode->i_nlink == 1)
9600 BTRFS_I(old_inode)->dir_index = old_idx;
9601 if (new_inode->i_nlink == 1)
9602 BTRFS_I(new_inode)->dir_index = new_idx;
9603
Filipe Manana86e8aa02016-05-05 02:02:27 +01009604 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009605 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009606 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9607 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009608 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009609 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009610 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009611 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009612 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009613 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9614 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009615 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009616 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617 }
9618out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009619 /*
9620 * If we have pinned a log and an error happened, we unpin tasks
9621 * trying to sync the log and force them to fallback to a transaction
9622 * commit if the log currently contains any of the inodes involved in
9623 * this rename operation (to ensure we do not persist a log with an
9624 * inconsistent state for any of these inodes or leading to any
9625 * inconsistencies when replayed). If the transaction was aborted, the
9626 * abortion reason is propagated to userspace when attempting to commit
9627 * the transaction. If the log does not contain any of these inodes, we
9628 * allow the tasks to sync it.
9629 */
9630 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009631 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9632 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9633 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009634 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009635 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009636 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009637
9638 if (root_log_pinned) {
9639 btrfs_end_log_trans(root);
9640 root_log_pinned = false;
9641 }
9642 if (dest_log_pinned) {
9643 btrfs_end_log_trans(dest);
9644 dest_log_pinned = false;
9645 }
9646 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009647 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009648out_notrans:
9649 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009650 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009651 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009652 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009653
9654 return ret;
9655}
9656
9657static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9658 struct btrfs_root *root,
9659 struct inode *dir,
9660 struct dentry *dentry)
9661{
9662 int ret;
9663 struct inode *inode;
9664 u64 objectid;
9665 u64 index;
9666
9667 ret = btrfs_find_free_ino(root, &objectid);
9668 if (ret)
9669 return ret;
9670
9671 inode = btrfs_new_inode(trans, root, dir,
9672 dentry->d_name.name,
9673 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009674 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009675 objectid,
9676 S_IFCHR | WHITEOUT_MODE,
9677 &index);
9678
9679 if (IS_ERR(inode)) {
9680 ret = PTR_ERR(inode);
9681 return ret;
9682 }
9683
9684 inode->i_op = &btrfs_special_inode_operations;
9685 init_special_inode(inode, inode->i_mode,
9686 WHITEOUT_DEV);
9687
9688 ret = btrfs_init_inode_security(trans, inode, dir,
9689 &dentry->d_name);
9690 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009691 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692
9693 ret = btrfs_add_nondir(trans, dir, dentry,
9694 inode, 0, index);
9695 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009696 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009697
9698 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009699out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009700 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009701 if (ret)
9702 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009703 iput(inode);
9704
Filipe Mananac9901612016-05-05 01:41:57 +01009705 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009706}
9707
Chris Mason39279cc2007-06-12 06:35:45 -04009708static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709 struct inode *new_dir, struct dentry *new_dentry,
9710 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009711{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009712 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009713 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009714 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009715 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9716 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009717 struct inode *new_inode = d_inode(new_dentry);
9718 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009719 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009720 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009721 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009722 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009723 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009724
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009725 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009726 return -EPERM;
9727
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009728 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009729 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009730 return -EXDEV;
9731
Li Zefan33345d012011-04-20 10:31:50 +08009732 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009733 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009734 return -ENOTEMPTY;
9735
Chris Mason39279cc2007-06-12 06:35:45 -04009736 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009737 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009738 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009739
9740
9741 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009742 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009743 new_dentry->d_name.name,
9744 new_dentry->d_name.len);
9745
9746 if (ret) {
9747 if (ret == -EEXIST) {
9748 /* we shouldn't get
9749 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309750 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009751 return ret;
9752 }
9753 } else {
9754 /* maybe -EOVERFLOW */
9755 return ret;
9756 }
9757 }
9758 ret = 0;
9759
Chris Mason5a3f23d2009-03-31 13:27:11 -04009760 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009761 * we're using rename to replace one file with another. Start IO on it
9762 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009763 */
Chris Mason8d875f92014-08-12 10:47:42 -07009764 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009765 filemap_flush(old_inode->i_mapping);
9766
Yan, Zheng76dda932009-09-21 16:00:26 -04009767 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009768 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009769 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009770 /*
9771 * We want to reserve the absolute worst case amount of items. So if
9772 * both inodes are subvols and we need to unlink them then that would
9773 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009774 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009775 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9776 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009777 * If our rename has the whiteout flag, we need more 5 units for the
9778 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9779 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009780 */
Filipe Manana5062af32016-05-05 10:26:26 +01009781 trans_num_items = 11;
9782 if (flags & RENAME_WHITEOUT)
9783 trans_num_items += 5;
9784 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009785 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009786 ret = PTR_ERR(trans);
9787 goto out_notrans;
9788 }
Chris Mason5f39d392007-10-15 16:14:19 -04009789
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009790 if (dest != root)
9791 btrfs_record_root_in_trans(trans, dest);
9792
Nikolay Borisov877574e2017-02-20 13:50:33 +02009793 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009794 if (ret)
9795 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009796
Miao Xie67de1172013-12-26 13:07:06 +08009797 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009798 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009799 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009800 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009801 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009802 btrfs_pin_log_trans(root);
9803 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009804 ret = btrfs_insert_inode_ref(trans, dest,
9805 new_dentry->d_name.name,
9806 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009807 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009808 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009809 if (ret)
9810 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009811 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009812
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009813 inode_inc_iversion(old_dir);
9814 inode_inc_iversion(new_dir);
9815 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009816 old_dir->i_ctime = old_dir->i_mtime =
9817 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009818 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009819
Chris Mason12fcfd22009-03-24 10:24:20 -04009820 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009821 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9822 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009823
Li Zefan33345d012011-04-20 10:31:50 +08009824 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009825 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9826 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9827 old_dentry->d_name.name,
9828 old_dentry->d_name.len);
9829 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009830 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9831 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009832 old_dentry->d_name.name,
9833 old_dentry->d_name.len);
9834 if (!ret)
9835 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009836 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009837 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009838 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009839 goto out_fail;
9840 }
Chris Mason39279cc2007-06-12 06:35:45 -04009841
9842 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009843 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009844 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009845 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009846 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9847 root_objectid = BTRFS_I(new_inode)->location.objectid;
9848 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9849 root_objectid,
9850 new_dentry->d_name.name,
9851 new_dentry->d_name.len);
9852 BUG_ON(new_inode->i_nlink == 0);
9853 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009854 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9855 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009856 new_dentry->d_name.name,
9857 new_dentry->d_name.len);
9858 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009859 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009860 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009861 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009862 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009863 goto out_fail;
9864 }
Chris Mason39279cc2007-06-12 06:35:45 -04009865 }
Josef Bacikaec74772008-07-24 12:12:38 -04009866
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009867 ret = btrfs_add_link(trans, new_dir, old_inode,
9868 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009869 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009870 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009871 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009872 goto out_fail;
9873 }
Chris Mason39279cc2007-06-12 06:35:45 -04009874
Miao Xie67de1172013-12-26 13:07:06 +08009875 if (old_inode->i_nlink == 1)
9876 BTRFS_I(old_inode)->dir_index = index;
9877
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009878 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009879 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009880
David Sterbaf85b7372017-01-20 14:54:07 +01009881 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9882 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009883 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009884 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009885 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009886
9887 if (flags & RENAME_WHITEOUT) {
9888 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9889 old_dentry);
9890
9891 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009892 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009893 goto out_fail;
9894 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009895 }
Chris Mason39279cc2007-06-12 06:35:45 -04009896out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009897 /*
9898 * If we have pinned the log and an error happened, we unpin tasks
9899 * trying to sync the log and force them to fallback to a transaction
9900 * commit if the log currently contains any of the inodes involved in
9901 * this rename operation (to ensure we do not persist a log with an
9902 * inconsistent state for any of these inodes or leading to any
9903 * inconsistencies when replayed). If the transaction was aborted, the
9904 * abortion reason is propagated to userspace when attempting to commit
9905 * the transaction. If the log does not contain any of these inodes, we
9906 * allow the tasks to sync it.
9907 */
9908 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009909 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9910 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9911 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009912 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009913 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009914 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009915
9916 btrfs_end_log_trans(root);
9917 log_pinned = false;
9918 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009919 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009920out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009921 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009922 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009923
Chris Mason39279cc2007-06-12 06:35:45 -04009924 return ret;
9925}
9926
Miklos Szeredi80ace852014-07-23 15:15:32 +02009927static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9928 struct inode *new_dir, struct dentry *new_dentry,
9929 unsigned int flags)
9930{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009931 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009932 return -EINVAL;
9933
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009934 if (flags & RENAME_EXCHANGE)
9935 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9936 new_dentry);
9937
9938 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009939}
9940
Miao Xie8ccf6f192012-10-25 09:28:04 +00009941static void btrfs_run_delalloc_work(struct btrfs_work *work)
9942{
9943 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009944 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009945
9946 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9947 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009948 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009949 filemap_flush(inode->i_mapping);
9950 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9951 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009952 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009953
9954 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009955 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009956 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009957 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009958 complete(&delalloc_work->completion);
9959}
9960
9961struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009962 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009963{
9964 struct btrfs_delalloc_work *work;
9965
David Sterba100d5702015-12-08 14:39:32 +01009966 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009967 if (!work)
9968 return NULL;
9969
9970 init_completion(&work->completion);
9971 INIT_LIST_HEAD(&work->list);
9972 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009973 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009974 WARN_ON_ONCE(!inode);
9975 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9976 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009977
9978 return work;
9979}
9980
9981void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9982{
9983 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009984 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009985}
9986
Chris Masond352ac62008-09-29 15:18:18 -04009987/*
9988 * some fairly slow code that needs optimization. This walks the list
9989 * of all the inodes with pending delalloc and forces them to disk.
9990 */
Miao Xie6c255e62014-03-06 13:55:01 +08009991static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9992 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009993{
Chris Masonea8c2812008-08-04 23:17:27 -04009994 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009995 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009996 struct btrfs_delalloc_work *work, *next;
9997 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009998 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009999 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010000
Miao Xie8ccf6f192012-10-25 09:28:04 +000010001 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010002 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010003
Miao Xie573bfb72014-03-06 13:55:03 +080010004 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010005 spin_lock(&root->delalloc_lock);
10006 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010007 while (!list_empty(&splice)) {
10008 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010009 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010010
Miao Xieeb73c1b2013-05-15 07:48:22 +000010011 list_move_tail(&binode->delalloc_inodes,
10012 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010013 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010014 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010015 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010016 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010017 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010018 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010019
David Sterba651d4942015-11-27 19:24:16 +010010020 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010021 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010022 if (delay_iput)
10023 btrfs_add_delayed_iput(inode);
10024 else
10025 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010026 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010027 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010028 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010029 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010030 btrfs_queue_work(root->fs_info->flush_workers,
10031 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010032 ret++;
10033 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010034 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010035 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010036 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010037 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010038 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010039
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010040out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010041 list_for_each_entry_safe(work, next, &works, list) {
10042 list_del_init(&work->list);
10043 btrfs_wait_and_free_delalloc_work(work);
10044 }
10045
Miao Xieeb73c1b2013-05-15 07:48:22 +000010046 if (!list_empty_careful(&splice)) {
10047 spin_lock(&root->delalloc_lock);
10048 list_splice_tail(&splice, &root->delalloc_inodes);
10049 spin_unlock(&root->delalloc_lock);
10050 }
Miao Xie573bfb72014-03-06 13:55:03 +080010051 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010052 return ret;
10053}
10054
10055int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10056{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010057 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010058 int ret;
10059
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010060 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010061 return -EROFS;
10062
Miao Xie6c255e62014-03-06 13:55:01 +080010063 ret = __start_delalloc_inodes(root, delay_iput, -1);
10064 if (ret > 0)
10065 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010066 /*
10067 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010068 * we have to make sure the IO is actually started and that
10069 * ordered extents get created before we return
10070 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010071 atomic_inc(&fs_info->async_submit_draining);
10072 while (atomic_read(&fs_info->nr_async_submits) ||
10073 atomic_read(&fs_info->async_delalloc_pages)) {
10074 wait_event(fs_info->async_submit_wait,
10075 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10076 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010077 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010078 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010079 return ret;
10080}
10081
Miao Xie6c255e62014-03-06 13:55:01 +080010082int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10083 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010084{
10085 struct btrfs_root *root;
10086 struct list_head splice;
10087 int ret;
10088
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010089 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010090 return -EROFS;
10091
10092 INIT_LIST_HEAD(&splice);
10093
Miao Xie573bfb72014-03-06 13:55:03 +080010094 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010095 spin_lock(&fs_info->delalloc_root_lock);
10096 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010097 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010098 root = list_first_entry(&splice, struct btrfs_root,
10099 delalloc_root);
10100 root = btrfs_grab_fs_root(root);
10101 BUG_ON(!root);
10102 list_move_tail(&root->delalloc_root,
10103 &fs_info->delalloc_roots);
10104 spin_unlock(&fs_info->delalloc_root_lock);
10105
Miao Xie6c255e62014-03-06 13:55:01 +080010106 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010107 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010108 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010109 goto out;
10110
Miao Xie6c255e62014-03-06 13:55:01 +080010111 if (nr != -1) {
10112 nr -= ret;
10113 WARN_ON(nr < 0);
10114 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010115 spin_lock(&fs_info->delalloc_root_lock);
10116 }
10117 spin_unlock(&fs_info->delalloc_root_lock);
10118
Miao Xie6c255e62014-03-06 13:55:01 +080010119 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010120 atomic_inc(&fs_info->async_submit_draining);
10121 while (atomic_read(&fs_info->nr_async_submits) ||
10122 atomic_read(&fs_info->async_delalloc_pages)) {
10123 wait_event(fs_info->async_submit_wait,
10124 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10125 atomic_read(&fs_info->async_delalloc_pages) == 0));
10126 }
10127 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010128out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010129 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010130 spin_lock(&fs_info->delalloc_root_lock);
10131 list_splice_tail(&splice, &fs_info->delalloc_roots);
10132 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010133 }
Miao Xie573bfb72014-03-06 13:55:03 +080010134 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010135 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010136}
10137
Chris Mason39279cc2007-06-12 06:35:45 -040010138static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10139 const char *symname)
10140{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010141 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010142 struct btrfs_trans_handle *trans;
10143 struct btrfs_root *root = BTRFS_I(dir)->root;
10144 struct btrfs_path *path;
10145 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010146 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010147 int err;
10148 int drop_inode = 0;
10149 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010150 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010151 int name_len;
10152 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010153 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010154 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010155 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010156
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010157 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010158 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010159 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010160
Josef Bacik9ed74f22009-09-11 16:12:44 -040010161 /*
10162 * 2 items for inode item and ref
10163 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010164 * 1 item for updating parent inode item
10165 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010166 * 1 item for xattr if selinux is on
10167 */
Filipe Manana9269d122015-12-31 18:16:29 +000010168 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010169 if (IS_ERR(trans))
10170 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010171
Li Zefan581bb052011-04-20 10:06:11 +080010172 err = btrfs_find_free_ino(root, &objectid);
10173 if (err)
10174 goto out_unlock;
10175
Josef Bacikaec74772008-07-24 12:12:38 -040010176 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010177 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10178 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010179 if (IS_ERR(inode)) {
10180 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010181 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010182 }
Chris Mason39279cc2007-06-12 06:35:45 -040010183
Casey Schauflerad19db72011-12-15 10:09:07 -050010184 /*
10185 * If the active LSM wants to access the inode during
10186 * d_instantiate it needs these. Smack checks to see
10187 * if the filesystem supports xattrs by looking at the
10188 * ops vector.
10189 */
10190 inode->i_fop = &btrfs_file_operations;
10191 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010192 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010193 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10194
10195 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10196 if (err)
10197 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010198
Chris Mason39279cc2007-06-12 06:35:45 -040010199 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010200 if (!path) {
10201 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010202 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010203 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010204 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010205 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010206 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010207 datasize = btrfs_file_extent_calc_inline_size(name_len);
10208 err = btrfs_insert_empty_item(trans, root, path, &key,
10209 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010210 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010211 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010212 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010213 }
Chris Mason5f39d392007-10-15 16:14:19 -040010214 leaf = path->nodes[0];
10215 ei = btrfs_item_ptr(leaf, path->slots[0],
10216 struct btrfs_file_extent_item);
10217 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10218 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010219 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010220 btrfs_set_file_extent_encryption(leaf, ei, 0);
10221 btrfs_set_file_extent_compression(leaf, ei, 0);
10222 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10223 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10224
Chris Mason39279cc2007-06-12 06:35:45 -040010225 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010226 write_extent_buffer(leaf, symname, ptr, name_len);
10227 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010228 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010229
Chris Mason39279cc2007-06-12 06:35:45 -040010230 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010231 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010232 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010233 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010234 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010235 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010236 /*
10237 * Last step, add directory indexes for our symlink inode. This is the
10238 * last step to avoid extra cleanup of these indexes if an error happens
10239 * elsewhere above.
10240 */
10241 if (!err)
10242 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010243 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010244 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010245 goto out_unlock_inode;
10246 }
10247
10248 unlock_new_inode(inode);
10249 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010250
10251out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010252 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010253 if (drop_inode) {
10254 inode_dec_link_count(inode);
10255 iput(inode);
10256 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010257 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010258 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010259
10260out_unlock_inode:
10261 drop_inode = 1;
10262 unlock_new_inode(inode);
10263 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010264}
Chris Mason16432982008-04-10 10:23:21 -040010265
Josef Bacik0af3d002010-06-21 14:48:16 -040010266static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10267 u64 start, u64 num_bytes, u64 min_size,
10268 loff_t actual_len, u64 *alloc_hint,
10269 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010270{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010271 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010272 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10273 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010274 struct btrfs_root *root = BTRFS_I(inode)->root;
10275 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010276 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010277 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010278 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010279 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010280 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010281 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010282 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010283
Josef Bacik0af3d002010-06-21 14:48:16 -040010284 if (trans)
10285 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010286 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010287 if (own_trans) {
10288 trans = btrfs_start_transaction(root, 3);
10289 if (IS_ERR(trans)) {
10290 ret = PTR_ERR(trans);
10291 break;
10292 }
Yan Zhengd899e052008-10-30 14:25:28 -040010293 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010294
Byongho Leeee221842015-12-15 01:42:10 +090010295 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010296 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010297 /*
10298 * If we are severely fragmented we could end up with really
10299 * small allocations, so if the allocator is returning small
10300 * chunks lets make its job easier by only searching for those
10301 * sized chunks.
10302 */
10303 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010304 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10305 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010306 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010307 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010308 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010309 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010310 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010311 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010312
Josef Bacik0b670dc2015-09-23 17:11:16 -040010313 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010314 ret = insert_reserved_file_extent(trans, inode,
10315 cur_offset, ins.objectid,
10316 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010317 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010318 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010319 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010320 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010321 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010322 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010323 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010324 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010325 break;
10326 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010327
Chris Masona1ed8352009-09-11 12:27:37 -040010328 btrfs_drop_extent_cache(inode, cur_offset,
10329 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010330
Josef Bacik5dc562c2012-08-17 13:14:17 -040010331 em = alloc_extent_map();
10332 if (!em) {
10333 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10334 &BTRFS_I(inode)->runtime_flags);
10335 goto next;
10336 }
10337
10338 em->start = cur_offset;
10339 em->orig_start = cur_offset;
10340 em->len = ins.offset;
10341 em->block_start = ins.objectid;
10342 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010343 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010344 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010345 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010346 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10347 em->generation = trans->transid;
10348
10349 while (1) {
10350 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010351 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010352 write_unlock(&em_tree->lock);
10353 if (ret != -EEXIST)
10354 break;
10355 btrfs_drop_extent_cache(inode, cur_offset,
10356 cur_offset + ins.offset - 1,
10357 0);
10358 }
10359 free_extent_map(em);
10360next:
Yan Zhengd899e052008-10-30 14:25:28 -040010361 num_bytes -= ins.offset;
10362 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010363 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010364
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010365 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010366 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010367 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010368 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010369 (actual_len > inode->i_size) &&
10370 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010371 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010372 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010373 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010374 i_size = cur_offset;
10375 i_size_write(inode, i_size);
10376 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010377 }
10378
Yan Zhengd899e052008-10-30 14:25:28 -040010379 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010380
10381 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010382 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010383 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010384 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010385 break;
10386 }
Yan Zhengd899e052008-10-30 14:25:28 -040010387
Josef Bacik0af3d002010-06-21 14:48:16 -040010388 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010389 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010390 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010391 if (cur_offset < end)
10392 btrfs_free_reserved_data_space(inode, cur_offset,
10393 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010394 return ret;
10395}
10396
Josef Bacik0af3d002010-06-21 14:48:16 -040010397int btrfs_prealloc_file_range(struct inode *inode, int mode,
10398 u64 start, u64 num_bytes, u64 min_size,
10399 loff_t actual_len, u64 *alloc_hint)
10400{
10401 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10402 min_size, actual_len, alloc_hint,
10403 NULL);
10404}
10405
10406int btrfs_prealloc_file_range_trans(struct inode *inode,
10407 struct btrfs_trans_handle *trans, int mode,
10408 u64 start, u64 num_bytes, u64 min_size,
10409 loff_t actual_len, u64 *alloc_hint)
10410{
10411 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10412 min_size, actual_len, alloc_hint, trans);
10413}
10414
Chris Masone6dcd2d2008-07-17 12:53:50 -040010415static int btrfs_set_page_dirty(struct page *page)
10416{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010417 return __set_page_dirty_nobuffers(page);
10418}
10419
Al Viro10556cb2011-06-20 19:28:19 -040010420static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010421{
Li Zefanb83cc962010-12-20 16:04:08 +080010422 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010423 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010424
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010425 if (mask & MAY_WRITE &&
10426 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10427 if (btrfs_root_readonly(root))
10428 return -EROFS;
10429 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10430 return -EACCES;
10431 }
Al Viro2830ba72011-06-20 19:16:29 -040010432 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010433}
Chris Mason39279cc2007-06-12 06:35:45 -040010434
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010435static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10436{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010437 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010438 struct btrfs_trans_handle *trans;
10439 struct btrfs_root *root = BTRFS_I(dir)->root;
10440 struct inode *inode = NULL;
10441 u64 objectid;
10442 u64 index;
10443 int ret = 0;
10444
10445 /*
10446 * 5 units required for adding orphan entry
10447 */
10448 trans = btrfs_start_transaction(root, 5);
10449 if (IS_ERR(trans))
10450 return PTR_ERR(trans);
10451
10452 ret = btrfs_find_free_ino(root, &objectid);
10453 if (ret)
10454 goto out;
10455
10456 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010457 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010458 if (IS_ERR(inode)) {
10459 ret = PTR_ERR(inode);
10460 inode = NULL;
10461 goto out;
10462 }
10463
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010464 inode->i_fop = &btrfs_file_operations;
10465 inode->i_op = &btrfs_file_inode_operations;
10466
10467 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010468 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10469
Chris Masonb0d5d102014-09-08 13:08:51 -070010470 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10471 if (ret)
10472 goto out_inode;
10473
10474 ret = btrfs_update_inode(trans, root, inode);
10475 if (ret)
10476 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010477 ret = btrfs_orphan_add(trans, inode);
10478 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010479 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010480
Filipe Manana5762b5c2014-08-01 00:10:32 +010010481 /*
10482 * We set number of links to 0 in btrfs_new_inode(), and here we set
10483 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10484 * through:
10485 *
10486 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10487 */
10488 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010489 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010490 d_tmpfile(dentry, inode);
10491 mark_inode_dirty(inode);
10492
10493out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010494 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010495 if (ret)
10496 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010497 btrfs_balance_delayed_items(fs_info);
10498 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010499 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010500
10501out_inode:
10502 unlock_new_inode(inode);
10503 goto out;
10504
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010505}
10506
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010507static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010508 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010509 .lookup = btrfs_lookup,
10510 .create = btrfs_create,
10511 .unlink = btrfs_unlink,
10512 .link = btrfs_link,
10513 .mkdir = btrfs_mkdir,
10514 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010515 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010516 .symlink = btrfs_symlink,
10517 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010518 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010519 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010520 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010521 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010522 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010523 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010524 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010525};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010526static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010527 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010528 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010529 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010530};
Yan, Zheng76dda932009-09-21 16:00:26 -040010531
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010532static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010533 .llseek = generic_file_llseek,
10534 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010535 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010536 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010537#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010538 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010539#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010540 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010541 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010542};
10543
David Sterba20e55062015-11-19 11:42:28 +010010544static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010545 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010546 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010547 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010548 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010549 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010550 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010551 .set_bit_hook = btrfs_set_bit_hook,
10552 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010553 .merge_extent_hook = btrfs_merge_extent_hook,
10554 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010555};
10556
Chris Mason35054392009-01-21 13:11:13 -050010557/*
10558 * btrfs doesn't support the bmap operation because swapfiles
10559 * use bmap to make a mapping of extents in the file. They assume
10560 * these extents won't change over the life of the file and they
10561 * use the bmap result to do IO directly to the drive.
10562 *
10563 * the btrfs bmap call would return logical addresses that aren't
10564 * suitable for IO and they also will change frequently as COW
10565 * operations happen. So, swapfile + btrfs == corruption.
10566 *
10567 * For now we're avoiding this by dropping bmap.
10568 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010569static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010570 .readpage = btrfs_readpage,
10571 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010572 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010573 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010574 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010575 .invalidatepage = btrfs_invalidatepage,
10576 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010577 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010578 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010579};
10580
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010581static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010582 .readpage = btrfs_readpage,
10583 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010584 .invalidatepage = btrfs_invalidatepage,
10585 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010586};
10587
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010588static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010589 .getattr = btrfs_getattr,
10590 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010591 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010592 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010593 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010594 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010595 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010596 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010597};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010598static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010599 .getattr = btrfs_getattr,
10600 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010601 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010602 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010603 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010604 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010605 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010606};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010607static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010608 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010609 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010610 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010611 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010612 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010613 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010614};
Yan, Zheng76dda932009-09-21 16:00:26 -040010615
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010616const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010617 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010618 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010619};